2014-06-18 237 views
0

我有我在Fragment使用數組字符串,我會讓我的列表與setListAdapter數組字符串項:刪除選擇列表項

public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
String[] array = getResources().getStringArray(R.array.examlearray); 
     final ArrayList<String> str = new ArrayList<String>(Arrays.asList(array)); 
     final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, str); 
     setListAdapter(arrayAdapter); 

    final ListView listView = getListView();  
     listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 
     listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {... 

,並在我的onActionItemClicked我要實現我的deleteSelectedItem ()方法,即刪除選定的列表項目,這是我的代碼,但它並沒有刪除選定的項目,它只是從列表中刪除,當我選擇所有項目,然後按刪除,應用程序崩潰!應該做什麼?,任何幫助將不勝感激!謝謝!

public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
       // Respond to clicks on the actions in the CAB 
       switch (item.getItemId()) { 
        case R.id.delete: 
        // deleteSelectedItems(); 

         Log.i(TAG, "deleteSelectedEntries"); 
         SparseBooleanArray checkedItems = listView.getCheckedItemPositions(); 
for(int i=0;i<checkedItems.size();++i) 

         { if(checkedItems.valueAt(i)) 
          str.remove(i); 

         } 
         arrayAdapter.notifyDataSetChanged(); 
+0

訪問該答案並使自定義listview http://stackoverflow.com/questions/23485986/custom-adapter-for-a-list-of-items-that-have-multiple-child-items/23486051#23486051 –

回答

1

而是通過SparseBooleanArray你listView.getCheckedItemPositions()獲得循環的,你通過一個ArrayAdapter的項目有循環,然後檢查是否SparseBooleanArray此項目返回true 。這是因爲當你從ArrayAdapter中刪除項目時,listView.getCheckedItemPositions()仍然返回不存在的項目。

所以你的情況:

SparseBooleanArray checkedItems = listView.getCheckedItemPositions(); 
// Loop backwards, so you can remove the items from within the loop 
for (int i = arrayAdapter.getCount() - 1; i >= 0; i--) { 
    if (checkedItems.get(i)) { 
     // This item is checked and can be removed 
     arrayAdapter.remove(arrayAdapter.getItem(i)); 
    } 
} 

您的應用程序崩潰的原因是因爲你試圖刪除不存在的項目。

+0

感謝它爲我工作;) –

0

您需要持有對陣列適配器的引用。然後嘗試撥打適配器上的remove功能,然後撥打notifyDataSetChanged。如果您只提及該職位,則還需要使用getItem

+0

PLZ看看我的代碼,我應該如何使用'getItem'? –

0

保留用於創建適配器的適配器和數據列表的引用,它更好地使用ArrayList,因爲它可以靈活地輕鬆移除元素。要將適配器設置,你可以使用下面的代碼 -

String[] array = getResources().getStringArray(R.array.examlearray); 
ArrayList<String> str = new ArrayList<String>(Arrays.asList(array)); 
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, str); 
menuListView.setAdapter(arrayAdapter); 

要刪除元素首先從數據列表中刪除的元素,然後調用notifyDatasetChnaged將更新ListView的爲好。您可以撥打下面的代碼裏面onActionItemClicked

`for(int i=0;i<checkedItems.size();++i) 
     { if(checkedItems.valueAt(i)) 
       str.remove(i); 
     } 

arrayAdapter.notifyDataSetChanged(); 
+0

坦克,適配器工作正常,但我刪除沒有工作,這是我的代碼:SparseBooleanArray checkedItems = listView.getCheckedItemPositions(); 對(INT I = 0; I

+0

您的'SparseBooleanArray'將包含對應於每個位置的布爾值數組,如果它是真實的項目被選中,否則它被取消選中。因此,在檢查物品時,請取出物品,取出物品後,您必須通過該物品。我現在編輯了答案檢查。 – Pr38y

+0

如果我選擇3個項目,它會刪除3個第一項!,而不是選定的項目,如果我選擇所有項目按刪除,應用程序將崩潰,我該怎麼辦? –