从列表视图中删除所选项目不起作用

时间:2014-11-03 22:53:52

标签: java android android-listview onclick selecteditem

所以我有一个listview并试图能够删除listview中的所选项目。到目前为止,继承我的代码:

public class MainActivity extends Activity {

    Button button1;
    Button button3;
    Button button2;
    Button button4;
    ListView listView1;
    ListView listView2;
    Spinner s1;
    String text;
    Integer i;
    SparseBooleanArray checkedItemPositions ;


    ArrayAdapter<String> adapter;
    ArrayAdapter<String> adapter2;
    int itemPos;
    ArrayList<String> list = new ArrayList<String>();
    ArrayList<String> list2 = new ArrayList<String>();

     @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button1 = (Button) findViewById(R.id.button1);
            button3 = (Button) findViewById(R.id.button3); 
            button4 = (Button) findViewById(R.id.button4); 
            button2 = (Button) findViewById(R.id.button2);
            s1 = (Spinner) findViewById(R.id.spinner1);
            listView1 = (ListView) findViewById(R.id.listView1);
            listView2 = (ListView) findViewById(R.id.listView2);



            adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_single_choice, list);
                listView1.setAdapter(adapter);

                button2.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {

                        String text = s1.getSelectedItem().toString();
                        list.add(text); 
                        adapter.notifyDataSetChanged();



                    }});

                adapter2 = new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_single_choice, list2);
                    listView2.setAdapter(adapter2);

                    button1.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {

                            String text = s1.getSelectedItem().toString();
                            list2.add(text); 
                            adapter2.notifyDataSetChanged();



                        }});

                    button3.setOnClickListener(new View.OnClickListener() {  //part to remove item
                        @Override
                        public void onClick(View v) {
                            /** Getting the checked items from the listview */
                            SparseBooleanArray checkedItemPositions = listView1.getCheckedItemPositions();


                            for(int i=itemPos-1; i >= 0; i--){
                                if(checkedItemPositions.get(i)){
                                    adapter.remove(list.get(i));
                                    adapter.notifyDataSetChanged();
                                }
                            }
                            checkedItemPositions.clear();
                            adapter.notifyDataSetChanged();

                        }
                    });



     listView1.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {

            view.setSelected(true);

        itemPos = position;

      }});
    listView2.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

                view.setSelected(true);

            itemPos = position;

          }
    });}}

在xml中定义的listview如下:

<ListView
    android:id="@+id/listView1"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:layout_alignLeft="@+id/button2"
    android:layout_below="@+id/textView2"
    android:choiceMode="singleChoice"
    android:background="@drawable/bg_key" > >
</ListView>

然而,当我点击删除时,它只是取消标记项目,它根本不会从列表中删除它。我不知道如何解决这个问题。我也是android / java的初学者。

谢谢!

2 个答案:

答案 0 :(得分:1)

您还应该从列表中删除该项目:

adapter.remove(list.get(i));
list.remove(i);
adapter.notifyDataSetChanged();

一般来说,它有效。但是,您可以使用一个技巧来创建适配器并再次添加它。

...
list.remove(i);
//adapter.notifyDataSetChanged();
adapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_list_item_single_choice, list);
listView1.setAdapter(adapter);

答案 1 :(得分:0)

请通过以下链接查看我的答案:

OnItemClickListener and OnClickListener not working for ListView

希望这能解决您的问题