android 在列表视图项目上获取不同的ID
我在将正确的 id 作为额外的内容发送到另一个活动时遇到了一些问题。我从数据库中获取 id,并将其发送到新的活动,以便我可以获得正确的数据。但问题是我的代码只发送最后一个 id,它位于 arraylist 上。我该如何解决这个问题?
这是我正在使用的代码:
for(cursorTitle.move(0); cursorTitle.moveToNext(); cursorTitle.isAfterLast()) {
if (vf != null) {
vf.removeAllViews();
}
text = cursorTitle.getString(cursorTitle.getColumnIndex("title"));
cardsCount = cursorTitle.getString(cursorTitle.getColumnIndex("cardsCount"));
collId = Integer.parseInt(cursorTitle.getString(cursorTitle.getColumnIndex("objectId")));
Log.i("CollID","Collection ID : "+collId);
b = BitmapFactory.decodeFile("/sdcard/7073d92dce10884554d7e047f1c51cb6.jpg", null);
array = new ArrayList<Integer>();
array.add(collId);
vf = new ViewFlipper(MyCollectionList.this);
myListView = new ListView(MyCollectionList.this);
hm = new HashMap<String, Object>();
hm.put(IMAGE, b);
hm.put(TITLE, text);
hm.put(CARDS_COUNT, cardsCount +" Stampii");
items.add(hm);
final SimpleAdapter adapter = new SimpleAdapter(MyCollectionList.this, items, R.layout.main_listview,
new String[]{TITLE, CARDS_COUNT, IMAGE}, new int[]{ R.id.main_name, R.id.main_info, R.id.main_img});
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Intent previewMessage = new Intent(getParent(), MyCollectionId.class);
previewMessage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
previewMessage.putExtra("collection_id", array.get(position));
parentActivity.startChildActivity("MyCollectionId", previewMessage);
}
});
}
提前致谢!
I have a little problem sending the right id to another activity as extra.I'm getting id from database which I'm sending to the new activity so I can get the right data. But the problem is that my code is sending only the last id,which is on arraylist. How can I fix that?
Here is the code I'm using :
for(cursorTitle.move(0); cursorTitle.moveToNext(); cursorTitle.isAfterLast()) {
if (vf != null) {
vf.removeAllViews();
}
text = cursorTitle.getString(cursorTitle.getColumnIndex("title"));
cardsCount = cursorTitle.getString(cursorTitle.getColumnIndex("cardsCount"));
collId = Integer.parseInt(cursorTitle.getString(cursorTitle.getColumnIndex("objectId")));
Log.i("CollID","Collection ID : "+collId);
b = BitmapFactory.decodeFile("/sdcard/7073d92dce10884554d7e047f1c51cb6.jpg", null);
array = new ArrayList<Integer>();
array.add(collId);
vf = new ViewFlipper(MyCollectionList.this);
myListView = new ListView(MyCollectionList.this);
hm = new HashMap<String, Object>();
hm.put(IMAGE, b);
hm.put(TITLE, text);
hm.put(CARDS_COUNT, cardsCount +" Stampii");
items.add(hm);
final SimpleAdapter adapter = new SimpleAdapter(MyCollectionList.this, items, R.layout.main_listview,
new String[]{TITLE, CARDS_COUNT, IMAGE}, new int[]{ R.id.main_name, R.id.main_info, R.id.main_img});
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Intent previewMessage = new Intent(getParent(), MyCollectionId.class);
previewMessage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
previewMessage.putExtra("collection_id", array.get(position));
parentActivity.startChildActivity("MyCollectionId", previewMessage);
}
});
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在每次迭代中创建一个新的 ArrayList,然后仅为其分配一个值。我不认为这就是你的意图。您应该在循环之外实例化 ArrayList。您还需要将所有其他一次性初始化代码(ListView、SimpleAdapter 等)移出循环。
You're creating a new ArrayList in each iteration and then assigning only a single value to it. I don't think that is what you intended. You should instantiate the ArrayList outside of your loop. You'll also want to move all of your other one-time initialization code (ListView, SimpleAdapter, etc.) out of the loop.
我得到的是,您需要使用光标中获得的记录填充列表视图。您在 for 循环的每次迭代中填充列表视图,这就是导致问题的原因。我认为,以下内容应该适合您。 (应相应地对变量进行所需的更改。)
尝试以下操作:
What i got is,you need to populate a listview with the records you get in cursor.You are populating your listview in each iteration of for loop and that is what causes problem.i think,following should work for you. (Required changes for varibles should be made accordingly.)
Try this:
将
array = new ArrayList();
放在 for 循环之外。喜欢,
Put
array = new ArrayList<Integer>();
outside of for loop.like,