简体   繁体   中英

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

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

});

ParseJSON.nombres is a string array . Use

i.putExtra("position", ParseJSON.nombres[position]);

nombres is a string array. While fetching it from WelcomeNoLogin use intent.getStringArrayExtra, If not already.

Hello @mvasco sir is Right you are passing array not single object. you can pass Single Object two way first pass object using bundle with serialization object or you can also use getter setter method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM