[英]How to get data from ListView. (My List View has 3 items)
I want retrieve the data from the Listview, where my list is having 4 items like, name, date, sem, category. 我想从Listview检索数据,我的列表中有4个项目,例如名称,日期,sem,类别。
i want to read the text present in the list view and pass it to another activity. 我想阅读列表视图中显示的文本,并将其传递给另一个活动。
If you are retrieving your data from server then you need to get item on item click of ListView. 如果要从服务器检索数据,则需要在ListView的项目单击上获取项目。 Like 喜欢
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name =urarraylist.get(position).get("name");// here you have to pas keyname which is put inyour Hashmap arraylist
String category =urarraylist.get(position).get("category");
String date=urarraylist.get(position).get("date");
String sem=urarraylist.get(position).get("sem");
Intent i = new Intent(getBaseContext(),anotheractivity.class);
i.putExtra("name", name);
i.putExtra("category ", category );
i.putExtra("date", date);
i.putExtra("sem", sem);
startActivity(i);
}
});
Now retrieve this data in your next Activity on onCreate() method. 现在,在onCreate()方法的下一个Activity中检索此数据。 Like 喜欢
Intent n = getIntent();
String name = n.getStringExtra("name");
String category = n.getStringExtra("category ");
String date= n.getStringExtra("date");
String sem= n.getStringExtra("sem");
As it's name suggest, a ListView
doesn't contains data, it only contains Views
. 顾名思义, ListView
不包含数据,仅包含Views
。 These views are often created from an array or a List
via an Adapter
. 这些视图通常是通过Adapter
从数组或List
创建的。 So you have to ask your adapter to retrieve the data. 因此,您必须要求适配器检索数据。
Unfortunately there is no default method for that, so you'll have to create your own getter, or you can also loop on the getItem()
method associated with the getCount()
one on your ListView's adapter. 不幸的是,没有为此的默认方法,因此您必须创建自己的getter,或者也可以在与ListView的适配器上的与getCount()
相关联的getItem()
方法上循环。
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name=(String)parent.getItemAtPosition(position);
Intent i = new Intent(getBaseContext(),anotheractivity.class);
i.putExtra("USERNAME", name);
startActivity(i);
}
});
if you click the listview row the value from corresponding position will be fetched and passed to intent and further to next activity. 如果您单击列表视图行,则将从相应位置获取值,并将其传递给intent并进一步传递给下一个活动。
The magic lies here: 魔术就在这里:
String name=(String)parent.getItemAtPosition(position);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.