使用ArrayAdapters时,单击按钮时将ListView choiceMode从singleChoice更改为multipleChoiceModal

时间:2018-06-23 05:39:59

标签: java android

实际上,我想实现一些类似于我们在手机中经常看到的用于删除聊天/ msgs的功能。单击菜单上的删除按钮时,必须显示复选框以进行行选择,同时在顶部显示不显示标签。选定的行。选择否后,单击右上方的“完成/删除”按钮。行中,必须出现一个删除确认对话框,并且必须删除选定的行。我在下面的图像中突出显示了所需的功能。 [菜单中的图像1按钮] [1] [单击删除按钮时图像2出现多模式] [2] [图像3选择和标签] [3] [图像4确认对话框] [4]到目前为止,这是我编写的代码,但是我的代码无法正常工作,需要帮​​助。这是我的代码:

//for the start I want the list be in single mode

        ListView listView = (ListView) findViewById(R.id.listView);
        ArrayAdapter arrList = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_single_choice, namesList);
        listView.setAdapter(arrList);

 // for the list to be in multimode on clicking the button in the menu

        public boolean onCreateOptionsMenu(Menu menu) 
        {
        getMenuInflater().inflate(R.menu.mymenu, menu);
        return true;
        }  
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();


        if (id == R.id.deletebutton) {
        if (namesList.size() != 0) {
                if (mListView.getChoiceMode() == ListView.CHOICE_MODE_SINGLE) {
                    mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                    directoryList = new ArrayAdapter<String>(this,
                            android.R.layout.simple_list_item_multiple_choice, 
                    namesList);
                    delete();
                }


            } else {
                Toast.makeText(getApplicationContext(), "names not found", 
                      Toast.LENGTH_SHORT).show();
            }

        }

        return super.onOptionsItemSelected(item);
    }
// delete method implementation
        AlertDialog.Builder a_builder = new AlertDialog.Builder(this);
                a_builder.setTitle("Confirm Delete?");
                a_builder.setMessage("DELETE")
                        .setCancelable(true)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                int i;
                                SparseBooleanArray checkedItemPositions = mListView.getCheckedItemPositions();
                                int itemCount = listView.getCount();
                                for (i = itemCount - 1; i >= 0; i--) {
                                    if (checkedItemPositions.get(i)) {
                                        arrList.remove(namesList.get(i));
                                    }
                                }
                                checkedItemPositions.clear();
                                arrList.notifyDataSetChanged();

                                    Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
                                   .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });





  [1]: https://i.stack.imgur.com/ZR1Wv.png
  [2]: https://i.stack.imgur.com/4gJGd.png
  [3]: https://i.stack.imgur.com/uvn9p.png
  [4]: https://i.stack.imgur.com/3gsML.png

0 个答案:

没有答案