How to programmatically reference multiple string-array by clicking on list item

对着背影说爱祢 提交于 2019-12-12 07:04:47

问题


I am new to android programming and wanna know this,

I have four string-array defined in string.xml file

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

I am using listview to show person_name string-array.

Now I want, when I am clicking on person name , I want to show there corresponding phone number, email, address and area code using textview in another activity.


回答1:


Since we are talking about data which has a relation, you probably want to use a relational data structure like a relational database. In Android, you can use SQLite.

If you do not expect your data to grow (it will always be the same persons), you could also JSON encode the values and pass the whole string to the next Activity

<resources>
<string-array name="person">
    <item>{"name": "Max", "phone":"123", "address":"XYZ", "areaCode": "1"}</item>
</string-array>
</resources>



回答2:


First of all you should really use a database to store such data.

Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

Then get the element number the user clicked on and access this position in planets.

See this tutorial.




回答3:


You can follow some simple steps :

1) Take position of "person_name" when it's click.

2) Pass that position to second activity using intent.

3) Get Values of other string arrays from that position.



来源:https://stackoverflow.com/questions/31205784/how-to-programmatically-reference-multiple-string-array-by-clicking-on-list-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!