Reputation: 209
I would like to show data from an API in a ListView. I don't know why but it seems to display my model path in the list. Here is the code:
Model:
public class Categories {
private String name;
public Categories(String name) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Adapter:
public class CategoriesAdapter extends ArrayAdapter<Categories> {
private final List<Categories> list;
private final Activity context;
public CategoriesAdapter(Activity context, List<Categories> list) {
super(context, android.R.layout.simple_list_item_activated_1, list);
this.context = context;
this.list = list;
}
}
Main Activity request
private void makeJsonArrayRequest(String api_url) {
showpDialog();
JsonArrayRequest req = new JsonArrayRequest(api_url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject cat_obj = (JSONObject) response.get(i);
list.add(new Categories(cat_obj.getString("title")));
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
});
// Volley Request Queue
AppController.getInstance().addToRequestQueue(req);
}
The result of this code is for each data: "com.xxx.xxx.Models.Categories@52b3adec" etc...
Upvotes: 0
Views: 30
Reputation: 51711
You need to override your Categories#toString()
too. What you're currently seeing in your list comes from the default Object#toString()
implementation.
public class Categories {
private String name;
public Categories(String name) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
The above will now display the category names in your ListView
.
Upvotes: 1