2017-08-04 82 views
0

我有一個用CheckedTextViews填充的Lsitview。我的目標是能夠一次刪除多行,如果可能的話用MultiChoiceModeListener。 要使用它,我必須設置ListView CHOICE_MODE_MULTIPLE_MODAL在ListVIew中刪除多行

現在的第一個問題是:我現在不能僅僅檢查複選框,因爲listview不再是ChOICE_MODE_MULTIPLE了。

我哈德的愛迪爾設置它CHOICE MODE MULTIPLE,只是設置爲ListView的OnItemClickListener,但隨後的ChoiceModeMultiple及其方法onItemStateChaed方法總是被調用,即使我只是想在我的應用程序檢查複選框。

如何解決這個問題的任何解決方案?

守則(佈局列表視圖):

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

    } 

} 
+0

你可以申請一個'onItemClickListener'的檢查和' onItemLongClickListener'用於選擇多行? – deHaar

+0

我試過了,但不幸的是應用程序崩潰了 – Sarius

+0

什麼樣的崩潰?你有沒有例外?你的日誌是怎麼說的? – deHaar

回答

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


}