【问题标题】:how to remove checked items/ specific items from an array list / listview如何从数组列表/列表视图中删除选中的项目/特定项目
【发布时间】:2012-03-22 09:10:44
【问题描述】:

我有这段代码,用于将数组列表传递到另一个页面并将其显示为列表视图。当列表出现时,我希望能够检查一个项目并在“按钮单击”时将其删除,这将修改数组。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.oppout);

    final ListView lv2 = (ListView) findViewById (R.id.custom_list_view);
    lv2.setClickable(true);
    lv2.setAdapter(new ArrayAdapter<String>(Oppout.this, 
                                            android.R.layout.simple_list_item_checked,
                                            Entername.playerList));

    lv2.setOnItemClickListener (
        new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView adapterView,
                                    View view,int arg2, long arg3) {
                int selectedPosition = adapterView.getSelectedItemPosition();                                 
                Toast.makeText(getBaseContext(), "mu"+ selectedPosition,  
                           Toast.LENGTH_SHORT).show();                               
            }
    });


    Button next = (Button) findViewById(R.id.button1);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            // on click call the adapterview and delete string at selected position.
            // This is my problem, am not getting how to call the adapter and deleted
            // the selected item/position
            int selectedPosition = adapterView.getSelectedItemPosition();
            adapterView.remove(player.SelectedPosition);

            Intent myIntent = new Intent (view.getContext(), Callacab.class);
            startActivityForResult(myIntent, 0);
        }
    });
}}

【问题讨论】:

  • 我更新了最后一种方法的格式和缩进。
  • 点击标签按钮下方的“编辑”链接,您会看到差异。
  • 好的,你重新格式化了我的代码..谢谢
  • 有没有人有真正的解决方案,不仅仅是重新格式化我的代码..而是解决我的真正问题..?

标签: android arrays listview arraylist android-listview


【解决方案1】:

contacts.remove(index); //你给适配器的数组列表

arrayadapter.remove(index); // 这是你给列表视图的适配器 arrayadapter.notifyDataSetChanged();

//你可以从你的arraylist或你的适配器中删除,然后notifyDataSetChanged();使效果发生在您的列表视图中......最好从 arraylist 中删除,它的索引将是您的列表视图项目侦听器中的 arg2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多