我有一个列表视图,我使用cusome ArrayAdapter填充,列表视图中的每一行都包含图像和标题。添加更多的项目自定义ArrayAdapter
我跟着这个教程http://www.vogella.com/tutorials/AndroidListView/article.html 创建自定义arrayadapter并使用它像填充我的ListView如下:
在这里,我将适配器连接到我的清单在MainActivity:
newsAdapter = new NewsAdapter(getActivity() , titles , images);
listView.setAdapter(newsAdapter);
每个图像和标题是ArrayList对象,我想填充我的列表视图。
这里是我的ArrayAdapter:
public class NewsAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> titles;
private final ArrayList<String> images;
public NewsAdapter(Context context,ArrayList<String> titles, ArrayList<String> images) {
super(context, R.layout.drawer_list_item, titles);
this.context = context;
this.titles = titles;
this.images = images;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.news_list_item, parent, false);
TextView title = (TextView) rowView.findViewById(R.id.news_title);
ImageView imageView = (ImageView) rowView.findViewById(R.id.news_thumble);
title.setText(titles.get(position));
new DownloadImageTask(imageView).execute(images.get(position));
return rowView;
}
}
此代码的工作完美,填入数据(图片和标题)ListView控件。
问题:
是否有可能追加在此实现的列表视图(添加更多行)? (例如使用newsAdapter.add()方法)。
如果这是可能的,如何实现这一目标?
非常感谢你,你是一个救生员。:) – Omar
我遵循你的指示,但它不起作用。谷歌是否改变它在一个新的版本? – Kinimod
afaik此方法仍然有效并且是列表视图的一种好方法。但是现在回收者的观点是 - 根据具体需求 - 在某些情况下可以很好地替代列表视图。 – stamanuel