使用notifyDataSetChanged动态更新列表视图不起作用

时间:2013-12-24 23:55:08

标签: android listview

我想要在JSONAdapter AsyncTask上更新自定义onPostExecute课程。但是,尽管我致电notifyDataSetChanged(),尽管getCount()发生了变化,但getView永远不会被调用。

private class JSONAdapter extends BaseAdapter implements ListAdapter {
    private final Activity activity;
    private JSONAdapter(Activity activity) {
        this.activity = activity;
    }

    @Override public int getCount() {
        if (result == null) {
            return 0;
        } else {
            return result.length();
        }
    }

    @Override public JSONObject getItem(int position) {
        return result.optJSONObject(position);
    }

    @Override public long getItemId(int position) {
        JSONObject jsonObject = getItem(position);
        return jsonObject.optLong("id");
    }

    @Override public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_item, parent, false);
        TextView textView1 = (TextView) rowView.findViewById(R.id.firstLine);
        TextView textView2 = (TextView) rowView.findViewById(R.id.secondLine);
        try {
            textView1.setText(result.getJSONObject(position).getString("image"));
            textView2.setText(result.getJSONObject(position).getString("distance"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return rowView;
    }
}

有人能看出我做错了吗?

1 个答案:

答案 0 :(得分:0)

我没弄清楚为什么notifyDataSetChanged()无法正常工作,但我设法通过每次重新创建和重置适配器来解决问题。