[英]Passing data from listView to activity
In an Android App I have an activity with a listView. 在一个Android应用程序中,我有一个带有listView的活动。 The listView is populated from a JSON string. listView是从JSON字符串填充的。 I want to pass some of the JSON data from the selected row to another activity. 我想将一些JSON数据从所选行传递到另一个活动。 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. 例如,如果选择第二行,则为ParseJSON.nombre获得值Ljava.lang.string; @ 42197b8。 What should I change to get the real data? 我应该改变以获得真实数据?
EDITED: ParseJSON.class 编辑: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. 因为您要将整个数组对象传递给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 . ParseJSON.nombres
是一个字符串数组 。 Use 采用
i.putExtra("position", ParseJSON.nombres[position]);
nombres is a string array. nombres是一个字符串数组。 While fetching it from WelcomeNoLogin use intent.getStringArrayExtra, If not already. 从WelcomeNoLogin获取它时,请使用intent.getStringArrayExtra(如果尚未获取)。
Hello @mvasco sir is Right you are passing array not single object. 您好@mvasco先生是,您正在传递数组而不是单个对象。 you can pass Single Object two way first pass object using bundle with serialization object or you can also use getter setter method 您可以使用捆绑带有序列化对象的方式通过单对象两种方式首先传递对象,也可以使用getter setter方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.