List from volley DB-request not updated

android list android-volley adapter

53 观看

1回复

18 作者的声誉

I am facing the problem that my application works with the data provided from the sqlite DB, but does work when retrieving data from a server based DB via volley, although the necessary data are received correctly.

My app is based on the Google Lite List example.

First let's have a look on the working example with sqlite DB.

 setContentView(R.layout.lite_list_demo);
    DatabaseHelper db = new DatabaseHelper(this);
    String skul = " WHERE status = 0";
    mistupload = db.getMista(skul);


    LIST_LOCATIONS = new NamedLocation[mistupload.size()];
    for (int i = 0; i < mistupload.size(); i++) {
        NamedLocation f = new NamedLocation(mistupload.get(i).getName(), new LatLng(mistupload.get(i).getLongi(), mistupload.get(i).getLati()),(mistupload.get(i).getKeyid()));
        LIST_LOCATIONS[i] = f;
    }

    // Set a custom list adapter for a list of locations
    mAdapter = new MapAdapter(this, LIST_LOCATIONS);
    mList = (ListFragment) getSupportFragmentManager().findFragmentById(R.id.list);
    mList.setListAdapter(mAdapter);

    // Set a RecyclerListener to clean up MapView from ListView
    AbsListView lv = mList.getListView();
    lv.setRecyclerListener(mRecycleListener);

This is working fine. Now I want to retrieve the data from my server via Volley.

 LIST_LOCATIONS = new NamedLocation[]{};
    getLocaciones();
    // Set a custom list adapter for a list of locations
    mAdapter = new MapAdapter(this, LIST_LOCATIONS);
    mList = (ListFragment) getSupportFragmentManager().findFragmentById(R.id.list);
    mList.setListAdapter(mAdapter);

    // Set a RecyclerListener to clean up MapView from ListView
    AbsListView lv = mList.getListView();
    lv.setRecyclerListener(mRecycleListener);

The method getLocaciones() retrieves data and I have tried to put mAdapter.notifyDataSetChanged();. in different positions without any result.

private void getLocaciones() {

    pDialog.setMessage("Downloading json...");
    pDialog.show();
    JsonArrayRequest req = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    pDialog.hide();
                    LIST_LOCATIONS = new NamedLocation[response.length()];
                    for (int i = 0; i < response.length(); i++) {
                        try { // Create a marker for each city in the JSON data.
                            JSONObject jsonObj = response.getJSONObject(i);

                            NamedLocation f = new NamedLocation(jsonObj.getString("name"), new LatLng(jsonObj.getJSONArray("latlng").getDouble(0), jsonObj.getJSONArray("latlng").getDouble(1)),0);
                            LIST_LOCATIONS[i] = f;
                            mAdapter.notifyDataSetChanged();

                        } catch (JSONException e) {
                            Log.e(TAG, "Json parsing error: " + e.getMessage());
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    mAdapter.notifyDataSetChanged();

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            pDialog.hide();

        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(req);

}

The adapter code is the following:

private class MapAdapter extends ArrayAdapter<LiteMapsActivity.NamedLocation> {

    private final HashSet<MapView> mMaps = new HashSet<MapView>();

    public MapAdapter(Context context, LiteMapsActivity.NamedLocation[] locations) {
        super(context, R.layout.lite_list_demo_row, R.id.lite_listrow_text, locations);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;

        // Check if a view can be reused, otherwise inflate a layout and set up the view holder
        if (row == null) {
            // Inflate view from layout file
            row = getLayoutInflater().inflate(R.layout.lite_list_demo_row, null);

            // Set up holder and assign it to the View
            holder = new LiteMapsActivity.ViewHolder();
            holder.mapView = (MapView) row.findViewById(R.id.lite_listrow_map);
            holder.title = (TextView) row.findViewById(R.id.lite_listrow_text);

            // Set holder as tag for row for more efficient access.
            row.setTag(holder);
            // check what is necessary!!
            row.setClickable(false);
            holder.mapView.setClickable(false);
            // Initialise the MapView
            holder.initializeMapView();

            // Keep track of MapView
            mMaps.add(holder.mapView);


        } else {
            // View has already been initialised, get its holder
            holder = (ViewHolder) row.getTag();
        }
        // Get the NamedLocation for this item and attach it to the MapView
        NamedLocation item = getItem(position);
        holder.mapView.setTag(item);

        // Ensure the map has been initialised by the on map ready callback in ViewHolder.
        // If it is not ready yet, it will be initialised with the NamedLocation set as its tag
        // when the callback is received.
        if (holder.map != null) {
            // The map is already ready to be used
            setMapLocation(holder.map, item);
        }

        // Set the text label for this item
        holder.title.setText(item.name);

        return row;
    }

In other applications my Volley requests are working fine and the lists are updated. Any help would be appreciated.

作者: Peter 的来源 发布者: 2017 年 2 月 14 日

回应 1


0

18 作者的声誉

The solution is really simple, when you change the code a little bit. Just replace the Class Array LIST_LOCATIONS[i] with an ArrayList and it is working.

 public MapAdapter(Context context,List<NamedLocation> listLocation) {

        super(context, R.layout.lite_list_demo_row, R.id.lite_listrow_text, listLocation);
    }

 private void getLocaciones() {

    pDialog.setMessage("Downloading json...");
    pDialog.show();
    JsonArrayRequest req = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    pDialog.hide();
                   /* LIST_LOCATIONS = new NamedLocation[response.length()];*/
                    for (int i = 0; i < response.length(); i++) {
                        try { // Create a marker for each city in the JSON data.
                            JSONObject jsonObj = response.getJSONObject(i);
                            NamedLocation record = new NamedLocation();
                            record.setName(jsonObj.getString("name"));
                            Double lat =jsonObj.getJSONArray("latlng").getDouble(0);
                            Double lng =jsonObj.getJSONArray("latlng").getDouble(1);
                            LatLng hugo = new  LatLng(lat,lng);
                            record.setLocation(hugo);
                            record.setKeyid(0);
                            listLocation.add(record);

                        } catch (JSONException e) {
                            Log.e(TAG, "Json parsing error: " + e.getMessage());
                        }

                    }

                    mAdapter.notifyDataSetChanged();

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            pDialog.hide();

        }
    });

Voilà.

作者: Peter 发布者: 2017 年 2 月 14 日
32x32