删除ListVIew

时间:2017-08-04 13:36:49

标签: java android nullpointerexception

我有一个用CheckedTextViews填充的Lsitview。我的目标是能够使用MultiChoiceModeListener删除多个行,如果可能的话。 要使用它,我必须设置ListView CHOICE_MODE_MULTIPLE_MODAL

现在存在问题:我现在不能仅仅查看复选框,因为列表视图不再是ChOICE_MODE_MULTIPLE

我喜欢将其设置为CHOICE MODE MULTIPLE并为ListView设置OnItemClickListener,但随后会调用ChoiceModeMultiple及其方法onItemStateChaed方法,甚至如果我只想检查我的应用程序中的复选框。

有关如何解决此问题的任何解决方案?

代码(布局是ListVIew):

layout.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
    @Override
    public void onItemCheckedStateChanged(android.view.ActionMode actionMode, int i, long l, boolean b) {

        if (!list_items.contains(list.get(i))) {
            count++;
            list_items.add(list.get(i));
            actionMode.setTitle(count + " items selected");
        } else if (list_items.contains(list.get(i))) {
            count--;
            list_items.remove(list.get(i));
            actionMode.setTitle(count + " items selected");
        }

    }

}

1 个答案:

答案 0 :(得分:1)

试试这个例子,请参考这个并解决

  public class MainActivity extends AppCompatActivity {

    ListView list;
    ArrayAdapter<String> arrayAdapter;
    ArrayList<String> arrayList = new ArrayList<>();
    ArrayList<String> arrayList2 = new ArrayList<>();
    int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list = (ListView)findViewById(R.id.listItems);
        arrayList.add("one");
        arrayList.add("two");
        arrayList.add("three");
        arrayList.add("four");
        arrayList.add("five");
        arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_layout,R.id.textView,arrayList);
        list.setAdapter(arrayAdapter);

        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);


        list.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode actionMode, int position, long l, boolean b) {

                count = count+1;
                actionMode.setTitle(count + " items selected");
                arrayList2.add(arrayList.get(position));

            }

            @Override
            public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
                MenuInflater inflater = actionMode.getMenuInflater();
                inflater.inflate(R.menu.list_menu,menu);

                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {

                switch (menuItem.getItemId())
                {

                    case R.id.delete_id:

                        for(String msg : arrayList2) {
                            arrayAdapter.remove(msg);

                        }
                            Toast.makeText(getApplicationContext(),"deleted",Toast.LENGTH_SHORT).show();
                            count=0;
                            actionMode.finish();
                        return true;


                    default:
                        Toast.makeText(getApplicationContext(),"Nothing selected",Toast.LENGTH_SHORT).show();
                        break;
                }

                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode actionMode) {

            }
        });

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                adapterView.setSelected(true);
            }
        });
    }


}