Android ArrayAdapter 不更新 ListView

Android ArrayAdapter does not update ListView

我已经为列表视图编写了一个小实现。 因此,我为列表行和对象扩展了 ArrayAdapter class。

public class MainActivity extends Activity implements NetplugListener {

    public static final int REFRESH_ID = 1;

    ListView listView;
    ArrayList<NetPlug> netplugs;
    NetplugArrayAdapter adapter;
    RemoteDataManager remoteManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        netplugs = new ArrayList<>();
        adapter = new NetplugArrayAdapter(this, netplugs);

        listView = new ListView(this);
        listView.setAdapter(adapter);
        setContentView(listView);

        remoteManager = new RemoteDataManager(this);
        remoteManager.registerNetplugListener(this);

    }

    @Override
    protected void onStart() {
        super.onStart();
        remoteManager.refresh();
    }



    @Override
    protected void onResume() {
        super.onResume();
        remoteManager.refresh();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        menu.add(0, REFRESH_ID, 0, "refresh");
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case REFRESH_ID:
            remoteManager.refresh();
            break;
        }
        return true;

    }

    @Override
    public void onNewNetplugs(ArrayList<NetPlug> netplugs) {
        adapter.addAll(netplugs);
        adapter.notifyDataSetChanged();
    }

}

这里是 ArrayAdapter

public class NetplugArrayAdapter extends ArrayAdapter<NetPlug> {

    Context context;
    ArrayList<NetPlug> netplugs;
    Map<Integer,ViewHolder> viewholders;

    public NetplugArrayAdapter(Context context, ArrayList<NetPlug> netplugs) {
        super(context, R.layout.netplug_item);
        this.context = context;
        this.netplugs = netplugs;
        this.viewholders = new HashMap<>();
    }

    @Override
    public void add(NetPlug object) {
        this.netplugs.add(object);
        notifyDataSetChanged();
    }

    @Override
    public void addAll(Collection<? extends NetPlug> collection) {
        this.netplugs.addAll(collection);
        notifyDataSetChanged();
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        NetPlug n = netplugs.get(position);

        ViewHolder viewHolder;
        if(viewholders.get(position) != null) {

            viewHolder = viewholders.get(position);

        } else {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.netplug_item, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.labelView = (TextView) convertView.findViewById(R.id.label);
            viewHolder.valueView = (TextView) convertView.findViewById(R.id.value);

            viewholders.put(position, viewHolder);

        }

        viewHolder.labelView.setText(n.getName());
        viewHolder.valueView.setText(n.getIp());

        return convertView;

    }

    private static class ViewHolder {

        public TextView labelView;
        public TextView valueView;

    }

}

还有用于填充数据的 RemoteManager

public class RemoteDataManager {

    RequestQueue queue;
    List<NetplugListener> listeners = new ArrayList<>();

    public RemoteDataManager(Context context) {
        queue = Volley.newRequestQueue(context);
    }

    public void refresh() {

        try {

            JSONObject json = new JSONObject();
            json.put("command", 0b1000);

            sendJson("http://192.168.0.120:7000", json);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public void sendJson(String url, JSONObject json) {

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
                url, json, new ResponseListener(), new ResponseErrorListener());

        queue.add(request);

    }

    public void registerNetplugListener(NetplugListener listener) {
        this.listeners.add(listener);
    }

    private class ResponseListener implements Response.Listener<JSONObject> {

        @Override
        public void onResponse(JSONObject response) {

            try {

                if(response.getBoolean("success")) {

                    JSONArray array = response.getJSONArray("netplugs");

                    ArrayList<NetPlug> netplugs = new ArrayList<>();
                    for(int i = 0; i < array.length(); i++) {
                        netplugs.add(new NetPlug(array.getJSONObject(i)));
                    }

                    for(NetplugListener listener: listeners) {
                        listener.onNewNetplugs(netplugs);
                    }

                }

            } catch (Exception e) {
                Log.e(this.toString(), e.getMessage());
            }

        }

    }

    private class ResponseErrorListener implements Response.ErrorListener {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(this.toString(), error.toString());
        }

    }

}

但是显示器上没有填充任何行。 是的,Volley 响应有一个 NetPlug 作为结果。

修改您的构造函数以使用参数调用超级构造函数:

ArrayAdapter(Context context, int resource, List<T> objects)

此外,您不需要重写 addaddAll 方法。默认实现就好了。

编辑:详细说明...
当你打电话

 ArrayAdapter(Context context, int resource)

适配器为对象创建一个空 ArrayList。 在 addaddAll 的实现中,您不调用 super,因此默认 ArrayyList 始终为空。