嘿伙计们我正致力于在listview
上显示项目列表,我正在使用的代码是
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
ListView lv= (ListView)findViewById(R.id.listview);
rtrnList = new ArrayList<HashMap<String,String>>();
getmyLocation();
listclass = new listClass(offersobj);
listclass.populate();
rtrnList = listclass.getListArray();
adapter = new SimpleAdapter(
this,
rtrnList,
R.layout.custom_row_view,
new String[] {"Name","Msg","time"},
new int[] {R.id.text1,R.id.text2, R.id.text3}
);
lv.setAdapter(adapter);
}
问题是说我正在展示三个名字Avinash,Arun,Rajesh。应用程序启动时,这三个名称将显示在列表中。当我关闭并再次启动应用程序时,值重复Avinash,Arun,Rajesh,Avinash,Arun,Rajesh。我无法弄清楚如何解决这个问题。
答案 0 :(得分:1)
你展示的代码似乎很好。我的猜测是listclass.populate()
修改了offersobj
,而offersobj
会在您的活动的多个创作中重复使用。因此,无论何时创建活动,都会填充其他数据。
答案 1 :(得分:0)
public class ListViewA extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv= (ListView)findViewById(R.id.listview);
// create the grid item mapping
String[] from = new String[] {"rowid", "col_1", "col_2", "col_3"};
int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4 };
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 10; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", "" + i);
map.put("col_1", "col_1_item_" + i);
map.put("col_2", "col_2_item_" + i);
map.put("col_3", "col_3_item_" + i);
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);
}
}
更多示例see this link
this also
listview about adapter, for create a hashmap, why bitmap type imported cannot show image in listview? What adapter shall I use to use HashMap in a ListView