• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps JSON and Arraylist

AlexeyS

Newbie
Mar 16, 2017
20
2
Hello, my app get json file, read two field(name and id) , and add them to the list view(in doinBackground).
In onPostExecute i add the listview to arrayadapter. I present only one of the variables(name). When user click on the name(setOnItemClickListener), i need to get id of the name.

My problem, i don't know how can i present only one field from the listview(and where i should do it, in AssyncTask or in other place) and how can i get id of the name when user click and choose name.

How can i do it? thank you for your help
 
My problem, i don't know how can i present only one field from the listview(and where i should do it, in AssyncTask or in other place) and how can i get id of the name when user click and choose name.

If you wish to store the ID for each Name, a very simple solution is to use a HashMap. Declare a class variable as follows:

Code:
Map<String, String> users = new HashMap<String, String>();

Then in your doInBackground() method, add the user:

Code:
users.put(id, name);

Another solution is to use a 'ViewHolder' class with your ListView. This is actually better, because it optimises the performance of your ListView scrolling

http://stackoverflow.com/questions/24865169/android-listview-using-viewholder

But the best solution would be to use a RecyclerView for your list because this has built-in support for the ViewHolder pattern

https://developer.android.com/training/material/lists-cards.html
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones