如何检索数组的数组中的Android的ListView [英] How to retrieve array of array in listview in android
问题描述
我要显示在的ListView
人名单,当我点击名称及其相应的地址,电话和面积code将在另一活动中显示那里有4 的TextView
来显示所有的信息。我的问题是我不能找回人的名字的ListView
,什么将是我的 setOnItemClickListener()
语句的样子...... ..我M在提前寻找解决了三天并不能解决它尚未..谢谢。
下面是我的整个code ......
<?XML版本=1.0编码=UTF-8&GT?;
<资源><字符串数组名=person_array>
<项目> @阵列/ PERSON_NAME< /项目>
<项目> @阵列/手机< /项目>
<项目> @阵列/地址< /项目>
<项目> @阵列/ AREA_ code< /项目>
< /字符串数组>
<字符串数组名=PERSON_NAME>
<项目>的Max< /项目>
<项目> Heven< /项目>
<项目>及为Jhon LT; /项目>
< /字符串数组><字符串数组名=手机>
<项目> 123 LT; /项目>
<项目> 456 LT; /项目>
<项目> 789< /项目>
< /字符串数组><字符串数组名=地址>
<项目>&XYZ LT; /项目>
<项目> ABC< /项目>
<项目>&MNO LT; /项目>
< /字符串数组><字符串数组名=AREA_ code>
<项目> 1 LT; /项目>
<项目> 2'; /项目>
<项目>第3版; /项目>
< /字符串数组>
我的主要活动.......
公共类MainActivity扩展ActionBarActivity {ListView的listOfPerson;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); listOfPerson =(ListView控件)findViewById(R.id.personList);
资源解析度= getResources();
ArrayList的<&人GT; extractedData =新的ArrayList<>();
TypedArray TA = res.obtainTypedArray(R.array.person_array);
INT N = ta.length();
的for(int i = 0; I< N ++我){
INT ID = ta.getResourceId(I,0);
如果(ID大于0){
extractedData.add(新的Person(res.getStringArray(ID)));
Log.d(TEST,应用程序开始在if语句);
}其他{
//什么毛病XML,不添加任何东西
}
}
ta.recycle();
PersonList适配器=新PersonList(这一点,0,extractedData);
listOfPerson.setAdapter(适配器);
listOfPerson.setOnItemClickListener(新AdapterView.OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
Log.d(TEST,应用程序启动);} });
}
我一个人的Java文件.....
公共类Person {私人字符串PERSONNAME;
私人字符串personPhone;
私人字符串personAddress;
私人字符串personArea code;公众人物(字符串[] personArray){
this.personName = personArray [0];
this.personPhone = personArray [1];
this.personAddress = personArray [2];
this.personArea code = personArray [3];}公共字符串getPersonName(){
返回PERSONNAME;
}公共字符串getPersonPhone(){
返回personPhone;
}公共字符串getPersonAddress(){
返回personAddress;
}公共字符串getPersonArea code(){
返回personArea code;
}
}
和我personList的Java文件...
公共类PersonList扩展ArrayAdapter<&人GT; {私人LayoutInflater mInflater;公共PersonList(上下文的背景下,INT textViewResourceId,
清单<&人GT;对象){
超(背景下,textViewResourceId,对象);
mInflater = LayoutInflater.from(上下文);
}
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
如果(convertView == NULL){
convertView = mInflater.inflate(
R.layout.person_details_view,父母,假);
}
人P =的getItem(位置);
((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.personArea code))的setText(p.getPersonArea code());
返回convertView;
}
}
首先做这样
listOfPerson.setOnItemClickListener(新AdapterView.OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
Log.d(TEST,应用程序启动);
人人=(人)listOfPerson
.getItemAtPosition(位置);
//得到它的指标只是putvalue的人意图后传递给你的第二个活动 }});
I want to show person name list in ListView
and when I'm clicking on name its corresponding address, phone and area code will be shown in another activity where there are 4 TextView
to show all the info. My problem is I cant retrieve person name to ListView
and what will be my setOnItemClickListener()
statement look like ..... I m searching for the solution for three days and cant solve it yet.. Thanks in Advance.
Here is my whole code......
<?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>
My main Activity.......
public class MainActivity extends ActionBarActivity {
ListView listOfPerson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listOfPerson = (ListView) findViewById(R.id.personList);
Resources res = getResources();
ArrayList<Person> extractedData = new ArrayList<>();
TypedArray ta = res.obtainTypedArray(R.array.person_array);
int n = ta.length();
for (int i = 0; i < n; ++i) {
int id = ta.getResourceId(i, 0);
if (id > 0) {
extractedData.add(new Person(res.getStringArray(id)));
Log.d("TEST", "Application started at if statement");
} else {
// something wrong with the XML, don't add anything
}
}
ta.recycle();
PersonList adapter = new PersonList(this,0,extractedData);
listOfPerson.setAdapter(adapter);
listOfPerson.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("TEST", "Application started");}
});
}
My Person Java file.....
public class Person {
private String personName;
private String personPhone;
private String personAddress;
private String personAreaCode;
public Person(String[] personArray){
this.personName=personArray[0];
this.personPhone=personArray[1];
this.personAddress=personArray[2];
this.personAreaCode=personArray[3];
}
public String getPersonName() {
return personName;
}
public String getPersonPhone() {
return personPhone;
}
public String getPersonAddress() {
return personAddress;
}
public String getPersonAreaCode() {
return personAreaCode;
}
}
And my personList Java file...
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;
}
}
do this way first
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
}
});
这篇关于如何检索数组的数组中的Android的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!