无法在 Activity 上显示列表

Can't display list on Activity

我试图在从文本文件中读取数据后显示位置的 ListView,当我打开 activity 时,我创建了一个自定义适配器和一个新的 activity bu,我只得到一个白屏。

这是我添加的适配器的代码:

public class LocationAdapter extends BaseAdapter {

        private List<Location> Locations;
        int monLayout;
        LayoutInflater inflater;


        public LocationAdapter(Context context, int layout){
            monLayout=layout;
            inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            Locations = new ArrayList<Location>();
        }



        private class Location{
            public String name;
            public String address;
            public Long date;

            public Location(String _name,String _address , Long _date){
                name=_name;
                address=_address;
                date=_date;

            }
        }


        private class ViewHolder{

            TextView name_view;
            TextView address_view;
            TextView date_view;

            public ViewHolder(View rowLayout){

                name_view = (TextView)rowLayout.findViewById(R.id.name);
                date_view = (TextView)rowLayout.findViewById(R.id.date);
                address_view = (TextView)rowLayout.findViewById(R.id.address);
            }
        }

        public void addLocation(String _name,String _address,Long _date){
            //Création d'une nouvelle location avec les données en paramètres
            Location new_loc = new Location(_name, _address,_date);

            //Ajout de la location à la liste des locations
            Locations.add(new_loc);

        }
    /*Méthodes de la classe mère*/

        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View view = convertView;

            if (view == null)
                view = inflater.inflate(monLayout, parent, false);

            ViewHolder holder = (ViewHolder)view.getTag();
            if (holder == null)
            {
                holder = new ViewHolder(view);
                view.setTag(holder);
            }

            Location location = (Location)getItem(position);

            holder.name_view.setText(location.name);
            holder.address_view.setText(location.address);
            holder.date_view.setText("Test");

            return view;
        }
}

这是我的 Activity 的 Oncreate 方法的代码:

  public class SortedLocationsListActivity extends ListActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_sorted_locations_list);


            InputStream is = getResources().openRawResource(R.raw.locations);
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            List<String> maListe = new ArrayList<String>();
            String myligne;
            LocationAdapter adapter = new LocationAdapter(this, R.layout.row_location);

            try {
                while((myligne = in.readLine()) != null)
                {
                    String a[] = myligne.split(";");
                    Long _date=Long.parseLong(a[2], 36);
                    System.out.println(a[0]+" "+a[1]+" "+a[2]);
                    adapter.addLocation(a[0],a[1],_date);
                }
                setListAdapter(adapter);

            }
            catch(IOException e)
            {
                System.out.println("Error");
            }

        }

这是我想为列表的每个元素使用的布局的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingBottom="5dp"
    android:paddingTop="10dp">
    <TextView android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Faculté de Droit"
        android:textStyle="bold"
        android:textColor="#000"
        android:layout_marginBottom="10dp"
        android:layout_alignParentLeft="true"/>
    <TextView android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="7 nov. 2014"
        android:layout_alignParentRight="true"/>
    <TextView android:id="@+id/address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="41 Boulevard François Mitterand, Clermont-Ferrand"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

在此先感谢您的帮助

您的 getCount() 在您的 adapter 中 returning 0。应该 return:

Locations.size();

修复您的适配器:

    @Override
    public int getCount() {
        return Locations.size();
    }

    @Override
    public Object getItem(int position) {
        return Locations.get(position);
    }    

要了解列表视图并对其进行优化,请阅读 http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/

您的问题的解决方法如下,但您可能还想优化您的 getView() 方法(在下面完成)。

适配器当前 returning getCount() 为 0,这意味着它告诉列表视图没有任何内容可显示。这应该是您列表的大小。

适配器也为 getItem return 设置了 null,因此在您的 getView() 方法中,调用 location.name 时您会得到一个空指针。 getItem() 应该 return Locations.get(position).

    @Override
    public int getCount() {
        return Locations.size();
    }

    @Override
    public Object getItem(int position) {
        return Locations.get(position);
    }   

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;

        if (convertView == null)
            convertView = inflater.inflate(monLayout, parent, false);
            holder = new ViewHolder();
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        Location location = (Location) getItem(position);

        holder.name_view.setText(location.name);
        holder.address_view.setText(location.address);
        holder.date_view.setText("Test");

        return view;
    }