无法显示在活动列表列表

2023-09-12 22:26:03 作者:讲道理你活在梦里

我想从一个文本文件中读取数据后显示的位置一个ListView,我创建了一个自定义的适配器和一个新的活动卜当我打开活动,我只得到一个白色的屏幕。

下面是我加的适配器的code:

公共类LocationAdapter扩展了BaseAdapter {         私人列表<地点>地点;         INT monLayout;         LayoutInflater充气;         公共LocationAdapter(上下文的背景下,INT布局){             monLayout =布局;             充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);             位置=新的ArrayList<地点>();         }         私有类位置{             公共字符串名称;             公共字符串的地址;             众长日期;             公共场所(字符串_name,字符串_address,龙_date){                 NAME = _name;                 地址= _address;                 日期= _date;             }         }         私有类ViewHolder {             TextView的name_view;             TextView的address_view;             TextView的date_view;             公共ViewHolder(查看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);             }         }         公共无效addLocation(字符串_name,字符串_address,龙_date){             //创建D'UNE新式的位置AVEC LESdonnées连接参数应用             位置new_loc =新的位置(_name,_address,_date);             // Ajout德拉位置点菜清单当然DES位置             Locations.add(new_loc);         }     / *Méthodes德拉CLASSE单纯的* /         @覆盖         公众诠释getCount将(){             返回0;         }         @覆盖         公共对象的getItem(INT位置){             返回null;         }         @覆盖         众长getItemId(INT位置){             返回0;         }         @覆盖         公共查看getView(INT位置,查看convertView,父母的ViewGroup)         {             查看查看= convertView;             如果(查看== NULL)                 鉴于= inflater.inflate(monLayout,父母,假);             ViewHolder支架=(ViewHolder)view.getTag();             如果(持有人== NULL)             {                 持有人=新ViewHolder(视图);                 view.setTag(保持器);             }             位置位置=(地点)的getItem(位置);             holder.name_view.setText(location.name);             holder.address_view.setText(location.address);             holder.date_view.setText(测试);             返回查看;         } }

任务可以暂停吗

下面是我的活动的onCreate方法的code:

公共类SortedLocationsListActivity扩展ListActivity {         @覆盖         保护无效的onCreate(包savedInstanceState){             super.onCreate(savedInstanceState);             //setContentView(R.layout.activity_sorted_locations_list);             InputStream的是= getResources()openRawResource(R.raw.locations)。             的BufferedReader在=新的BufferedReader(新InputStreamReader的(是));             名单<字符串> maListe =新的ArrayList<字符串>();             字符串myligne;             LocationAdapter适配器=新LocationAdapter(这一点,R.layout.row_location);             尝试 {                 而((myligne = in.readLine())!= NULL)                 {                     字符串[] = myligne.split();                     长_date =的Long.parseLong(一个[2],36);                     的System.out.println(一个[0] ++一[1] ++一个[2]);                     adapter.addLocation(一个[0],A [1],_日期);                 }                 setListAdapter(适配器);             }             赶上(IOException异常E)             {                 的System.out.println(错误);             }         }

下面是布局的我想用于列表中的每个元素的XML:

< XML版本=1.0编码=UTF-8? > < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android     机器人:layout_width =FILL_PARENT     机器人:layout_height =50dp     机器人:以下属性来=16DP     机器人:paddingRight =16DP     机器人:paddingBottom会=5DP     机器人:paddingTop =10dp>     < TextView的机器人:ID =@ + ID /名称         机器人:layout_width =WRAP_CONTENT         机器人:layout_height =WRAP_CONTENT         机器人:文本=Faculté德所有权         机器人:TEXTSTYLE =黑体         机器人:文字颜色=#000         机器人:layout_marginBottom =10dp         机器人:layout_alignParentLeft =真/>     < TextView的机器人:ID =@ + ID /日期         机器人:layout_width =WRAP_CONTENT         机器人:layout_height =WRAP_CONTENT         机器人:文本=。2014年11月7日         机器人:layout_alignParentRight =真/>     < TextView的机器人:ID =@ + ID /地址         机器人:layout_width =WRAP_CONTENT         机器人:layout_height =WRAP_CONTENT         机器人:文本=41大道密特朗,克莱蒙费朗         机器人:layout_alignParentBottom =真/> < / RelativeLayout的>

在此先感谢您的帮助

解决方案

解决您的适配器:

  @覆盖
    公众诠释getCount将(){
        返回Locations.size();
    }

    @覆盖
    公共对象的getItem(INT位置){
        返回Locations.get(位置);
    }
 

I'm trying to display a ListView of locations after reading the data from a text file, I have created a custom adapter and a new activity bu when I open the activity, I only get a white screen.

Here is the code of the adapter I added :

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;
        }
}

Here is the code of the Oncreate method of my Activity :

  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");
            }

        }

Here is the XML of the layout I want to use for each element of the list :

<?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>

Thanks in advance for your help

解决方案

Fix your adapter:

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

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