我正在尝试使用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;
}
}
感谢。
答案 0 :(得分:0)
发现错误。在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);
}