I am extracting the JSON file from the assets folder inside the device. I want to pull product information and images from my JSON file. I can get information, but I don't know how to get pictures. How can I pull different photos in my JSON file? My photos are now in the drawable folder. Names: "soupexample", "simitexample", "chickenexample" .
ListView listView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
try {
JSONObject obj=new JSONObject(LoadFromJsonAssets());
JSONArray array =obj.getJSONArray("domestic");
HashMap<String,String> list;
ArrayList<HashMap<String,String>> arrayList=new ArrayList<>();
for (int i=0;i<array.length();i++){
JSONObject o=array.getJSONObject(i);
String productName=o.getString("productName");
String productPrice=o.getString("productPrice");
String productPic=o.getString("productPic");
list= new HashMap<>();
list.put("productName",productName);
list.put("productPrice",productPrice);
list.put("productPic",productPic);
arrayList.add(list);
}
ListAdapter adapter= new SimpleAdapter(this,arrayList,R.layout.list_view_design,new String[]{"productPic","productName","productPrice"},new int[]{R.id.productPic,R.id.productName,R.id.productPic});
listView.setAdapter(adapter);
}catch (JSONException e){
e.printStackTrace();
}
}
public String LoadFromJsonAssets(){
String json=null;
try {
InputStream in=this.getAssets().open("products.json");
int size = in.available();
byte[] bbuffer=new byte[size];
in.read(bbuffer);
in.close();
json=new String(bbuffer,"UTF-8");
}catch (IOException e){
e.printStackTrace();
return null;
}
return json;
}
}
my json file:
{
"domestic": [
{
"productId": 1,
"productName": "Soup",
"productPrice": "25,00",
"productPic": "soupexample"
},
{
"productId": 2,
"productName": "Simit",
"productPrice": "17,00",
"productPic": "simitexample"
},
{
"productId": 3,
"productName": "Chicken",
"productPrice": "15,00",
"productPic": "chickenexample"
}
]
}