How to convert JSONArray to ListView?

I have code that does the following -

  • Connecting to a web service via HttpClient to a PHP file
  • Returns result from SQL query
  • Returns jArray format (JSONArray)

for(int i=0; i < jArray.length() ; i++) { json_data = jArray.getJSONObject(i); int id=json_data.getInt("id"); String name=json_data.getString("name"); Log.d(name,"Output"); } 

When I look at LogCat, I see all the "names" of the request, each entry is printed. I just need to hook these results into a ListView. How can i do this?

PS - I do not have a separate class for the ArrayAdapter. Could this be the reason?

+4
source share
4 answers

If you just want to display a list of text elements, you do not need to redefine anything, you can just add all the elements to an arrayList and use arrayAdapter.

Put the list view in your xml called android: list, and then create your arrayAdapter using the textView you want to use.

After that you only need to call setListAdapter (mArrayAdapter) and it should populate your list.

 ArrayList<String> items = new ArrayList<String>(); for(int i=0; i < jArray.length() ; i++) { json_data = jArray.getJSONObject(i); int id=json_data.getInt("id"); String name=json_data.getString("name"); items.add(name); Log.d(name,"Output"); } ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, items)); setListAdapter(mArrayAdapter) 

hope this helps!

+8
source

you can take a look at this webpage that has an example that you can do exactly as you are looking for: http://p-xr.com/android-tutorial-how-to-parse-read-json-data -into-a-android-listview /

Basically, inside the for loop in which you print the names, you should load a list of arrays containing the values ​​that you want to insert into the list later. Note that this example uses the following:

ArrayList< HashMap < String, String > > mylist = new ArrayList < HashMap < String, String > > ();

And you have an integer and a string to add to the structure so that you can simply turn that identifier into a string and solve the problem. After that, you can use the list adapter without having to create a separate class:

ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, new String[] { "name", "id" }, new int[] { R.id.item_title, R.id.item_subtitle });

If the "name" and "id" are the keys on your map for the name and identifier values ​​returned by json and item_title, item_subtitle types to "adapt" the text.

Hopefully I was clear enough, take a look at this example, in any case, quite simply.

+2
source

Put the data in an array and use the ArrayAdapter to bind the data to the list items.

See article on:

http://www.josecgomez.com/2010/05/03/android-putting-custom-objects-in-listview/

0
source

I don’t like reassigning the data, but I already have them in the JSON array so this is my code ... it's simple enough for me .... hope this helps

 package ...; import android.content.Context; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class DataListFragment extends ListFragment{ JSONArray data; public DataListFragment(){ } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Log.i("mycode", "DataListFragment onActivityCreated"); data=((MainActivity)this.getActivity()).data; Log.i("mycode", "data length "+data.toString()); setEmptyText("No Data Here"); final JSONArrayAdapter adapter = new JSONArrayAdapter(this.getActivity(),data); setListAdapter(adapter); // Start out with a progress indicator. //setListShown(false); } @Override public void onListItemClick(ListView l, View v, int position, long id) { // Insert desired behavior here. Log.i("mycode", "Item clicked: " + id); } private class JSONArrayAdapter extends BaseAdapter { JSONArray data; Context context; public JSONArrayAdapter(Context context,JSONArray data) { super(); this.context=context; this.data=data; } @Override public int getCount() { return data.length(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub try { return data.getJSONObject(arg0); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override public long getItemId(int arg0) { return arg0; } @Override public boolean hasStableIds(){ return true; } @Override public boolean isEmpty(){ return data==null || data.length()==0; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.single_item, parent, false); TextView textView1 = (TextView) rowView.findViewById(R.id.firstLine); TextView textView2 = (TextView) rowView.findViewById(R.id.secondLine); //ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); try { JSONObject jo = (JSONObject) data.get(position); textView1.setText(jo.getString("category_title")); textView2.setText(jo.getString("description")); } catch (JSONException e) { e.printStackTrace(); } return rowView; } } } <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:padding="6dip" > <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentBottom="true" android:layout_alignParentTop="true" android:layout_marginRight="6dip" android:contentDescription="icon" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/secondLine" android:layout_width="fill_parent" android:layout_height="26dip" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_toRightOf="@id/icon" android:ellipsize="marquee" android:singleLine="true" android:textSize="12sp" /> <TextView android:id="@+id/firstLine" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/secondLine" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_alignWithParentIfMissing="true" android:layout_toRightOf="@id/icon" android:gravity="center_vertical" android:textSize="16sp" /> </RelativeLayout> 
0
source

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


All Articles