mvasco mvasco - 22 days ago 8
Android Question

Passing data from listView to activity

In an Android App I have an activity with a listView.
The listView is populated from a JSON string.
I want to pass some of the JSON data from the selected row to another activity.
This is the current method:

private void showJSON(String json){
ParseJSON pj = new ParseJSON(json);
pj.parseJSON();
CustomList cl = new CustomList(this, ParseJSON.ids,ParseJSON.nombres,ParseJSON.apellidos,ParseJSON.emails,ParseJSON.tels);
listView.setAdapter(cl);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

Toast.makeText(Asesor.this, "Ha seleccionado el asesor -> "+ ParseJSON.nombres, Toast.LENGTH_LONG).show();
Intent i = new Intent(Asesor.this, WelcomeNoLogin.class);

// Pass listview item click position
i.putExtra("position", ParseJSON.nombres);
// Open SingleItemView.java Activity
startActivity(i);
}

});


My issue is that I am getting strange values for the selected row data.

For example, if I select the second row, I get for ParseJSON.nombre the value Ljava.lang.string;@42197b8.
What should I change to get the real data?

EDITED:
ParseJSON.class

public class ParseJSON {
public static String[] ids;
public static String[] nombres;

public static String[] apellidos;

public static String[] tels;
public static String[] emails;

public static final String JSON_ARRAY = "result";
public static final String KEY_ID = "id";
public static final String KEY_NOMBRE = "nombre";

public static final String KEY_TEL = "tel";

public static final String KEY_APELLIDOS = "apellidos";
public static final String KEY_EMAIL = "email";

private JSONArray users = null;

private String json;

public ParseJSON(String json){
this.json = json;
}

protected void parseJSON(){
JSONObject jsonObject=null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);

ids = new String[users.length()];
nombres = new String[users.length()];
apellidos = new String[users.length()];
tels = new String[users.length()];

emails = new String[users.length()];

for(int i=0;i<users.length();i++){
JSONObject jo = users.getJSONObject(i);
ids[i] = jo.getString(KEY_ID);
nombres[i] = jo.getString(KEY_NOMBRE);
apellidos[i] = jo.getString(KEY_APELLIDOS);
tels[i] = jo.getString(KEY_TEL);

emails[i] = jo.getString(KEY_EMAIL);
}
} catch (JSONException e) {
e.printStackTrace();
}
}

Answer

Because you are passing whole array object to intent. You have to pass the object of specific position, which you have clicked.

Following code will help you:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

        Toast.makeText(Asesor.this, "Ha seleccionado el asesor -> "+  ParseJSON.nombres[position], Toast.LENGTH_LONG).show();
        Intent i = new Intent(Asesor.this, WelcomeNoLogin.class);

        // Pass listview item click position
        i.putExtra("position", ParseJSON.nombres[position]); /// update code here
        // Open SingleItemView.java Activity
        startActivity(i);
    }

});