gpt4 book ai didi

android - ArrayAdapter 数据集中的元素数为零

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:33:33 24 4
gpt4 key购买 nike

我正在尝试构建一个包含行的自定义 View 的列表,每行将包含一个 ImageView 和两个 TextView 。

为此,我扩展了 ArrayAdapter 类(称为 PostersArrayAdapter)并覆盖了 getView() 方法,以便在数据和行布局之间建立正确的连接。

但是,当我尝试使用一些数据构造带有 PosterData 类数组(我的实现)的 PostersArrayAdapter 时,结果是适配器为空,这意味着 getCount() 返回零并且 listView 为空。

谁能告诉我我做错了什么?我依赖于我在这里找到的代码 - http://www.vogella.de/articles/AndroidListView/article.html

非常感谢!

这里是相关代码:(PosterData类只是一个有两个字符串字段的类)

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

这是我正在使用的 ListView 布局:

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

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

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

最佳答案

确保 getCount() 已正确实现,因此:

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

编辑:通过调用

   ArrayAdapter(Context context, int textViewResourceId)

构造函数您没有将对象数组传递给构造函数。

你应该调用

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

构造函数也将对象数组作为参数。

关于android - ArrayAdapter 数据集中的元素数为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7483460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com