递增/递减listview在notifydatachanged上的选定位置

时间:2013-05-29 07:14:37

标签: android listview

嗨!我正在尝试在listview中添加一些值,并尝试使用(inc)和(dec)按钮增加/减少项目数量。

要增加或减少列表视图项目数量,我必须选择列表行,如果我在其中添加任何项目。

如何在notifydatachanged上增加/减少listview选择的位置?
现在我这样做:

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

}

在此处获取所选索引并使用此索引值来增加或减少项目数量

2 个答案:

答案 0 :(得分:0)

ListView填充了您提供给适配器的数据集。如果要增加/减少ListView显示的项目,则必须在此数据集中添加/删除项目并调用notifyDatasetChanged()

答案 1 :(得分: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();

}