ArrayAdapter要素数据集数为零集数、为零、要素、数据

2023-09-06 10:43:53 作者:十里桃花

我试图建立与该行的自定义视图列表,每一行都会包含一个图像视图和两个文本的意见。

为了这样做,我伸出一个ArrayAdapter类(称为PostersArrayAdapter),并覆盖了getView()方法,以使数据和行布局之间的正确连接。

然而,当我尝试构建一个PostersArrayAdapter与PosterData类(我的实现),还有一些数据的结果是,适配器是空数组,表示getCount将()返回零和ListView控件是空的。

任何人都可以提出我究竟做错了什么? 我正在依托code,我发现在这里 - http://www.vogella.de/articles/AndroidListView/article.html

非常感谢你!

这里是有关code:(PosterData类只是一类具有两个字符串字段)

 公共类PostersListActivity扩展ListActivity {
最后的私人诠释NUM_OF_PICS = 2;
私人ContentGetter CG;
私人PosterData []海报;
私人的ListView ListView的;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    CG =新ContentGetter(NUM_OF_PICS);
    尝试
    {
        海报= cg.parseIndexFile();
        INT RES = cg.DownloadPosterPics(1);

    }
    赶上(ClientProtocolException E)
    {
        e.printStackTrace();
    }
    赶上(IOException异常E)
    {
        e.printStackTrace();
    }

    //使用我们自己的名单适配器
    ListView的=(的ListView)findViewById(android.R.id.list);
    //listView.setAdapter((ListAdapter)新ArrayAdapter<字符串>(这一点,R.layout.list_item,R.id.title,职称));
    PostersArrayAdapter postersAdapter =新PostersArrayAdapter(本,海报);
    Log.d(PostersListActivity.onCreate(),在适配器的数据集elementes数为+ postersAdapter.getCount());
    listView.setAdapter(postersAdapter);
}


公共类PostersArrayAdapter扩展ArrayAdapter< PosterData> {
    私人最终活动范围内;
    私人最终PosterData []海报;

    公共PostersArrayAdapter(活动方面,PosterData []海报){
        超(背景下,R.layout.list_item);
        this.context =背景;
        this.posters =海报;
    }

    //静态的引用保存到外部类,避免访问
    包含类的//任何成员
    类ViewHolder {
        公共ImageView的标志;
        公共TextView的称号;
        公共TextView的名称;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        // ViewHolder将缓冲评估到该行的各个字段
        // 布局

        ViewHolder持有人;
        //回收现有的视图如果作为参数传递
        //这将节省内存和时间在Android
        //此方法在所有类的基地布局是相同的
        查看rowView = convertView;
        如果(rowView == NULL)
        {
            LayoutInflater充气= context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.list_item,空,真正的);
            持有人=新ViewHolder();
            holder.title =(TextView中)rowView.findViewById(R.id.title);
            holder.names =(TextView中)rowView.findViewById(R.id.names);
            holder.logo =(ImageView的)rowView.findViewById(R.id.logo);
            rowView.setTag(保持器);
        }
        其他
        {
            支架=(ViewHolder)rowView.getTag();
        }

        holder.title.setText(海报[位置] .getTitle());
        holder.names.setText(海报[位置] .getNames());
        holder.logo.setImageResource(R.drawable.icon);

        返回rowView;
    }
}
}
 

下面是列表视图布局我使用的:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    < TextView的机器人:ID =@ + ID / textView1
        机器人:textAppearance =机器人:ATTR / textAppearanceLarge
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =WRAP_CONTENT
        机器人:文本=@字符串/ selectPoster
        机器人:layout_gravity =center_horizo​​ntal>
    < / TextView的>
    < ListView的机器人:ID =@机器人:ID /列表
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =match_parent>
    < / ListView控件>
< / LinearLayout中>
 
应用于Graph SLAM的基于扫描相似度的位姿图构建方法

这里是列表元素的布局我使用的:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>

    <的LinearLayout机器人:ID =@ + ID / linearLayout1
        机器人:layout_height =match_parent
        机器人:layout_width =WRAP_CONTENT>

        < ImageView的机器人:ID =@ + ID /标志
            机器人:SRC =@可绘制/图标
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =22px
            机器人:layout_marginTop =递四方
            机器人:layout_marginRight =递四方
            机器人:layout_marginLeft =递四方>
        < / ImageView的>

        <的LinearLayout机器人:ID =@ + ID / linearLayout2
            机器人:layout_height =match_parent
            机器人:layout_width =FILL_PARENT
            机器人:方向=垂直>
            < TextView的机器人:ID =@ + ID /标题
                机器人:文本=TextView的
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:TEXTSIZE =30像素>
            < / TextView的>
            < TextView的机器人:ID =@ + ID /名称
                机器人:文本=TextView的
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT>
            < / TextView的>
        < / LinearLayout中>

    < / LinearLayout中>
< / LinearLayout中>
 

解决方案

确认 getCount将()正确实施这样:

  @覆盖
     公众诠释getCount将(){
           返回海报!= NULL? posters.length:0;
     }
 

编辑:致电

  ArrayAdapter(上下文的背景下,INT textViewResourceId)
 

构造你不通过你的对象的数组构造函数。

您应该调用

  ArrayAdapter(上下文的背景下,INT textViewResourceId,T []对象)
 

构造函数,也需要为参数的对象的数组。

I'm trying to build a list with a customized view for the rows, each row will consist of an image view and two text views.

in order to do so i extended the ArrayAdapter class (called PostersArrayAdapter) and overridden the getView() method in order to make the right connection between the data and the row layout.

However, when i try to construct a PostersArrayAdapter with an array of PosterData class (my implementation) with some data the result is that the adapter is empty, means getCount() returns zero and the listView is empty.

can anyone suggest what am i doing wrong? i'm relying on a code that i found here - http://www.vogella.de/articles/AndroidListView/article.html

Thank you very much!

here is the relevant code: (PosterData class is just a class with two string fields)

public class PostersListActivity extends ListActivity {
final private int NUM_OF_PICS = 2;
private ContentGetter   cg;
private PosterData[]    posters;
private ListView        listView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    cg = new ContentGetter(NUM_OF_PICS);
    try 
    {
        posters = cg.parseIndexFile();
        int res = cg.DownloadPosterPics(1);

    } 
    catch (ClientProtocolException e) 
    {
        e.printStackTrace();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

    // Use our own list adapter
    listView = (ListView)findViewById(android.R.id.list);
    //listView.setAdapter((ListAdapter) new ArrayAdapter<String>(this,R.layout.list_item,R.id.title,titles));
    PostersArrayAdapter postersAdapter = new PostersArrayAdapter(this, posters);
    Log.d("PostersListActivity.onCreate()","Number of elementes in the data set of the adapter is " + postersAdapter.getCount());
    listView.setAdapter(postersAdapter);
}


public class PostersArrayAdapter extends ArrayAdapter<PosterData> {
    private final Activity context;
    private final PosterData[] posters;

    public PostersArrayAdapter(Activity context, PosterData[] posters) {
        super(context, R.layout.list_item);
        this.context = context;
        this.posters = posters;
    }

    // static to save the reference to the outer class and to avoid access to
    // any members of the containing class
    class ViewHolder {
        public ImageView logo;
        public TextView  title;
        public TextView  names;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // ViewHolder will buffer the assess to the individual fields of the row
        // layout

        ViewHolder holder;
        // Recycle existing view if passed as parameter
        // This will save memory and time on Android
        // This only works if the base layout for all classes are the same
        View rowView = convertView;
        if (rowView == null) 
        {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.list_item, null, true);
            holder = new ViewHolder();
            holder.title = (TextView) rowView.findViewById(R.id.title);
            holder.names = (TextView) rowView.findViewById(R.id.names);
            holder.logo = (ImageView) rowView.findViewById(R.id.logo);
            rowView.setTag(holder);
        } 
        else 
        {
            holder = (ViewHolder) rowView.getTag();
        }

        holder.title.setText(posters[position].getTitle());
        holder.names.setText(posters[position].getNames());
        holder.logo.setImageResource(R.drawable.icon);

        return rowView;
    }
}   
}

Here is the list view layout i'm using:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/textView1" 
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="@string/selectPoster" 
        android:layout_gravity="center_horizontal">
    </TextView>
    <ListView android:id="@android:id/list" 
        android:layout_height="wrap_content"  
        android:layout_width="match_parent">
    </ListView>
</LinearLayout>

And here is the list element layout i'm using:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content">

    <LinearLayout android:id="@+id/linearLayout1"
        android:layout_height="match_parent" 
        android:layout_width="wrap_content" >

        <ImageView android:id="@+id/logo" 
            android:src="@drawable/icon" 
            android:layout_height="wrap_content"            
            android:layout_width="22px"
            android:layout_marginTop="4px" 
            android:layout_marginRight="4px"
            android:layout_marginLeft="4px">
        </ImageView>

        <LinearLayout android:id="@+id/linearLayout2" 
            android:layout_height="match_parent" 
            android:layout_width="fill_parent" 
            android:orientation="vertical">
            <TextView android:id="@+id/title"
                android:text="TextView" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:textSize="30px">
            </TextView>
            <TextView android:id="@+id/names"  
                android:text="TextView"             
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </TextView>             
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

解决方案

Make sure getCount() is properly implemented so :

     @Override
     public int getCount(){
           return posters!=null ? posters.length : 0;
     }

EDIT: by calling the

   ArrayAdapter(Context context, int textViewResourceId)

constructor you are not passing your array of objects to the constructor.

You should be calling the

   ArrayAdapter(Context context, int textViewResourceId, T[] objects)

constructor which also takes as parameter the array of objects.