2016-09-30 3 views
0

setzen Ich habe Probleme mit JsonArray in ListView. Ich benutze eine Custom ArrayAdapter aber alles, was auf dem Emulator-Bildschirm kommt, ist nur das ganze JsonArray.Kann nicht in der Lage zu sein, JsonArray in ListView

Diese Aktivität die ganze Iteration durch die JsonArray hält, ein ArrayAdapter und ein ViewHolder

public class ShowFishiesActivity extends Activity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_show_fishies); 
    Spinner spinner = (Spinner) findViewById(R.id.fishSpinner); 
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.fishSortArray, android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(adapter); 


} 

@Override 
protected void onStart() { 
    super.onStart(); 
    ReadTask task = new ReadTask(); 
    task.execute("http://api.evang.dk/v1/catches"); 

} 




private class ReadTask extends ReadHttpTask { 

    Adapter newAdapter; 
    List<Catch> catches; 

    @Override 
    protected void onPostExecute(CharSequence charSequence) { 
     super.onPostExecute(charSequence); 
     TextView messageText = (TextView) findViewById(R.id.messageText); 
     messageText.setText(charSequence); 
     catches = new ArrayList<>(); 
     try { 
      JSONArray array = new JSONArray(charSequence.toString()); 
      for (int i = 0; i < array.length(); i++) { 
       JSONObject obj = array.getJSONObject(i); 
       Integer id = obj.getInt("id"); 
       String angler_name = obj.getString("angler_name"); 
       String dateTime = obj.getString("datetime"); 
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
       Date jsonDate = sdf.parse(dateTime); 
       String fishingMethod = obj.getString("fishing_method"); 
       String fishBreed = obj.getString("breed"); 
       Double length = obj.getDouble("length"); 
       Double weight = obj.getDouble("weight"); 
       String weather = obj.getString("weather"); 
       String location = obj.getString("location"); 
       Double latitude = obj.getDouble("latitude"); 
       Double longitude = obj.getDouble("longitude"); 


       Catch fishCatch = new Catch(id, angler_name, jsonDate, fishingMethod, fishBreed, length, weight, weather, location, latitude, longitude); 
       catches.add(fishCatch); 
       ListView listView = (ListView) findViewById(R.id.fishListView); 
       //ArrayAdapter<String> arrayAdapter = new ArrayAdapter(ShowFishiesActivity.this, android.R.layout.simple_expandable_list_item_1, catches); 
       //listView.setAdapter(arrayAdapter); 
       newAdapter = new Adapter(); 
       listView.setAdapter(newAdapter); 

      } 

     } catch (JSONException ex) { 
      messageText.setText(ex.getMessage()); 
      Log.e("Catches", ex.getMessage()); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
    } 

Individuelle Adapter

class Adapter extends ArrayAdapter<Catch> { 

     public Adapter() { 
      super(ShowFishiesActivity.this, android.R.layout.simple_list_item_1, catches); 
     } 

     public View getView(int position, View convertView, ViewGroup parent){ 
      ViewHolder holder; 

      if(convertView == null){ 
       LayoutInflater inflater = getLayoutInflater(); 
       convertView = inflater.inflate(R.layout.activity_item_adapter, null); 

       holder = new ViewHolder(convertView); 
       convertView.setTag(holder); 
      } 
      else { 
       holder = (ViewHolder) convertView.getTag(); 

      } 
      holder.populateFrom(catches.get(position)); 
      return (convertView); 
     } 
    } 

anzeigen Halter

class ViewHolder { 

     TextView anglerName = null; 
     TextView date = null; 
     TextView fishingmethod = null; 
     TextView fishBreed = null; 
     TextView fishlength = null; 
     TextView fishWeight = null; 
     TextView fishWeather = null; 
     TextView fishLocation = null; 
     TextView fishLatitude = null; 
     TextView fishLongitude = null; 

     ViewHolder(View catchView){ 

      anglerName = (TextView) catchView.findViewById(R.id.fishList_item_anglerName); 
      date = (TextView) catchView.findViewById(R.id.fishList_item_dateTime); 
      fishingmethod = (TextView) catchView.findViewById(R.id.fishList_item_fishingMethod); 
      fishBreed = (TextView) catchView.findViewById(R.id.fishList_item_fishBreed); 
      fishlength = (TextView) catchView.findViewById(R.id.fishList_item_length); 
      fishWeight = (TextView) catchView.findViewById(R.id.fishList_item_weight); 
      fishWeather = (TextView) catchView.findViewById(R.id.fishList_item_weather); 
      fishLocation = (TextView) catchView.findViewById(R.id.fishList_item_location); 
      fishLatitude = (TextView) catchView.findViewById(R.id.fishList_item_latitude); 
      fishLongitude = (TextView) catchView.findViewById(R.id.fishList_item_longitude); 


     } 

     void populateFrom(Catch catchFish){ 
      anglerName.setText(catchFish.getAngler_name()); 

      String str = String.format(String.valueOf(catchFish.getDateTime())); 
      date.setText(str); 

      fishingmethod.setText(catchFish.getSpearfishing()); 

      fishBreed.setText(catchFish.getBreed()); 

      String parseLength = Double.toString(catchFish.getLength()); 
      fishlength.setText(parseLength); 

      String parseWeight = Double.toString(catchFish.getWeight()); 
      fishWeight.setText(parseWeight); 

      fishWeather.setText(catchFish.getWeather()); 

      fishLocation.setText(catchFish.getLocation()); 

      String parseLatitude = Double.toString(catchFish.getLatitude()); 
      fishLatitude.setText(parseLatitude); 

      String parseLongitude = Double.toString(catchFish.getLongitude()); 
      fishLongitude.setText(parseLongitude); 
     } 
    } 

} 

} 

Ich habe ein XML für die ListView und ein XML für das benutzerdefinierte Layout des ListView. Ich bin mir nicht ganz sicher, ob Sie das sehen möchten, aber wenn es nötig ist, würde ich mich freuen, es der Frage hinzuzufügen.

+0

Welche Ansicht ist das gesamte JSON-Array in? – brandall

Antwort

1

Wenn Ihre ReadHttpTask Http Aufgabe ausführen und Daten charSequence zurückgeben. Ihr parsen Sie Ihr Json-Objekt falsch, sollte so sein (es ein Objekt kein Array)

JSONObject reader = new JSONObject(charSequence.toString()); 
JSONArray jsonArray = reader.optJSONArray("catches"); 
Verwandte Themen