JSON for ListView on Android with Gson

I am trying to cast a json result to a ListView in my android application.

This is my Json:

[ { "Result": "8:30,21", "Meeting": "Antwerp Olympics", "Event": "3000m", "Date": "30/05/2013" }, { "Result": "008,32", "Meeting": "Antwerp Olympics", "Event": "Long Jump", "Date": "30/05/2013" }, { "Result": "6,35", "Meeting": "Antwerp Olympics", "Event": "High Jump", "Date": "30/05/2013" }, { "Result": "5,00", "Meeting": "Antwerp Olympics", "Event": "Discus Throw", "Date": "30/05/2013" } ] 

This is my Android code.

 Gson gson = new Gson(); Result[] res = gson.fromJson(results, Result[].class); ListView lv1 = (ListView) getView().findViewById(R.id.sampleListView); String[] values = new String[] { }; //values ArrayAdapter<String> files = new ArrayAdapter<String>(getActivity(),android.R.layout.list_content, values); lv1.setAdapter(files); 

Getting json result when working with GSON, I did it with simple json. Now I do not know how to implement this JSON result in my list. How to fill in String [] values?

Thank you in advance

+4
source share
3 answers

Create a name assigned to the EventEntity class or your choice

 import com.google.gson.annotations.SerializedName; public class EventEntity{ @SerializedName("Result") public int Result; @SerializedName("Meeting") public String Meeting; @SerializedName("Event") public String Event; @SerializedName("Date") public String Date; public EventEntity() {} } 

and change the lookup list data codes to this

 ListView lv1 = (ListView) getView().findViewById(R.id.sampleListView); Gson gson = new Gson(); List<EventEntity> events = (List<EventEntity>) gson.fromJson(results, new TypeToken<EventEntity>>() {}.getType()); ArrayAdapter<EventEntity> files = new ArrayAdapter<EventEntity>(..........); lv1.setAdapter(files); 

EDIT, you must create a custom adapter class and line layout

 public class MyCustomAdapter extends ArrayAdapter<EventEntity> { private final List<EventEntity> list; private final Activity context; public MyCustomAdapter (Activity context, List<EventEntity> list) { super(context, R.layout.rowlayout, list); this.context = context; this.list = list; } static class ViewHolder { protected TextView eventTitle; protected TextView eventDate; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; LayoutInflater inflator = context.getLayoutInflater(); view = inflator.inflate(R.layout.rowlayout, null); final ViewHolder viewHolder = new ViewHolder(); viewHolder.eventTitle = (TextView) view.findViewById(R.id.label); viewHolder.eventDate = (TextView) view.findViewById(R.id.date); view.setTag(viewHolder); ViewHolder holder = (ViewHolder) view.getTag(); holder.eventTitle.setText(list.get(position).Event); holder.eventDate.setText(list.get(position).Date); return view; } 
+7
source

You can use your own class in the ArrayAdapter. Like ArrayAdapter<Result[]>

I suggest you use a class that extends ArrayAdapter<Result[]>

And override the getView() method.

Use this method in the getView() method: http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder (not necessary for you, but for better performance.)

0
source

Here is an example when I call webservice and return a result, which is a list of objects. Then I parse the JSON objects and put them in the list of hashmap elements. The second piece of code is where I am typing a list of hashmap elements for the actual list. It uses a custom layout for list items.

List> values ​​= new ArrayList> ();
result = clsGlobals.PerformGetFromWeb (appcontext, client);

  if (result != "") { try { JSONArray menuItems = new JSONArray(result); for (int i = 0; i < menuItems.length(); i++) { HashMap<String, String> map = new HashMap<String, String>(); JSONObject jsonObject = menuItems.getJSONObject(i); map.put("MenuItemID", jsonObject.getString("MenuItemID")); map.put("MenuItemName", jsonObject.getString("MenuItemName")); map.put("MenuItemDesc", jsonObject.getString("MenuItemDesc")); map.put("Calories", jsonObject.getString("Calories")); map.put("Protein", jsonObject.getString("Protein")); map.put("Fat", jsonObject.getString("Fat")); map.put("Carbohydrates", jsonObject.getString("Carbohydrates")); map.put("Sodium", jsonObject.getString("Sodium")); map.put("FranchiseID", jsonObject.getString("FranchiseID")); values.add(map); } Log.d(clsGlobals.AppName, "Location menu items retrieved successfully!"); } if (values.size() > 0) { //ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyAppDateActivity.this, R.layout.listitemlayout, values); String[] colsNames = new String[] {"MenuItemName", "MenuItemDesc"}; int[] resItemsToAssign = new int[] {R.id.lstName, R.id.lstDesc}; SimpleAdapter sa = new SimpleAdapter(appContext, values, R.layout.listitemlayout, colsNames, resItemsToAssign ); listViewMenu.setAdapter(sa); } 
0
source

Source: https://habr.com/ru/post/1482017/


All Articles