ListAdapter修改数据源(这是一个arraylist)

发布于 2024-08-30 23:23:21 字数 160 浏览 3 评论 0原文

这是我最近遇到的一个问题: 我有一个带有自定义适配器类的列表视图,适配器接受列表视图并用其中的元素填充列表视图。现在,我想在列表视图的每一行上有一个按钮来从中删除项目。我应该如何解决这个问题?有没有办法远程触发活动类中的方法并调用适配器上的notifydatachanged()方法来刷新listview?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

决绝 2024-09-06 23:23:21

我做过类似的事情:

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

}

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

}
枕梦 2024-09-06 23:23:21

在 getView() 方法中,不能只在按钮上 setOnClickListener() 吗?

类似这样:

static final class MyAdapter extends BaseAdapter {

    /** override other methods here */

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            // inflate the view for row from xml file

            // keep a reference to each widget on the row.
            // here I only care about the button
            holder = new ViewHolder();
            holder.mButton = (Button)convertView.findViewById(R.id.button);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        // redefine the action for the button corresponding to the row
        holder.mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something depending on position
                performSomeAction(position);
                // mark data as changed
                MyAdapter.this.notifyDatasetChanged();
            }
        }
    }
    static final class ViewHolder {
        // references to widgets
        Button mButton;
    }
}

如果您不确定是否扩展 BaseAdapter,请查看 ApiDemos 中的示例 List14。该技术为您提供了一种灵活的方法来修改适配器的几乎任何方面,尽管这需要相当多的工作。

In the getView() method, can't you just setOnClickListener() on the button?

Something like this:

static final class MyAdapter extends BaseAdapter {

    /** override other methods here */

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            // inflate the view for row from xml file

            // keep a reference to each widget on the row.
            // here I only care about the button
            holder = new ViewHolder();
            holder.mButton = (Button)convertView.findViewById(R.id.button);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        // redefine the action for the button corresponding to the row
        holder.mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something depending on position
                performSomeAction(position);
                // mark data as changed
                MyAdapter.this.notifyDatasetChanged();
            }
        }
    }
    static final class ViewHolder {
        // references to widgets
        Button mButton;
    }
}

If you're unsure about extending BaseAdapter, check out example List14 in ApiDemos. This techniques provides you with a flexible way to modify just about any aspect of your adapter, though it's quite some work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文