-1

I have to make a custom ArrayAdapter from the JSON response I'm getting. Here is my code from where I'm getting response and putting it in simple ArrayAdapter with android.R.layout.simple_list_item_1

ListView lv = (ListView) findViewById(R.id.list);

    try {

        HttpClient hClient = new DefaultHttpClient();
        HttpGet hGet = new HttpGet(
                "API HERE");
        ResponseHandler<String> rHandler = new BasicResponseHandler();
        data = hClient.execute(hGet, rHandler);

        JSONObject rootObj = new JSONObject(data);
        JSONObject searchObj = rootObj.getJSONObject("searchdata");
        JSONArray titlesObj = searchObj.getJSONArray("titles");
        JSONArray descsObj = searchObj.getJSONArray("desc");
        JSONArray linksObj = searchObj.getJSONArray("links");

        String[] a = new String[titlesObj.length()];

        String[] b = new String[descsObj.length()];

        String[] c = new String[linksObj.length()];

        for (int i = 0; i < titlesObj.length(); i++) {
            String title = titlesObj.getString(i);
            a[i] = title;
        }

        for (int i = 0; i < descsObj.length(); i++) {
            String desc = descsObj.getString(i);
            b[i] = desc;
        }

        for (int i = 0; i < linksObj.length(); i++) {
            String link = linksObj.getString(i);
            c[i] = link;
        }

        ArrayList<String> al = new ArrayList<String>();
        for (int i = 0; i < linksObj.length(); i++)

        {
            al.add(" " + a[i] + " " + b[i] + "" + c[i] + "");
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_list_item_1, al);

        lv.setAdapter(adapter);

    } catch (Exception e) {

    }

Here, is the screenshot of my view:

OUTPUT of my CODE

I want to show the view which has three TextView one for the title, another one for URL and the last one for description.

Any help will be appreciated.

Anupam
  • 3,742
  • 18
  • 55
  • 87

2 Answers2

1

Make a Custom Adapter. Set that adapter to your listview. Your Adapter class should extend ArrayAdapter. In getView() inflate your custom xml with 3 textviews . Use a viewholder for performance.

Hers's an example http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/. Hers's a bit more to listview http://www.youtube.com/watch?v=wDBM6wVEO70.

how to set json parsed data in a listview and then adding search functionality in it. Have a look at the answer. I have used hashmap and displayed data in listview accordingly with search on listview items. Modify the same according to your needs.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • The example of androidhive is using HashMap and the problem with hashmap is that it replaces the old value with the new one and by this example I get only the last value in the listview. Can you tell me how we can recover that problem. – Anupam Feb 28 '13 at 15:32
  • Check the edited answer and the last link answers your question. – Raghunandan Feb 28 '13 at 17:05
0

Anupam, you can place the small data like

   al.add(" " + apple + " " + ball + "" +cat + "");

in single row in single line. If u want to keep that much data(above mentioned in image in one single row) in single row in single line is not possible.

Vinod Kumar
  • 312
  • 4
  • 10