如何在android

时间:2015-07-06 09:50:57

标签: android arrays listview

我想在ListView中显示人名列表,当我点击名称时,相应的地址,电话和区号将显示在另一个活动中,其中有4 TextView来显示所有信息。我的问题是我无法将人名检索到ListView以及我的setOnItemClickListener()语句是什么样的......我正在寻找解决方案三天而且无法解决它...谢谢进展。

这是我的整个代码......

 <?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>

我的主要活动.......

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文件.....

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;
}
}

我的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;
}


}

1 个答案:

答案 0 :(得分:0)

首先这样做

    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

      }

});