Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate Listview from JSON

Does anyone know of any examples to dynamically load Json data into a ListView, most examples I have seen just use a static array of some kind. I need to load say 10 rows of Json data, then at the bottom have a load more.. to get the next 10 etc etc. Examples using Json please....

like image 853
Ian Avatar asked Jun 08 '11 10:06

Ian


3 Answers

Programmer Bruce is correct, there is no default way to do this. However, there is a very clean and simple way to get this accomplished. Here is the adapter I use to handle JSONArrays.

class JSONAdapter extends BaseAdapter implements ListAdapter {

    private final Activity activity;
    private final JSONArray jsonArray;
    private JSONAdapter(Activity activity, JSONArray jsonArray) {
        assert activity != null;
        assert jsonArray != null;

        this.jsonArray = jsonArray;
        this.activity = activity;
    }


    @Override public int getCount() {

        return jsonArray.length();
    }

    @Override public JSONObject getItem(int position) {

        return jsonArray.optJSONObject(position);
    }

    @Override public long getItemId(int position) {
        JSONObject jsonObject = getItem(position);

        return jsonObject.optLong("id");
    }

    @Override public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = activity.getLayoutInflater().inflate(R.layout.row, null);

        JSONObject jsonObject = getItem(position);  

        return convertView;
    }
}
like image 122
dbaugh Avatar answered Oct 28 '22 11:10

dbaugh


Android doesn't have a ready-made adapter to populate a ListView with a JSON array, like it does for populating a ListView with database records.

I recommend getting comfortable with populating a Java data structure of your choice with the JSON data, and getting comfortable with working with ListViews, populating them from different collections, with different custom rows.

Here's a simple example of populating a List from a JSON array, and then using the List to populate a ListView.

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Main extends Activity
{
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try
    {
      String jsonInput = "[\"one\",\"two\",\"three\",\"four\",\"five\",\"six\",\"seven\",\"eight\",\"nine\",\"ten\"]";
      JSONArray jsonArray = new JSONArray(jsonInput);
      int length = jsonArray.length();
      List<String> listContents = new ArrayList<String>(length);
      for (int i = 0; i < length; i++)
      {
        listContents.add(jsonArray.getString(i));
      }

      ListView myListView = (ListView) findViewById(R.id.my_list);
      myListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listContents));
    }
    catch (Exception e)
    {
      // this is just an example
    }
  }
}
like image 22
Programmer Bruce Avatar answered Oct 28 '22 10:10

Programmer Bruce


dbaugh has a good answer, but this post explains how to use it for the rest of us who don't know where to go from there.

like image 3
Austin Avatar answered Oct 28 '22 09:10

Austin