通过JSON对象到另一个活动的列表项点击 [英] passing JSON object to another activity on list item click

查看:107
本文介绍了通过JSON对象到另一个活动的列表项点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况在这里,我想通过在列表项单击所需的JSON对象在我下面的活动

I have a scenario here,i want to pass the desired json object on the list item click in my below activity

假设当我点击处理2 从jArray第二JSON对象应传递给意图,我的下一个活动都必须明白这一点。

suppose when i click deal 2 2nd json object from jArray should be passed to intent and my next activity must get it.

但这里的问题是,当我点击任何列表项的下一个活动收到最后一个索引在JSON对象

but problem here is that when i click any of the list items the next activity receives json object at last index .

下面是我从alldeals.java code

here is my code from alldeals.java

try{
            jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                    json_data = jArray.getJSONObject(i);

                    Log.i("log_tag","dealid: "+json_data.getString("deal_id")+
                            ", hotel name: "+json_data.getString("hotel_name")+
                            ", location: "+json_data.getString("location")+
                            ", website: "+json_data.getString("website")
                    );

            }

            json_data=new JSONObject();
            String[] data=new String[jArray.length()];
            planetList = new ArrayList<String>();
                for(int i=0;i<jArray.length();i++)
                {
                    json_data= jArray.getJSONObject(i);
                    data[i]=json_data.getString("deal_id");
                    Log.i("log_tag", "string "+data[i]);
                    planetList.addAll( Arrays.asList("Deal "+ (i+1)));  
                    listAdapter = new ArrayAdapter<String>(this, R.layout.listrow, planetList);
                    runOnUiThread(new Runnable() {
                        public void run() {
                            list.setAdapter(listAdapter);
                        }
                    });
                 }
                list.setOnItemClickListener(new OnItemClickListener() {  //where to put this piece of code???

                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            final int arg2, long arg3) {

                    Intent intent = new Intent(context,Finaldeal.class);  
                    intent.putExtra("deal", json_data.toString());
                    startActivity(intent);                                     
                    }

                }); 

    }
    catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }

我知道,为了解决这个问题,我必须把我的 list.setOnItemClickListener()在适当的地方,但我很困惑,把它放在哪里,所以,当我点击在 deal1 第一JSON对象将被传递给意向

i know in order to solve this i have to put my list.setOnItemClickListener() at proper place but i am confused where to put it,so that when i click on deal1 the 1st json object will be passed to intent

推荐答案

您可以根据ListView项通过的JSONObject 从JsonArray点击位置为:

you can pass JsonObject from JsonArray according to ListView item clicked Position as:

list.setOnItemClickListener(new OnItemClickListener() {

   public void onItemClick(AdapterView<?> arg0, View arg1,
                    final int arg2, long arg3) {
         if(!jArray.isNull(arg2)){
           Intent intent = new Intent(context,Finaldeal.class);  
           intent.putExtra("deal", jArray.optJSONObject(arg2).toString());
           startActivity(intent);                                     
         }   
       }

    }); 

目前你逝去的 json_data 总是包含JSONArray最后的JSONObject当for循环执行结束

currently you are passing json_data which always contain last JSONObject from JSONArray when for loop execution end

这篇关于通过JSON对象到另一个活动的列表项点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆