2014-10-04 211 views
0

我是新來的android開發,現在我試圖解析一個J SON數組到對象,然後將每個對象保存到數組列表中,然後顯示數組適配器的數組列表。但是,現在我只能讓陣列適配器顯示最後一個元素。Arrayadapter只返回最後一個元素

這裏是主類

public class Venue extends Activity { 

     Venue_Listing venue = new Venue_Listing(); 
     ArrayList<Venue_Listing> venueList; 
     ListView listview; 

    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      listview = (ListView) findViewById(R.id.list); 

      new GetVenus().execute(URL); 
      venueList = new ArrayList<Venue_Listing>(); 
    } 

    private class GetVenus extends AsyncTask<String, Void, Boolean> { 

      ProgressDialog dialog; 
      String data; 

       protected void onPreExecute() { 
        super.onPreExecute(); 
        dialog = new ProgressDialog(Venue.this); 
        dialog.setMessage("Loading, please wait"); 
        dialog.show(); 
        dialog.setCancelable(false); 
       } 

       protected Boolean doInBackground(String... url) { 
        try { 
         HttpGet httppost = new HttpGet(url[0]); 
         HttpClient httpclient = new DefaultHttpClient(); 
         HttpResponse response = httpclient.execute(httppost); 

         int status = response.getStatusLine().getStatusCode(); 

         if (status == 200) { 
          HttpEntity entity = response.getEntity(); 
          data = EntityUtils.toString(entity); 
         } 
         return true; 
        } 
        catch (ParseException e1) { 
          e1.printStackTrace(); 
        } 
        catch (IOException e) { 
          e.printStackTrace(); 
        } 
        return false; 
       } 

       protected void onPostExecute(Boolean result) { 
        dialog.cancel(); 
        Venue_Listing_Adapter adapter = new     

        Venue_Listing_Adapter(getApplicationContext(),R.layout.venue_list_layout, venueList); 
        try { 
         JSONArray jarray = new JSONArray(data); 
         for (int i = 0; i < jarray.length(); i++) { 
           JSONObject object = jarray.getJSONObject(i); 
            if (venue != null) { 
             venue.SET_ID(object.getString("venue_ID")); 
             venue.SET_NAME(object.getString("name")); 

             listview.setAdapter(adapter); 
            }   
          } 

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

此類包含所有殲SON解析數據

public class Venue_Listing { 

    private String ID; 
    private String NAME; 

public Venue_Listing() { 
} 

public Venue_Listing(String ID, String NAME) { 
    super(); 
    this.ID = ID; 
    this.NAME = NAME; 
} 

// setter & getter.... 

這裏就是我有我的陣列適配器

public class Venue_Listing_Adapter extends ArrayAdapter<Venue_Listing> { 

private ArrayList<Venue_Listing> objects; 

public Venue_Listing_Adapter(Context context, int resource, ArrayList<Venue_Listing> objects) { 
    super(context, resource, objects); 
    this.objects = objects; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 

    if (v == null) { 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.venue_list_layout, null); 
    } 

    Venue_Listing i = objects.get(position); 

    if (i != null) { 
     TextView id = (TextView) v.findViewById(R.id.id); 
     TextView name = (TextView) v.findViewById(R.id.name); 

     if (id != null){ 
      id.setText(i.GET_ID()); 
     } 


     if (name != null) { 
      name.setText(i.GET_NAME()); 
     } 
    } 

    return v; 
    } 
} 

任何幫助將不勝感激 謝謝

回答

0

請打印並檢查'venueList'的值。 您尚未在'venueList'對象中添加值。

您需要每次創建「場地」對象並在「場地列表」中添加對象'場地'。因此,您沒有獲得列表視圖中的所有項目。

代碼可能會走這條路: -

Venue_Listing venue; 
       try { 
        JSONArray jarray = new JSONArray(data); 
        for (int i = 0; i < jarray.length(); i++) { 
          JSONObject object = jarray.getJSONObject(i); 
          venue = new Venue_Listing(); 
           if (object != null) { 
            venue.SET_ID(object.getString("venue_ID")); 
            venue.SET_NAME(object.getString("name")); 
            venueList.add(venue); 
           }   
         } 
         Venue_Listing_Adapter adapter = new Venue_Listing_Adapter(getApplicationContext(),R.layout.venue_list_layout, venueList); 
         listview.setAdapter(adapter); 

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

謝謝了現在的工作。 – 2014-10-05 15:59:18

相關問題