我是 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