向自定义ArrayAdapter添加更多项目 [英] adding more items to custom ArrayAdapter

查看:168
本文介绍了向自定义ArrayAdapter添加更多项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,我使用cusome ArrayAdapter填充该列表视图,列表视图中的每一行都包含image和title.

I have a listview which i populate using cusome ArrayAdapter , each row in the listview contain image and title .

我遵循了本教程 http://www.vogella.com/tutorials/AndroidListView/article.html 创建自定义arrayadapter并使用它填充我的listview,如下所示:

i followed this tutorial http://www.vogella.com/tutorials/AndroidListView/article.html to create custom arrayadapter and populate my listview using it like the following :

在这里,我将适配器附加到MainActivity的列表中:

here i attach the adapter to my list in MainActivity:

newsAdapter = new NewsAdapter( getActivity() , titles , images );
listView.setAdapter(newsAdapter);

每个图像和标题都是ArrayList对象,我要使用其填充列表视图.

each of images and titles are ArrayList objects which i want to populate my listview with.

这是我的ArrayAdapter:

here is my 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中填充了数据(图像和标题).

this code works perfectly and the listView populated with the data (images and titles).

问题:

是否可以在此实现中附加Listview(添加更多行)? (例如,使用newsAdapter.add()方法).

Is it possible to append the Listview (Add more rows) in this implementation ? (e.g using newsAdapter.add() method) .

如果可能的话,如何实现?

and if this is possible , how to achieve that ?

推荐答案

是的,例如,您可以在自定义适配器中实现addItem(String title, String image)方法. 然后,此方法将传递的值添加到titlesimages列表中.

yeah, you could for example implement an addItem(String title, String image) method in your custom adapter. this method then adds the passed values to your titles and images list.

更改listadapter的内容后,您必须通过在该适配器中调用notifyDataSetChanged()重绘listview来使视图无效.

after changing the content of the listadapter you have to invalidate the view by calling notifyDataSetChanged() in that adapter to redraw the listview.

然后在您的主要活动中调用此方法以将更多项目添加到列表中. (例如newsAdapter.addItem("some Title", imageString);

call this method then in your main activity to add more items to the list. (eg newsAdapter.addItem("some Title", imageString);

这篇关于向自定义ArrayAdapter添加更多项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆