0

this is my first question on stackoverflow, and I'm kinda new to android (I'm an iOS developer). I'm using volley to populate a listview with Json data, now I want to handle the click on a listview item and pass all the data related to the item I clicked, to a new activity. How can I do that? Here is my "MainActivity" that I called "MenuActivity". Thanks in advance for your help ^^.

public class MenuActivity extends AppCompatActivity {
JSONArray contact = null;
private String urlJsonObj = "http://advadwords.it/Hotel_managing_app/index.php/Controller_managing_app/json_menu";
private static String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
//private String jsonResponse;
ArrayList<HashMap<String, String>> contactList;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
    contactList = new ArrayList<HashMap<String, String>>();
    lv = (ListView) findViewById(R.id.list);
    pDialog = new ProgressDialog(this);
    pDialog.setMessage("Sto caricando...");
    pDialog.setCancelable(false);
    makeJsonObjectRequest();


}
private void makeJsonObjectRequest() {
    pDialog.show();
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
            urlJsonObj, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, response.toString());
            try {
                contact = response.getJSONArray("menu");
                for (int i = 0; i < contact.length(); i++) {
                    JSONObject details = (JSONObject) contact.get(i);
                    String giorno = details.getString("Giorno");
                    String pranzoPrimo = details.getString("Pranzo_primo");

                    HashMap<String, String> contct = new HashMap<String, String>();
                    contct.put("Giorno", giorno);
                    contct.put("Pranzo_primo", pranzoPrimo);

                    contactList.add(contct);


                    //adapter to set response in textview
                    ListAdapter adapter = new SimpleAdapter(
                            MenuActivity.this, contactList,
                            R.layout.list_item, new String[]{"Giorno", "Pranzo_primo"}, new int[]{R.id.name, R.id.primoPranzo});
                    lv.setAdapter(adapter);
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(),
                        "Error: " + e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }
            hidepDialog();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
            // hide the progress dialog
            hidepDialog();
        }
    });
    AppController.getInstance().addToRequestQueue(jsonObjReq);
}
private void showpDialog() {
    if (!pDialog.isShowing())
        pDialog.show();
}
private void hidepDialog() {
    if (pDialog.isShowing())
        pDialog.dismiss();
}

}

Tizio Fit
  • 1
  • 1

1 Answers1

0

I want to handle the click on a listview item and pass all the data related to the item I clicked, to a new activity.

You have to implement onItemClickListener in your listView. When list clicked, pass the ID to another activity and finally retrieved all the data which belongs to ID in the activity.

  lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                HashMap<String, String> clickedItem = contactList.get(position);
                ID=clickedItem.get(Configs.TAG_ID); // your key ID
                Toast.makeText(getApplicationContext(),"Edit"+ID,Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getApplicationContext(), ActivityB.class);
                intent.putExtra("ID", ID); // pass the ID to another activity
                startActivity(intent);
            }
        });
John Joe
  • 12,412
  • 16
  • 70
  • 135