如何在android中从多列列表视图中检索特定数据
我在android应用程序中使用多列列表视图。我使用简单适配器在不同的文本视图中存储数据。现在我想从列表视图中检索数据,即每个特定的行和列。我正在动态地向Listview添加行。 我就是这样储存的如何在android中从多列列表视图中检索特定数据,android,android-listview,Android,Android Listview,我在android应用程序中使用多列列表视图。我使用简单适配器在不同的文本视图中存储数据。现在我想从列表视图中检索数据,即每个特定的行和列。我正在动态地向Listview添加行。 我就是这样储存的 ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); HashMap<String, String> m
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
s2=new String[s1.length];
for(int i=0;i<s2.length;i++)
{
map = new HashMap<String, String>();
map.put("name",s1[i][0]);
map.put("salary",s1[i][1]);
map.put("age",s1[i][2]);
map.put("exp",s1[i][3]);
map.put("invest",s1[i][4]);
map.put("gain",s1[i][5]);
mylist.add(map);
}
sd =new SimpleAdapter(getApplicationContext(),mylist,R.layout.activity_listview,new
String[]{"name","iprice","qty","total","lprice","gain"},new int[]
{R.id.t1,R.id.t2,R.id.t3,R.id.t4,R.id.t5,R.id.t6});
}
catch(Exception e)
{
}
自定义XML文件代码:-
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/t1"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:text="text1"/>
<TextView
android:id="@+id/t2"
android:layout_width="30dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="text2" />
<TextView
android:id="@+id/t3"
android:layout_width="20dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="text3" />
<TextView
android:id="@+id/t4"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:text="text4" />
<TextView
android:id="@+id/t5"
android:layout_width="30dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="text5" />
<TextView
android:id="@+id/t6"
android:layout_width="30dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="text6" />
</LinearLayout>
主XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="288dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1" >
</ListView>
</RelativeLayout>
现在单击特定行,我想从listview中检索name和age字段。我该怎么做?先谢谢你
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String name = ((TextView) view.findViewById(R.id.tv1)).getText().toString();
String age = ((TextView) view.findViewById(R.id.tv3)).getText().toString();
TextView tv111 = (TextView) view.findViewById(R.id.tv);
tv111.setText(name);
TextView tv222 = (TextView) view.findViewById(R.id.tw);
tv222.setText(age);
}
});
在main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="text1" />
<TextView
android:id="@+id/tw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_weight="1"
android:text="text3" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" >
</ListView>
</RelativeLayout>
将itemclicklistener添加到listview并获取单击的视图,然后获取其子视图
thelistview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parentvu, View clickedvu, final int ind,long arg3)
{
///
TextView text_view1=(TextView)((View)clickedvu.getElementById(R.id.t1));
TextView text_view2=(TextView)((View)clickedvu.getElementById(R.id.t2));
TextView text_view3=(TextView)((View)clickedvu.getElementById(R.id.t3));
TextView text_view4=(TextView)((View)clickedvu.getElementById(R.id.t4));
TextView text_view5=(TextView)((View)clickedvu.getElementById(R.id.t5));
TextView text_view6=(TextView)((View)clickedvu.getElementById(R.id.t6));
}
});
希望这有帮助
编辑:
clickedvu此处根据文档返回单击/触摸的视图,因此我们应该能够获取其中的元素,您可以从列表中的arraylist获取查看项目单击。如下图所示
String name = mylist.get(position).get("name");
String age = mylist.get(position).get("age");
然后你想在TextView中设置这个字符串,然后你可以使用这个
textName.setText(name);
textAge.setText(age);
不,它不起作用..在我的情况下,一旦onItemClick方法起作用,就会添加onItemClick方法..你能在这里发布你的活动代码吗?我不会向另一个活动发送数据,因为我只想在这个活动中显示它。在这个活动中,你想在点击任何项目时显示数据….在对话框或其他对话框中,getElementById方法是未定义的你查过我的答案了吗?