0

I`m using JSON to parse data from mysql table, with "id" and "firstname" rows, and populate listview with "firstname" row and its working fine. But, I want to add onListItemClick and when user clicks on some name on listview, the "id" value for that name should be forwarded to another class. This is how I populate listview:

            JSONArray jArray = new JSONArray(result);
            JSONObject json_data = null;
            for (int i = 0; i < jArray.length(); i++) {
                json_data = jArray.getJSONObject(i);
                r.add(json_data.getString("firstname"));

            }

            dataAdapter = new ArrayAdapter<String>(Persons.this,
                    R.layout.person_row, r);
            ListView lv = (ListView) findViewById(android.R.id.list);
            lv.setAdapter(dataAdapter); 

and I can forward "firstname" to other class with this:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Object o = this.getListAdapter().getItem(position);
    String FirstName = o.toString();
    Intent i = new Intent(this, PersonDetails.class);
    Bundle bandl = new Bundle();
    bandl.putString("fn", FirstName);
    i.putExtras(bandl);
    startActivity(i);

}

But the problem is that I want to display "firstname" on listview and onClick to forward only "id" to other class, and I cant figure that out. Forwarding "firstname" to other class and then retreiving other mysql data worked fine until I got two people with same name, so that`s why I need to forward row "id" ...

3 Answers 3

1

The easiest way is to create a custom class CustomPerson with 2 attributes : id and firstName. In CustomPerson override toString to return firstName.

ArrayAdapter Creation :

// list should be a List<CustomPerson> and should be filled with your data
dataAdapter = new ArrayAdapter<CustomPerson>(Persons.this, R.layout.person_row, list);

OnListItemClick implementation |:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    CustomPerson o = (CustomPerson) this.getListAdapter().getItem(position);
    long id = o.getId();
    // DO SOMETHING WITH the person's id

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the hints guys, but I`m afraid I will stick to forwarding names with sufix at the end, since this is little complicated for me :)
1

simple short working answer to you

keep a referecne to the JsonArray where you can access it in the onListItemClick

so you can do the following

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String id = jArray. getJSONObject(position).getString("id");
    Intent i = new Intent(this, PersonDetails.class);
    Bundle bandl = new Bundle();
    bandl.putString("id",id);
    i.putExtras(bandl);
    startActivity(i);

}

note you should make it better by change you Adapter to something that can hold both the id and the name. so you don't need to keep a reference to the JsonArray object. here is a tutorial for you .

Comments

0

I somehow managed to get it working using this code:

            lv.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    try {

                        String ajdi = jArray.getJSONObject(position).getString("id").toString();
                        Intent i = new Intent(Persons.this, PersonDetails.class);
                        Bundle bandl = new Bundle();
                        bandl.putString("id", ajdi);
                        i.putExtras(bandl);
                        startActivity(i);

                    } catch (JSONException e) {
                        ;
                    }
                }

            });

Thanks again both of you guys!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.