0

我正在制作一个应用程序,它从网络获取一些数据作为 JSON,然后它应该显示在列表视图中。

到目前为止,我有一个类可以获取数据并按预期成功运行,然后我有一个自定义 ArrayAdapter 类。当我在各个地方使用 soem Log.d() 运行我的代码以检查代码是否正常工作时,应用程序将打开,下载 JSON,然后将其发送回我的活动,然后将数据发送到数组适配器成功

(通过调用发送到适配器的数据中第一个 JSONObject 的字段来测试它)。

据我所知,问题在于 getView() 方法没有被调用。

代码:

try {

        listAdapter = new CropListAdapter(this, R.layout.crop_listitem,
                crops.getJSONArray("crops"));

        listView.setAdapter(listAdapter);

        // setUpViews();

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

适配器:

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;

    }
}
4

4 回答 4

0
  1. 首先,这样做 "crops.getJSONArray("crops")" 不是一个好主意,似乎是,您正在使用同步调用下载 jsonArray。如果它是同步的,那么应用程序将在较新的 android 版本上崩溃。您必须异步执行下载任务,完成后您必须通知适配器数据已更新。它将刷新列表。

  2. 在 GetView 方法中解析 json 也是一个坏主意,如果您没有正确处理异常,它会影响 listview 的平滑滚动并使您的应用程序崩溃。您应该在下载并解析后将您的json数据加载到一些数据结构中。然后您可以在getView中使用它们。

  3. 在这种情况下,您应该通过扩展 BaseAdapter 来制作您的适配器,它更容易和更可定制,这就是为什么在所有情况下都能很好地工作的原因。

希望这可以帮助。

于 2013-10-17T14:35:37.190 回答
0

我看不到您在哪里覆盖了getCount()方法,您的列表适配器返回 0 作为列表中的元素数,您需要覆盖它并返回 JSONArray 中的元素数

于 2013-10-17T14:25:00.897 回答
0

如果认为BaseAdapter 在这种情况下你应该覆盖。顺便说一句,如果你想getView()被调用,你必须覆盖getCount()并让它返回data. length()

于 2013-10-17T14:25:10.420 回答
0

像这样试试

getView()方法中

LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 row = inflater.inflate(layoutResourceId,null);
于 2013-10-17T14:42:13.553 回答