【问题标题】:Android custom listview with ArrayAdapter with JSONObject带有 ArrayAdapter 和 JSONObject 的 Android 自定义列表视图
【发布时间】:2013-10-12 10:20:29
【问题描述】:

我是 android 开发的新手。我想使用带有 JSONObject 而不是 arraylist 的 ArrayAdapter 显示自定义列表视图。我工作但没有收到任何错误,LogCat 也打印所有键、值,但列表视图仅包含一项。请告诉我哪里出错了。

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);

谢谢 Sakthi Prakash.D

【问题讨论】:

  • 你的 logcat 错误在哪里?
  • 嗨,格里斯胡。谢谢你的答复。 LogCat 中没有显示错误或异常。

标签: android android-listview adapter android-arrayadapter jsonobject


【解决方案1】:

` JSONObject item = (JSONObject) getItem(position); 行更改为item = (JSONObject) getItem(position); 并全局定义JSONObject 项

【讨论】:

  • 您好,Shashank Agarwal,感谢您的回复。我改变了,但只显示相同的一个结果。这是正在处理的示例 json 对象。 {"1":{"na":"第一类",},"15":{"na":"第二类",},"24":{"na":"第三类",}}
【解决方案2】:

我现在这是旧帖子,但我想提出一些建议。
您必须在 arrayadapter 之外或在构造函数内部迭代 JSONList,您必须使用 add 方法,以便列表知道有超过 1 个项目。

【讨论】:

    猜你喜欢
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    相关资源
    最近更新 更多