-4

I am trying to show database values into a Listview but, I set position of element manually (e.g. if(position==0){}) to do the intent to next activity. But i want to show these element according to their id, name, and mobile (i.e. for 1st element activity can parse these parameter to next activity, for 2nd parameters can hold their appropriate value...etc). so how can i perform this!

List.java

  protected void showList(){
            try {
                JSONObject jsonObj = new JSONObject(myJSON);
                peoples = jsonObj.getJSONArray(TAG_RESULTS);

                for(int i=0;i<peoples.length();i++){
                    JSONObject c = peoples.getJSONObject(i);
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String mobile = c.getString(TAG_MOB);

                    HashMap<String,String> persons = new HashMap<String,String>();

                    persons.put(TAG_ID,id);
                    persons.put(TAG_NAME,name);
                    persons.put(TAG_MOB,mobile);

                    personList.add(persons);
                }

                ListAdapter adapter = new SimpleAdapter(
                        User_List.this, personList, R.layout.list_item,
                        new String[]{TAG_ID,TAG_NAME,TAG_MOB},
                        new int[]{R.id.id, R.id.name, R.id.mobile}
                );

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

                                Intent intent = new Intent(User_List.this, MapsActivity.class);
                                startActivity(intent);
                            }
                    }
                });

            } catch (JSONException e) {
                e.printStackTrace();
            }
Rushi K
  • 37
  • 2
  • 7

1 Answers1

0

All you need to do is send the Hashmap object at that position in personList array like this:

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

                                Intent intent = new Intent(User_List.this, MapsActivity.class);
                                intent.putExtra("object",(Serializable)personList.get(position));
                                startActivity(intent);
                            }

                });

and in another activity:

  HashMap<String,String> object=(HashMap<String, String>)getIntent().getSerializableExtra("object");

and then just set the textview like:

textview.setText(object.TAG_ID);
textview1.setText(object.TAG_NAME);
textview2.setText(object.TAG_MOB);

check this if it works!!!!!

if u have anymore doubts about how to pass hashmap between actvities check below link:

Android - How to pass HashMap<String,String> between activities?

Community
  • 1
  • 1
Satish Silveri
  • 393
  • 4
  • 9