android使用listview将json数据转发到另一个类

android使用listview将json数据转发到另一个类,android,json,listview,Android,Json,Listview,我正在使用JSON解析mysql表中的数据,其中包含“id”和“firstname”行,并使用“firstname”行填充listview及其工作正常。但是,我想添加onListItemClick,当用户单击listview上的某个名称时,该名称的“id”值应该转发到另一个类。这是我填充listview的方式: JSONArray jArray = new JSONArray(result); JSONObject json_data = null

我正在使用JSON解析mysql表中的数据,其中包含“id”和“firstname”行,并使用“firstname”行填充listview及其工作正常。但是,我想添加onListItemClick,当用户单击listview上的某个名称时,该名称的“id”值应该转发到另一个类。这是我填充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); 

但问题是,我想在listview上显示“firstname”,单击onClick只将“id”转发给其他类,我无法理解这一点。将“firstname”转发到其他类,然后再转发其他mysql数据,效果很好,直到我找到两个同名的人,所以这就是为什么我需要转发行“id”…

简单的简短工作答案给您

保留对
JsonArray
的引用,您可以在
onListItemClick

因此,您可以执行以下操作

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

}

注意,您应该将适配器更改为既可以保存id又可以保存名称的适配器,这样做会更好。因此,不需要保留对JsonArray对象的引用。这是给您的一个示例。

最简单的方法是创建一个自定义类CustomPerson,它有两个属性:id和firstName。在CustomPerson中,重写toString以返回firstName

ArrayAdapter创建:

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

我设法使用以下代码使其正常工作:

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

            });
lv.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
试一试{
字符串ajdi=jArray.getJSONObject(position.getString(“id”).toString();
意向i=新意向(Persons.this,PersonDetails.class);
Bundle bandl=新Bundle();
bandl.putString(“id”,ajdi);
i、 putExtras(bandl);
星触觉(i);
}捕获(JSONException e){
;
}
}
});

再次感谢你们两位

谢谢大家的提示,但我恐怕最后还是会坚持用sufix转发名字,因为这对我来说有点复杂:)
@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

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

            });