So here I have class ListProductActivity.java that display list of products based on the search criteria entered by user and the data are retrieve from MySQL server using Json. My question is, how can I pass a single value (productName in this case) when user select from the listview to another activity so that I can use that value in the new Activity (viewProductActivity.java).
ListProductActivity.java
public class ListProductActivity extends AppCompatActivity {
private ListView list;
private ProgressDialog loading;
ArrayList<HashMap<String, String>> productList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_product);
list = (ListView) findViewById(R.id.listView);
productList = new ArrayList<HashMap<String,String>>();
getData();
}
public void getData(){
String s = getIntent().getStringExtra("productName");
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
String url = Config.DATA_URL2 + s;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showList(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ListProductActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showList(String response){
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray products = jsonObject.getJSONArray(Config.JSON_ARRAY);
for(int i=0;i<products.length();i++){
JSONObject productData = products.getJSONObject(i);
String name = productData.getString(KEY_NAME);
String price = productData.getString(KEY_PRICE);
String brand = productData.getString(KEY_BRAND);
HashMap<String,String> product = new HashMap<String,String>();
product.put(KEY_NAME,name);
product.put(KEY_PRICE,price);
product.put(KEY_BRAND,brand);
productList.add(product);
}
ListAdapter adapter = new SimpleAdapter(
ListProductActivity.this, productList, R.layout.list_product,
new String[]{KEY_NAME,KEY_PRICE,KEY_BRAND},
new int[]{R.id.name, R.id.price, R.id.brand}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
So, what function can I implement in my ListProductActivity class to pass the productName based on user selected in the listView to another activity and how to receive the passing value in viewProductActivity class?
I really hope you guys can post the function and briefly explain on how it works, so that I can understand the codes really well. Comment at the codes will be more clear and helpful. Thank You.
Edited
Here is the picture of the result from the listview after searching Click To View Image