2011-03-31 80 views
14

我正在研究一個應用程序,需要從按鈕事件中刪除ListView中的項目。如何動態刪除ListView上的按鈕單擊項目?

我試圖從ArrayList中刪除它,然後創建整個新適配器並再次加載列表,但由於列表很大,它會影響我的應用程序的性能,所以我想知道是否有其他方式可以刪除項目從我的列表動態?

我試過你說的人。

起初我只是試圖刪除一個項目,它工作完美,但隨着我增加選定的項目數量,它開始給我IndexOutOfBoundException。

這裏是我的代碼: -

public void onClick(View view) 
{ 
    SparseBooleanArray checkedPositions = new SparseBooleanArray(); 
    checkedPositions.clear(); 
    checkedPositions = lv.getCheckedItemPositions(); 
    int size = checkedPositions.size(); 
    if(size != 0) 
    { 

     for(int i = 0; i <= size; i++) 
     { 
      if(checkedPositions.valueAt(i)) 
      { 
       list.remove(notes.getItem(checkedPositions.keyAt(i))); 
       notes.notifyDataSetChanged(); 
      } 
     } 
    } 
     else{} 
} 

這裏指出是SimpleAdapter的對象的引用。

回答

43

那麼您只需使用ArrayAdapterremove()方法從列表中刪除所需的項目。

一個可能的方式做到這一點是:

Object toRemove = arrayAdapter.getItem([POSITION]); 
arrayAdapter.remove(toRemove); 

另一種方法是修改ArrayList並在ArrayAdapter調用notifyDataSetChanged()

arrayList.remove([INDEX]); 
arrayAdapter.notifyDataSetChanged(); 
+5

你必須添加arrayAdapter.notifyDataSetChanged() – 2011-03-31 09:30:38

+0

那麼,如果你用'ArrayAdapter'工作直接與你沒有,但如果你一起工作的'ArrayList'你要。 – 2011-03-31 09:41:12

+0

不幸的是我使用SimpleAdapter而不是ArrayadAdapter。那麼是否有任何方法可以像上面提到的那樣從SimpleAdapter中刪除項目?還是我需要使用SimpleAdapter的ArrayAdapter? – Varundroid 2011-03-31 09:46:56

4
adapter.remove(arraylist.get(position)); 
adapter.notifyDataSetChanged(); 

,或者你可以再次調用

setListAdapter(adapter) 
+0

爲什麼他應該在'getView()'方法中做到這一點? – 2011-03-31 09:42:08

+0

(它不是必須的)如果他想刪除位置的物品..或者如果他想靜態刪除他可以在任何地方使用.. – Udaykiran 2011-03-31 09:48:21

+1

我認爲你現在動態和靜態混合起來。 – 2011-03-31 09:56:12

2

對列表中的按鈕,讓它在XML的onclick功能,喜歡拿現在的位置是第一

public void OnClickButton(View V){ 
    final int postion = listView.getPositionForView(V); 
    System.out.println("postion selected is : "+postion); 
Delete(postion); 

} 
public void Delete(int position){ 
    if (adapter.getCount() > 0) { 

     //Log.d("largest no is",""+largestitemno); 
     //deleting the latest added by subtracting one 1 
     comment = (GenrricStoring) adapter.getItem(position); 
     //Log.d("Deleting item is: ",""+comment); 
     dbconnection.deleteComment(comment); 
     List<GenrricStoring> values = dbconnection.getAllComments(); 
     //updating the content on the screen 
     this.adapter = new UserItemAdapter(this, android.R.layout.simple_list_item_1, values); 
     listView.setAdapter(adapter); 
    } 
    else 
    { 
     int duration = Toast.LENGTH_SHORT; 
     //for showing nothing is left in the list 
     Toast toast = Toast.makeText(getApplicationContext(),"Db is empty", duration); 
     toast.setGravity(Gravity.CENTER, 0, 0); 

     toast.show(); 
    } 

} 
5

這爲我工作。希望它能幫助別人。 :)

SimpleAdapter adapter = (SimpleAdapter) getListAdapter(); 
this.resultsList.remove((int) info.id); 
adapter.notifyDataSetChanged(); 
-1
<ImageView 
    android:id="@+id/btnDelete" 
    android:layout_width="35dp" 
    android:layout_height="match_parent" 
    android:layout_alignBottom="@+id/editTipo" 
    android:layout_alignParentRight="true" 
    android:background="@drawable/abc_ic_clear" 
    android:onClick="item_delete_handler"/> 

,創造事件item_delete_handler

0
List<String> entries; 
private ArrayAdapter<String> categoryAdapter; 
//Your list of entries {Example: <"category1","category2","category3">} 
entries = new ArrayList<String>(); 
categoryAdapter = new ArrayAdapter<String>(ViewBeaconsActivity.this, 
            android.R.layout.simple_list_item_1, entries); 
//Remove that specific category from the list 
entries.remove(categoryName); 
//Notify the adapter that your dataset has changed. 
categoryAdapter.notifyDataSetChanged(); 
0

您可以從這樣的列表視圖中刪除項: 或者你可以在你的按鈕事件,項目必須選擇刪除

public class Third extends ListActivity { 
private ArrayAdapter<String> adapter; 
private List<String> liste; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_third); 
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
       "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
       "Linux", "OS/2" }; 
    liste = new ArrayList<String>(); 
    Collections.addAll(liste, values); 
    adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, liste); 
    setListAdapter(adapter); 
} 
@Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
    liste.remove(position); 
    adapter.notifyDataSetChanged(); 
    } 
} 
0

至於你最後一個問題,h用這個簡單的例子說明問題:

假設您的列表包含5個元素:list = [1, 2, 3, 4, 5]以及您要移除的項目列表(即,指數)是indices_to_remove = [0, 2, 4]。在循環的第一次迭代中,您刪除索引0處的項目,因此您的列表變爲list = [2, 3, 4, 5]。在第二次迭代中,您刪除索引2處的項目,因此您的列表將變爲list = [2, 3, 5](如您所見,這將刪除錯誤的元素)。最後,在第三次迭代中,您嘗試刪除索引4處的元素,但該列表只包含三個元素,因此您會遇到越界異常。

既然你看到了什麼問題,希望你能想出一個解決方案。祝你好運!

0
 private List<DataValue> datavalue=new ArrayList<Datavalue>; 

    @Override 
    public View getView(int position, View view, ViewGroup viewGroup) { 

      view.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       datavalue.remove(position); 
       notifyDataSetChanged(); 
      } 
     }); 
    } 
    } 
相關問題