找到你要的答案

Q:Android custom listview with ArrayAdapter with JSONObject

Q:Android自定义listview ArrayAdapter JSONObject

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

新的Android开发。我想显示自定义ListView代替ArrayList JSONObject ArrayAdapter。我的工作但我没有错误和logcat打印所有关键值但ListView只包含一个项目。请告诉我哪里犯了错误。

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>

我的活动

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

Thanks Sakthi Prakash.D

answer1: 回答1:

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

改线` JSONObject项=(JSONObject)getitem(位置);项目=(JSONObject)getitem(位置);定义JSONObject项全局

answer2: 回答2:

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.

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.

android  android-listview  adapter  android-arrayadapter  jsonobject