2017-02-14 3 views
0

Ich stehe vor dem Problem, dass meine Anwendung mit den Daten aus der sqlite DB arbeitet, aber funktioniert beim Abrufen von Daten von einer Server-basierten DB über Volley, obwohl die notwendigen Daten korrekt empfangen werden .Liste von Volley DB-Anfrage nicht aktualisiert

Meine App basiert auf dem Google Lite List-Beispiel.

Zuerst schauen wir uns das Arbeitsbeispiel mit sqlite DB an.

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); 

Das funktioniert gut. Jetzt möchte ich die Daten von meinem Server via Volley abrufen.

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); 

Die Methode getLocaciones() ruft Daten und ich habe versucht mAdapter.notifyDataSetChanged() zu setzen ;. in verschiedenen Positionen ohne Ergebnis.

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); 

} 

Der Adapter Code ist folgende:

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 anderen Anwendungen mein Volley-Anfragen arbeiten gut und die Listen aktualisiert werden. Jede Hilfe wäre willkommen.

+0

Haben Sie den Logcat auf Fehler überprüft? auch eine kleine Probe (ackle von Datensätzen), wie die Json-Daten aussehen – Tasos

+0

Dies ist nicht das Problem, Daten sind in Ordnung und die LIST_LOCATIONS [] ist gefüllt, wie es sein sollte. Das Debuggen zeigt, dass der Code niemals in den Adapterbereich gelangt. – Peter

Antwort

0

Die Lösung ist wirklich einfach, wenn Sie den Code ein wenig ändern. Ersetzen Sie einfach das Klassenarray LIST_LOCATIONS [i] durch eine ArrayList und es funktioniert.

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à.

Verwandte Themen