2013-10-11 3 views
1

어떻게 아래 코드를 사용하여 목록보기에서 여러 행을 삭제하고 MultiChoiceModeListener를 사용하고 있습니까?Android ListView에서 여러 항목 삭제

MainActivity

list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 
     list.setMultiChoiceModeListener(new MultiChoiceModeListener() { 

      @Override 
      public void onItemCheckedStateChanged(ActionMode mode, 
        int position, long id, boolean checked) { 
       // Here you can do something when items are selected/de-selected 
       final int checkedCount = list.getCheckedItemCount(); 
       // Update the title in the CAB 
       mode.setTitle(checkedCount + " Selected"); 

      } 
    @Override 
      public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
       switch (item.getItemId()) { 
       case R.id.menu_clear: 
        deleteSelectedItems(item.getItemId()); 
        mode.finish(); 
        return true; 
       default: 
        return false; 
       } 
      } 
      public void deleteSelectedItems(int id) { 
       adapter.remove(adapter.getItem(id)); //The method remove(Object) is undefined for the type ListViewAdapter 
      } 

ListViewAdapter

public class ListViewAdapter extends BaseAdapter { 

    // Declare Variables 
    Context context; 
    String[] rank; 
    String[] country; 
    String[] population; 
    LayoutInflater inflater; 
    View itemView; 

    public ListViewAdapter(Context context, String[] rank, String[] country, 
      String[] population) { 
     this.context = context; 
     this.rank = rank; 
     this.country = country; 
     this.population = population; 
    } 

    @Override 
    public int getCount() { 
     return rank.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     // Declare Variables 
     TextView txtrank; 
     TextView txtcountry; 
     TextView txtpopulation; 

     inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     itemView = inflater.inflate(R.layout.listview_item, parent, false); 


     txtrank = (TextView) itemView.findViewById(R.id.rank); 
     txtcountry = (TextView) itemView.findViewById(R.id.country); 
     txtpopulation = (TextView) itemView.findViewById(R.id.population); 


     txtrank.setText(rank[position]); 
     txtcountry.setText(country[position]); 
     txtpopulation.setText(population[position]); 

     return itemView; 
    } 

에서

@Override 
      public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
       switch (item.getItemId()) { 
       case R.id.menu_clear: 

        int count = adapter.getCount(); 
        for (int i = 0; i < count; i++) { 
        adapter.remove(adapter.getItem(i)); 
        } 
        adapter.notifyDataSetChanged();//The method remove(Object) is undefined for the type ListViewAdapter 
        mode.finish(); 
        return true; 
       default: 
        return false; 
       } 
      } 
+0

무엇이 있습니까? – CommonsWare

+0

나는 listview에서 여러 행을 삭제하려고합니다. –

+1

그건 질문이 아닙니다. – CommonsWare

답변

0

편집하여 onItemCheckedStateChanged 당신이 목록에서 (체크 된 모든 위치 또는 ID의 추적을 유지하거나 어떤 것).

특정 메뉴 옵션을 클릭하면 모든 위치 또는 ID를 반복하여 데이터 세트에서 삭제합니다.

을 삭제 한 후 어댑터에 notifyDatasetChanged을 호출하십시오.

+0

그래서 모든것이 deleteSelectedItems (int id) 클래스 내부에서 처리된다는 것을 의미합니까? –

+0

그게 아니라 클래스 thats 메서드를 호출하지만 당신은 거기에 그것을 할 필요 없어. 'onActionItemClicked'에서 for 루프를 만들고 id의 목록을 반복하고 그 메소드에 id를 전달한 다음 데이터 세트에서 항목을 제거하십시오. – tyczj

+0

이 오류가 발생했습니다 remove (Object) 메서드는 ListViewAdapter 유형에 대해 정의되지 않았습니다. 나는 무엇을해야합니까? 도와주세요 –

관련 문제