2017-09-16 75 views
1

我想獲取使用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

發現錯誤。初始化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); 
     }