如何从数组列表/列表视图中删除选中的项目/特定项目

时间:2012-03-04 18:04:49

标签: android arrays listview arraylist android-listview

我有这个代码我习惯将数组列表传递给另一个页面并将其显示为listview。当列表显示时,我希望能够检查一个项目并在“按钮点击”删除它,这将修改数组。

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

1 个答案:

答案 0 :(得分:0)

contacts.remove(索引); //你把它给你的适配器的arraylist

arrayadapter.remove(索引); //这是您将其提供给列表视图的适配器   arrayadapter.notifyDataSetChanged();

//你可以从你的arraylist或你的适配器删除然后notifyDataSetChanged();在listview中实现效果....最好从arraylist中删除它的索引将是listview项侦听器中的arg2