0

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

5 Answers5

3

Implement like this,

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      String name = productList.get(position).get(KEY_NAME);
      Intent i;
        i = new Intent(mContext, viewProductActivity.class);
        i.putExtra("productName",name);
        startActivity(i);
  } 
});
Exigente05
  • 2,161
  • 3
  • 22
  • 42
3

First simply implement onItemClickListener of listview on current activity

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String product_name = productList.get(position).get(KEY_NAME); 
                Intent intent = new Intent(YourCurrentActivity.this,YourNextActivity.class);
                intent.putExtra("product_name",product_name);
                startActivity(intent);
            }
        });

and receive that value in onCreate method of YourNextActivity.java like this:

Bundle bundle = getIntent().getExtras();
String productname = bundle.getString("product_name");
Krupa Kakkad
  • 857
  • 1
  • 13
  • 28
2

Pass the array list to the new activity later you can use whatever you want from that extra eg:-

Intent i = new Intent(ctx, SecondActivity.class);

            i.putExtra("List", List); //List is an arraylist like yours

            startActivity(i);

Second activity:

SList = (ArrayList<HashMap<String,String>>) getIntent().getSerializableExtra("List");

Now use whatever you want using SList.get(0).get("keyname")

Sumit Shetty
  • 112
  • 1
  • 9
2

put your code here

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);

           // put your new code here
           list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String product_name = productList.get(position).get(KEY_NAME); 
                Intent intent = new Intent(YourCurrentActivity.this,YourNextActivity.class);
                intent.putExtra("product_name",product_name);
                startActivity(intent);
            }
        });

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
Mubashar Javed
  • 749
  • 1
  • 5
  • 12
0

You can pass it via Intent in your listview adapter as

        Intent i;
        i = new Intent(mContext, viewProductActivity.class);
        i.putExtra("productName",whateverstring);
        mContext.startActivity(i);
Ali Asheer
  • 117
  • 13