Reputation: 2748
I want to show data from my json to my listview
. Here is what i try
private void loadIntoListView(String json) throws JSONException {
List<Itemadapter> items = new ArrayList<Itemadapter>();
JSONArray jsonArray = new JSONArray(json);
String[] ba = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
String code = obj.getString("ItemCode");
String name = obj.getString("ItemName");
String photo = obj.getString("PhotoName");
items.add(new Itemadapter(code, name,photo));
Log.i(code, code);
}
Toast.makeText(getActivity(), items.toString(), Toast.LENGTH_LONG).show();
ArrayAdapter<Itemadapter> mArrayAdapter = new ArrayAdapter<Itemadapter>(getActivity(),android.R.layout.simple_expandable_list_item_1,items);
listView.setAdapter(mArrayAdapter);
}
My Itemadapter
public class Itemadapter {
String ItemCode;
String ItemName;
String ItemPhoto;
public Itemadapter(String ItemCode, String ItemName, String ItemPhoto) {
this.ItemCode = ItemCode;
this.ItemPhoto = ItemPhoto;
this.ItemName = ItemName;
}
public String getCode() {
return ItemCode.toString();
}
public String getItemPhoto() {
return ItemPhoto.toString();
}
public String getItemName(){
return ItemName.toString();
}
}
when i run it, there is no error . But, in my listview
i see something like this in multiplerows
com.example.boby.firstapp.Itemadapter@(somecode)
How can i fix it ? did i miss something ?
Upvotes: 0
Views: 501
Reputation: 985
ArrayAdapter<Itemadapter> mArrayAdapter = new ArrayAdapter<Itemadapter>(getActivity(),android.R.layout.simple_expandable_list_item_1,items);
This error comes because of this line. Let me explain. If you want to show itemCode in listview, then the code must be like this.
ArrayList<String> itemsCode = new ArrayList<>();
for(int i=0;i<items.size();++i){
itemsCode.add(item.get(i).getCode());
}
ArrayAdapter<> mArrayAdapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,itemsCode);
The third parameter should be what you want to display
Upvotes: 1
Reputation: 4839
You need to override toString()
in Itemadapter
:
public class Itemadapter {
String ItemCode;
String ItemName;
String ItemPhoto;
public Itemadapter(String ItemCode, String ItemName, String ItemPhoto) {
this.ItemCode = ItemCode;
this.ItemPhoto = ItemPhoto;
this.ItemName = ItemName;
}
public String getCode() {
return ItemCode.toString();
}
public String getItemPhoto() {
return ItemPhoto.toString();
}
public String getItemName(){
return ItemName.toString();
}
@Override
public String toString() {
return ItemCode + " " + ItemName;
}
Upvotes: 2
Reputation: 101
The error is on the getters methods at ItemAdapter
class, remove .toString()
from all getters.
public class Itemadapter {
String ItemCode;
String ItemName;
String ItemPhoto;
public Itemadapter(String ItemCode, String ItemName, String ItemPhoto) {
this.ItemCode = ItemCode;
this.ItemPhoto = ItemPhoto;
this.ItemName = ItemName;
}
public String getCode() {
return ItemCode;
}
public String getItemPhoto() {
return ItemPhoto;
}
public String getItemName(){
return ItemName;
}
}
I hope it will help you!
Upvotes: 0