繁体   English   中英

自定义列表适配器获取相同的对象

[英]Custom List Adapter is getting the same Object

我正在尝试使用GET方法获取用户列表,并将其显示在列表视图中。 扼杀我正在使用的客户列表视图是用数组列表中的最后一个对象填充列表视图。 之前可能已经问过这个问题。 但是我无法得到任何适当的答复。 提前应用。 以下是我主要班级的节选。

String tag_string_req = "req_search";

StringRequest strReq = new StringRequest(Method.POST,
    AppConfig.searchUrl, new Response.Listener<String>() {

  @Override
  public void onResponse(String response) {
    Log.d(TAG, "Search Response: " + response.toString());
    hidePDialog();
    // for(int i=0;i<response.length();i++) {
    try {
      JSONObject jObj = new JSONObject(response);
      boolean error = jObj.getBoolean("error");

      // Check for error node in json
      if (!error) {
        // user successfully logged in
        // Create login session

        // Now store the user in SQLite
        Donors donor = new Donors();
        // String uid = jObj.getString("uid");

        JSONArray user = jObj.getJSONArray("users");

        for (int i = 0; i < user.length(); i++) {
          JSONObject ja = user.getJSONObject(i);
          donor.setId(Integer.parseInt(ja.getString("id")));
          donor.setName(ja.getString("name"));
          donor.setPhone(ja.getString("phone"));

          System.out.println(
              "*********************************************************" + Integer
                  .parseInt(ja.getString("id")) + " " + ja.getString("name") + " " + ja
                  .getString("phone"));
          donorsList.add(donor);
        }

        adapter.notifyDataSetChanged();

      } else {
        // Error in login. Get the error message
        String errorMsg = jObj.getString("message");
        Toast.makeText(getApplicationContext(),
            errorMsg, Toast.LENGTH_LONG).show();

      }
    } catch (JSONException e) {
      // JSON error
      e.printStackTrace();
      Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(),
          Toast.LENGTH_LONG).show();
    }

  }


}, new Response.ErrorListener() {

  @Override
  public void onErrorResponse(VolleyError error) {
    Log.e(TAG, "Search Error: " + error.getMessage());
    Toast.makeText(getApplicationContext(),
        error.getMessage(), Toast.LENGTH_LONG).show();
    hidePDialog();
  }
}) {

  @Override
  protected Map<String, String> getParams() {
    // Posting parameters to login url
    Map<String, String> params = new HashMap<String, String>();
    System.out.println(
        "*********************************************************" + distance + " " + donorLat
            + " " + donorLong + " " + bloodGroup);

    params.put("distance", distance);
    params.put("origin_lat", donorLat);
    params.put("origin_long", donorLong);
    params.put("blood_group", bloodGroup);

    return params;
  }

};

// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

这就是列表适配器。

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Donors> donorItems;
public CustomListAdapter(Activity activity, List<Donors> donorItems) {
this.activity = activity;
this.donorItems = donorItems;
}

@Override
public int getCount() {
return donorItems.size();
}

@Override
public Object getItem(int location) {
return donorItems.get(location);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (inflater == null)
  inflater = (LayoutInflater) activity
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
  convertView = inflater.inflate(R.layout.list_row, null);

;
TextView title = (TextView) convertView.findViewById(R.id.title);
ImageButton btnText = (ImageButton) convertView.findViewById(R.id.btnText);
ImageButton btnCall = (ImageButton) convertView.findViewById(R.id.btnCall);

// getting movie data for the row
Donors d = donorItems.get(position);
System.out.println("--------------------"+d);


// title
title.setText(d.getName());


return convertView;
}

}

谢谢。

发现错误。 在for循环之外初始化对象。 应该是这样 谢谢。

for (int i = 0; i < user.length(); i++) {
          JSONObject ja = user.getJSONObject(i);
          Donors donor = new Donors();
          donor.setId(Integer.parseInt(ja.getString("id")));
          donor.setName(ja.getString("name"));
          donor.setPhone(ja.getString("phone"));

          System.out.println(
              "*********************************************************" + Integer
                  .parseInt(ja.getString("id")) + " " + ja.getString("name") + " " + ja
                  .getString("phone"));
          donorsList.add(donor);
          System.out.println(donor);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM