Android 带有JSONArray数据的ArrayAdapter不调用getView()
我正在制作一个应用程序,从网络上获取一些数据作为JSON,然后它应该显示在列表视图中 到目前为止,我有一个类可以获取数据并按预期成功运行,然后我有一个自定义ArrayAdapter类。当我在不同的地方使用soem Log.d()运行代码以检查代码是否正常工作时,应用程序将打开,下载JSON,然后将其发送回我的活动,然后将数据成功发送到阵列适配器 (通过调用发送到适配器的数据中第一个JSONObject的字段来测试这一点) 就我所知,问题在于getView()方法没有在应该调用的时候被调用 代码: 阿达帕特:Android 带有JSONArray数据的ArrayAdapter不调用getView(),android,json,Android,Json,我正在制作一个应用程序,从网络上获取一些数据作为JSON,然后它应该显示在列表视图中 到目前为止,我有一个类可以获取数据并按预期成功运行,然后我有一个自定义ArrayAdapter类。当我在不同的地方使用soem Log.d()运行代码以检查代码是否正常工作时,应用程序将打开,下载JSON,然后将其发送回我的活动,然后将数据成功发送到阵列适配器 (通过调用发送到适配器的数据中第一个JSONObject的字段来测试这一点) 就我所知,问题在于getView()方法没有在应该调用的时候被调用 代码:
public class CropListAdapter extends ArrayAdapter<JSONArray> {
Context context;
JSONArray data;
int layoutResourceId;
CropHolder cropHolder;
public CropListAdapter(Context context, int layoutResourceId, JSONArray data) {
super(context, layoutResourceId);
// TODO Auto-generated constructor stub
this.context = context;
this.data = data;
this.layoutResourceId = layoutResourceId;
this.cropHolder = null;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
Log.d("Working", "works here 38");
View row = convertView;
JSONObject crop;
try {
Log.d("Pos", Integer.toString(pos));
crop = (JSONObject) data.getJSONObject(pos);
if (row == null) {
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
cropHolder = new CropHolder();
cropHolder.tvCropId = (TextView) row
.findViewById(R.id.tvCropId);
cropHolder.tvCropName = (TextView) row
.findViewById(R.id.tvCropName);
cropHolder.ivCropImage = (ImageView) row
.findViewById(R.id.ivCropImage);
row.setTag(cropHolder);
} else {
cropHolder = (CropHolder) row.getTag();
}
cropHolder.tvCropId.setText(crop.getString("crop_id"));
cropHolder.tvCropName.setText(crop.getString("name"));
Log.d("Item", "creating item " + crop.getString("name"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return row;
}
static class CropHolder {
TextView tvCropName;
ImageView ivCropImage;
TextView tvCropId;
}
}
公共类CropListAdapter扩展了ArrayAdapter{
语境;
JSONArray数据;
国际布局资源;
CropHolder CropHolder;
公共croplistatadapter(上下文、intlayoutresourceid、JSONArray数据){
超级(上下文、布局资源ID);
//TODO自动生成的构造函数存根
this.context=上下文;
这个数据=数据;
this.layoutResourceId=layoutResourceId;
this.cropHolder=null;
}
@凌驾
公共视图getView(int pos、视图转换视图、视图组父视图){
日志d(“工作”、“在这里工作38”);
视图行=转换视图;
JSONObject作物;
试一试{
Log.d(“Pos”,Integer.toString(Pos));
crop=(JSONObject)data.getJSONObject(pos);
if(行==null){
充气机=((活动)上下文)
.getLayoutInflater();
行=充气机。充气(layoutResourceId,父级,false);
cropHolder=新cropHolder();
cropHolder.tvCropId=(TextView)行
.findviewbyd(R.id.tvCropId);
cropHolder.tvCropName=(文本视图)行
.findviewbyd(R.id.tvCropName);
cropHolder.ivCropImage=(图像视图)行
.findViewById(R.id.ivCropImage);
row.setTag(cropHolder);
}否则{
cropHolder=(cropHolder)row.getTag();
}
cropHolder.tvCropId.setText(crop.getString(“crop_id”));
cropHolder.tvCropName.setText(crop.getString(“name”));
Log.d(“项”,“创建项”+crop.getString(“名称”);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回行;
}
静态级CropHolder{
文本视图tvCropName;
图像视图ivCropImage;
TextView-tvCropId;
}
}
我看不出你在哪里重写了getCount()
方法,你的列表适配器返回0作为列表中的元素数,如果你认为在这种情况下应该重写BaseAdapter
,你需要重写它并返回JSONArray中的元素数。顺便说一下,如果你想调用getView()
,你必须重写getCount()
,让它返回数据。长度()
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutResourceId,null);
只是一个关于实现的建议,如果您曾经将web服务从JSON转换为XML(或其他任何内容),您可能希望将JSON转换为本机对象作为中间数据。就我的2美分。
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutResourceId,null);