2013-05-29 27 views
0

嗨!我試圖在listview中添加一些值,並嘗試使用(inc)和(dec)按鈕增加/減少項目的數量。增加/減少listview在notifydatachanged上的選定位置

要增加或減少列表視圖項目數量,我必須選擇列表行,如果我添加任何項目。

如何在notifydatachanged上增加/減少listview選定的位置?
現在我做這樣的:

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, 
     long id) { 
    index = position; 

} 

及彼的選擇指標,並使用該指數值增加或減少項目數量

+0

使用'爲setSelection(INT位置)'http://developer.android.com/ reference/android/widget/ListView.html#setSelection(int) –

+0

@Dev Soman是的我使用自定義適配器來設置列表項目 – Bora

回答

0

ListView與你的適配器提供的數據集填滿。如果你想增加/減少項目的ListView的節目,你必須添加/從這個數據集刪除項目,並調用notifyDatasetChanged()

0
Your List is filled up with some data like this, 

    ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>(); 
       for(int i = 1; i <= list.size(); i++){ 
        HashMap<String, String> map = new HashMap<String, String>(); 
        map.put("rowid", "" + i); 
        map.put("col_1",list.get()); 
        map.put("col_2", map.get(list.get(i-1))); 
        map.put("col_3", "X"); 
        arrayList.add(map); 
       } 

       // fill in the list item layout 
       adapter = new SimpleAdapter(this, fillMaps, android.R.layout.simple_list_item_1, from, to); 
       lv.setAdapter(adapter); 

} 

      buttonAdd.setOnClickListener(new OnClickListener() 
      { 
      public void onClick(View v) 
       { 

       //Here we are adding data to list 
       int size=list.size(); 
       HashMap<String, String> map = new HashMap<String, String>(); 
            map.put("rowid", "" + size); 
            map.put("col_1",list.get(size-1)); 
            map.put("col_2", map.get(list.get(size-1))); 
            map.put("col_3", "X"); 
            arraylist.add(map); 
            adapter.notifyDataSetChanged();//refreshing adapter 
}); 

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 

      public boolean onItemLongClick(AdapterView<?> arg0, View v, 
        int index, long arg3) { 

//Here we are removing data from list 
    listview.remove(index); 
       arrayList.remove(index); 
       adapter.notifyDataSetChanged(); 

}