简体   繁体   English

使用notifyDataSetChanged动态更新listview不起作用

[英]Dynamically updating listview with notifyDataSetChanged doesn't work

I have a custom JSONAdapter class that I want to update on AsyncTask onPostExecute . 我有一个自定义JSONAdapter类,我想在AsyncTask onPostExecute上进行更新。 However, despite the fact that I call notifyDataSetChanged() and despite the fact that getCount() changes, getView is never called. 但是,尽管事实上我调用了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;
    }
}

Can someone see what I'm doing wrong? 有人可以看到我在做什么吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM