Arrayadapter只返回最后一个元素

时间:2014-10-04 18:21:51

标签: android-listview android-arrayadapter

我是Android开发的新手,现在我试图将J SON数组解析为对象,然后将每个对象保存到数组列表中,然后使用数组适配器显示数组列表。但是,现在我只能让数组适配器显示最后一个元素。

这是主要的课程

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

此类包含所有J 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;
    }
}

任何帮助将不胜感激 谢谢

1 个答案:

答案 0 :(得分:3)

请打印并检查&#39; venueList&#39;的值。 您尚未在&#39; venueList&#39;中添加值对象

你需要创建场地&#39;每次对象并添加对象&#39;场所&#39;在&#39; venueList&#39;。因此,您没有在列表视图中获取所有项目。

守则可以这样: -

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