ListAdapter修改的数据源(它是一个ArrayList) [英] ListAdapter to modify the datasource (which is an arraylist)

查看:634
本文介绍了ListAdapter修改的数据源(它是一个ArrayList)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我碰到的最近一个问题:
我有一个自定义适配器类列表视图,适配器需要在ListView并填充从中元素列表视图。现在,我想有一个列表视图中的每一行一个按钮,从中取出物品。我应该如何解决这个问题呢?有没有一种方法来远程触发在活动类中的方法,并调用notifydatachanged()方法在适配器上刷新列表视图?

here's a problem that i've run into lately: I have a listview with a custom adapter class, the adapter takes in a listview and populates the listview with elements from it. Now, i'd like to have a button on each row of a listview to remove the item from it. How should i approach this problem? Is there a way to remotely trigger a method in the activity class and call notifydatachanged() method on the adapter to refresh the listview?

推荐答案

我已经做了这样的事情:

I've done something like that:

public class MyAdapter extends Adapter {

private final ArrayList<String> items = new ArrayList<String>();

// ...

deleteRow(int position) {
    items.remove(position);
    notifyDataSetChanged();
}
//

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView == null) {
        Tag tag = new Tag();
        // inflate as usual, store references to widgets in the tag
        tag.button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
                    deleteRow(position);
        }
    });
    }
    // don't forget to set the actual position for each row
    Tag tag = (Tag)convertView.getTag();
    // ...
    tag.position = position;
    // ...
}



class Tag {

    int position;

    TextView text1;
    // ...
    Button button;
}

}

这篇关于ListAdapter修改的数据源(它是一个ArrayList)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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