【问题标题】:Programmatically Add/Remove items to/from custom listview?以编程方式在自定义列表视图中添加/删除项目?
【发布时间】:2015-04-01 19:52:39
【问题描述】:

我正在创建一个能够添加或删除项目的自定义列表视图。我尝试使用此代码向列表视图添加一个新行项目,但它显示“方法 add(String) 未定义 ListView 类型”。

这是我的代码:

public class AddActivityCustomList extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] web;

    public AddActivityCustomList(Activity context, String[] web) {
        super(context, R.layout.add_activity_single_list, web);
        this.context = context;
        this.web = web;

    }

    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.add_activity_single_list,
                null, true);
        TextView txtTitle = (TextView) rowView
                .findViewById(R.id.act_title_single);

        txtTitle.setTypeface(myface);
        txtTitle.setText(web[position]);

        return rowView;

    }

我的添加按钮代码是这样的:

add_activity.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                lv.add(add_act_title.getText().toString());
                add_act_title.setText("");
                adapter.notifyDataSetChanged();
                add_act_title.getText();
            }
        });

lv.add(...part 有问题。我该怎么办?

【问题讨论】:

    标签: java android listview android-arrayadapter custom-lists


    【解决方案1】:

    列表视图显示来自适配器的数据。通过它的 add 函数将新字符串添加到适配器。

    【讨论】:

    • 更改“lv.add(add_act_title.getText().toString());”到“adapter.add(add_act_title.getText().toString());”
    【解决方案2】:
        add_activity.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                // get the adapter from the listview
                AddActivityCustomList adapter = (AddActivityCustomList) lv.getAdapter();
                // call ArrayAdapter.add, no need to call notifyDataSetChanged as add does this
                adapter.add(add_act_title.getText().toString());
                // clear old title
                add_act_title.setText("");
            }
        });
    

    【讨论】:

    • 这一行仍有错误:adapter.add(add_act_title.getText().toString()); .我想我应该改变我的 AddActivityCustomList 不应该吗?
    【解决方案3】:

    您不能将数据直接添加到列表视图。您需要在适配器中添加数据并调用notifyDataSetChanged()

    使用这个

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        adapter.add(add_act_title.getText().toString());
        adapter.notifyDataSetChanged();
        add_act_title.setText("");
        add_act_title.getText();
    }
    

    【讨论】:

    • 如果您在阵列适配器上调用.add,则无需调用notifyDataSetChanged
    【解决方案4】:

    “未定义 ListView 类型的 add(String) 方法”

    因为你是在 ListView 而不是 Adapter 上调用 add 方法。

    如何在列表视图中添加新的行项

    要从 ListView 添加/删除项目,请使用 ArrayList 作为数据源而不是 Array,因为如果您使用 Array 并调用适配器 add 方法,那么您将获得 UnsupportedOperationException

    所以在 AddActivityCustomList 适配器中进行以下更改:

    1. 使用ArrayList&lt;String&gt; 代替String[] 数组。

    2. 将新项目添加到适配器:

    AddActivityCustomList adapter=listView.getAdapter();
    adapter.add(add_act_title.getText().toString());
    adapter.notifyDataSetChanged();
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    相关资源
    最近更新 更多