如何在android中检索listview中的数组
我想在如何在android中检索listview中的数组,android,arrays,listview,Android,Arrays,Listview,我想在ListView中显示人名列表,当我点击姓名时,其相应的地址、电话和区号将显示在另一个活动中,其中有4个TextView来显示所有信息。我的问题是我无法将人名检索到ListView,我的setOnItemClickListener()语句会是什么样子。。。。。3.我找了三天的解决办法,但还不能解决。。提前谢谢 这是我的全部代码 <?xml version="1.0" encoding="utf-8"?> <resources> <string-array
ListView
中显示人名列表,当我点击姓名时,其相应的地址、电话和区号将显示在另一个活动中,其中有4个TextView
来显示所有信息。我的问题是我无法将人名检索到ListView
,我的setOnItemClickListener()
语句会是什么样子。。。。。3.我找了三天的解决办法,但还不能解决。。提前谢谢
这是我的全部代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="person_array">
<item>@array/person_name</item>
<item>@array/phone</item>
<item>@array/address</item>
<item>@array/area_code</item>
</string-array>
<string-array name="person_name">
<item>Max</item>
<item>Heven</item>
<item>Jhon</item>
</string-array>
<string-array name="phone">
<item>123</item>
<item>456</item>
<item>789</item>
</string-array>
<string-array name="address">
<item>XYZ</item>
<item>ABC</item>
<item>MNO</item>
</string-array>
<string-array name="area_code">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
还有我的personList Java文件
public class PersonList extends ArrayAdapter<Person>{
private LayoutInflater mInflater;
public PersonList(Context context, int textViewResourceId,
List<Person> objects) {
super(context, textViewResourceId, objects);
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(
R.layout.person_details_view, parent, false);
}
Person p = getItem(position);
((TextView) convertView.findViewById(R.id.personName)).setText(p.getPersonName());
((TextView) convertView.findViewById(R.id.personPhone)).setText(p.getPersonPhone());
((TextView) convertView.findViewById(R.id.personAddress)).setText(p.getPersonAddress());
((TextView) convertView.findViewById(R.id.personAreaCode)).setText(p.getPersonAreaCode());
return convertView;
}
}
公共类PersonList扩展了ArrayAdapter{
私人停车场;
公共PersonList(上下文,int textViewResourceId,
列出对象){
超级(上下文、textViewResourceId、对象);
mInflater=LayoutInflater.from(上下文);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
convertView=mInflater.充气(
R.layout.person\u details\u view,parent,false);
}
人员p=getItem(职位);
((TextView)convertView.findViewById(R.id.personName)).setText(p.getPersonName());
((TextView)convertView.findviewbyd(R.id.personPhone)).setText(p.getPersonPhone());
((TextView)convertView.findViewById(R.id.PersonalAddress)).setText(p.getPersonalAddress());
((TextView)convertView.findviewbyd(R.id.personAreaCode)).setText(p.getPersonAreaCode());
返回视图;
}
}
先这样做
listOfPerson.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("TEST", "Application started");
Person person = (Person) listOfPerson
.getItemAtPosition(position);
//after getting index of it just putvalue of person in intent and pass to your second activity
}
});
listoperson.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Log.d(“测试”、“应用程序启动”);
个人=(个人)个人列表
.getItemAtPosition(位置);
//在得到它的索引后,只需将person的值放入intent中,并传递给第二个活动
}
});
public class PersonList extends ArrayAdapter<Person>{
private LayoutInflater mInflater;
public PersonList(Context context, int textViewResourceId,
List<Person> objects) {
super(context, textViewResourceId, objects);
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(
R.layout.person_details_view, parent, false);
}
Person p = getItem(position);
((TextView) convertView.findViewById(R.id.personName)).setText(p.getPersonName());
((TextView) convertView.findViewById(R.id.personPhone)).setText(p.getPersonPhone());
((TextView) convertView.findViewById(R.id.personAddress)).setText(p.getPersonAddress());
((TextView) convertView.findViewById(R.id.personAreaCode)).setText(p.getPersonAreaCode());
return convertView;
}
}
listOfPerson.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("TEST", "Application started");
Person person = (Person) listOfPerson
.getItemAtPosition(position);
//after getting index of it just putvalue of person in intent and pass to your second activity
}
});