I have setup a Listview with BaseAdapter and so far its working fine with setOnItemClickListener. But it's hard coding.
Main2Activity.java
String sku[]={"1","2","3","4"};
String name[]={"Bad Food","Very Bad Food","Extremly Bad Food","Super Bad Food"};
String price[]={"99","999","9999","99999"};
String quantity[]={"123","456","789","101112"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new my_adapter(Main2Activity.this,sku,name,price,quantity));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(Main2Activity.this, "Sku->"+sku[i]
+" Name->"+name[i]
+" Price->"+price[i]
+" Quantity->"+quantity[i]
,Toast.LENGTH_SHORT).show();
}
});
}
my_adapter.java
public class my_adapter extends BaseAdapter {
Context ctx;
LayoutInflater inflater=null;
String sku[];
String name[];
String price[];
String quantity[];
public my_adapter(Context ctx, String sku[], String name[], String price[],String quantity[]){
this.ctx=ctx;
this.sku=sku;
this.name=name;
this.price=price;
this.quantity=quantity;
}
@Override
public int getCount() {
return sku.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View row=view;
if(row==null){
inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=inflater.inflate(R.layout.rows,null);
}
TextView product_sku=(TextView)row.findViewById(R.id.sku);
TextView product_name=(TextView)row.findViewById(R.id.name);
TextView product_price=(TextView)row.findViewById(R.id.price);
TextView product_quantity=(TextView)row.findViewById(R.id.quantity);
product_sku.setText(sku[i]);
product_name.setText(name[i]);
product_price.setText(price[i]);
product_quantity.setText(quantity[i]);
return row;
}
}
Next, I need to do something similar which is to convert JsonArray to Array and then use it in setOnItemClickListener. That means, I need to store all the data from json into 4 arrays and then use any of the elements later on when they click on the item. Just like my hard coding example.
I have seen some examples with HashMap as well as some JsonArray to array templates coding but not sure how to combine them with my ListView and baseAdapter.
getting json data
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// working with HashMap or Arraylist?????
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsObjRequest);
localhost/web_service/ohno.php
{
"482":["1","Chicken Rice","1","1"], //Unique ID, sku,name,price,quantity
"483":["1","French Fries","1","1"],
"484":["1","apple","1","1"],
"492":["1","western+italian","1","1"],
"493":["1","no_cat","1","1"]
}
The format that I have setup in Listview is exactly same as The above Json format(4 columns) except Json format has an extra element that is Unique product ID.