尝试从android中列表视图点击事件中的文本视图中获取值
我有一个点击事件连接到我的列表视图,如图所示。
int[] GenusListIDs = { R.id.txt_ID, R.id.txt_Genus, R.id.txt_Count };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.genusitem, cursor_genuslist, service.GenusListColumns, GenusListIDs);
ListView list_Genus = (ListView)findViewById(R.id.list_Genus);
list_Genus.setAdapter(adapter);
list_Genus.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView parent, View view, int position, long id)
{
try
{
Log.println(1, "ItemClick", view.toString());
TextView tv = (TextView)view;
String genus = (String) tv.getText();
Intent i = new Intent(getBaseContext(), Cat_Species.class);//new Intent(this, Total.class);
/*view
i.putExtra("id", id);*/
startActivity(i);
}
catch(Exception ex)
{
Log.println(1, "item-click-event", ex.getMessage());
}
}
});
我需要根据他们单击的列表项将字符串参数传递给新意图。我想要传递的值位于名为 txt_Genus 的列表项中。如何从列表项中获取该值以传递给意图?请不要关注我的实验,哈哈。
I have a click event hooked up to my listview as shown.
int[] GenusListIDs = { R.id.txt_ID, R.id.txt_Genus, R.id.txt_Count };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.genusitem, cursor_genuslist, service.GenusListColumns, GenusListIDs);
ListView list_Genus = (ListView)findViewById(R.id.list_Genus);
list_Genus.setAdapter(adapter);
list_Genus.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView parent, View view, int position, long id)
{
try
{
Log.println(1, "ItemClick", view.toString());
TextView tv = (TextView)view;
String genus = (String) tv.getText();
Intent i = new Intent(getBaseContext(), Cat_Species.class);//new Intent(this, Total.class);
/*view
i.putExtra("id", id);*/
startActivity(i);
}
catch(Exception ex)
{
Log.println(1, "item-click-event", ex.getMessage());
}
}
});
I need to pass a string param to the new intent based on which listitem they clicked on. The value I want to pass is in the listitem called txt_Genus. How do I get that value out of the listitem to pass to the intent? Don't pay attention to my experiments please haha.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以做到。
然后把它放到intent extras里,我想你已经知道这一点了。
编辑;
在您的新活动中,您将访问意图并获得额外的捆绑包。然后,您可以访问您在先前活动的意图中放置的任何内容,如下所示;
This should do it.
Then put it into the intent extras, I think you already know this bit.
Edit;
In your new activity you would access the intent and get the extras bundle. You can then access anything you placed in the intent on your previous activity like below;