1

Am new to android development. I would like to show custom listview with ArrayAdapter with JSONObject instead of arraylist. I worked but am getting no error and also LogCat prints all key, values but listview contains only one item. Please tell me where I made mistake.

import java.util.Arrays;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Simple Adapter for JSON
 * 
 * Based on {@link ArrayAdapter} and {@link CursorAdapter}
 * 
 * @author dave
 *
 */
@SuppressWarnings("rawtypes")
public class SearchJSONAdapter extends ArrayAdapter {    
    Context context;
    int layoutResourceId;
    JSONObject jsonObj = new JSONObject();
    @SuppressWarnings("unchecked")
    public SearchJSONAdapter(Context context, int layoutResourceId, JSONObject objects) {
        super(context, layoutResourceId, Arrays.asList(objects));
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.jsonObj = objects;
    }   

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(layoutResourceId, parent, false);
        } else {
            view = convertView;
        }           
        JSONObject item = (JSONObject) getItem(position);
        bindView(view, item);
        return view;
    }
    /**
     * a la bindView of {@link CursorAdapter}
     * TODO allow for more complex bindings
     */
    public void bindView(View view, JSONObject json) {
        @SuppressWarnings("unchecked")
        Iterator<String> subIter = json.keys();
        while (subIter.hasNext()) {
            String key = subIter.next();
            JSONObject catObj1;
            final View v = view.findViewById(R.id.Catlink);
            final View u = view.findViewById(R.id.catImg);
            try {
                catObj1 = json.getJSONObject(key);
                String catStr = catObj1.getString("na");
                setViewText((TextView) v, catStr);
                setViewImage((ImageView) u, key);               
                Log.d("SearchJSONAdapter", key+"==>"+catStr);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public void setViewImage(ImageView v, String value) {
        try {
            v.setImageResource(this.context.getResources().getIdentifier("ic_category_"+value, "drawable", this.context.getPackageName()));
        } catch (NumberFormatException nfe) {
            v.setImageURI(Uri.parse(value));
        }
    }
    public void setViewText(TextView v, String text) {
        v.setText(text);
    }   
}

search_category.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <ImageView
        android:id="@+id/catImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/Catlink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:padding="10dp"
        android:text=""
        android:textColor="@android:color/black"
        android:textSize="16sp"
        android:textStyle="bold" >
    </TextView>
</LinearLayout>

My Activity

SearchJSONAdapter searchJsonList = new SearchJSONAdapter(this, R.layout.search_category, mainObj);
listCat.setAdapter(searchJsonList);

Thanks Sakthi Prakash.D

power_scriptor
  • 3,274
  • 1
  • 15
  • 16

2 Answers2

0

change the line ` JSONObject item = (JSONObject) getItem(position); with item = (JSONObject) getItem(position); and define JSONObject item globally

Smogger
  • 553
  • 5
  • 17
  • Hi Shashank Agarwal, Thanks for reply. I changed, but show same one results only. This is sample json object am handling. {"1":{"na":"Category One",},"15":{"na":"Category Two",},"24":{"na":"Category Three",}} – power_scriptor Oct 13 '13 at 12:39
0

I now this is old post, but I want to suggest something.
You have to iterate the JSONList outside of the arrayadapter or maybe inside the constructor, you have to use the add method so the list will know that have more than 1 item.

gmetax
  • 3,853
  • 2
  • 31
  • 45