passing JSON object to another activity on list it

2020-05-03 11:58发布

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

activity alldeals

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

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

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

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

3条回答
Explosion°爆炸
2楼-- · 2020-05-03 12:20

If you want it to be more flexible, you can put the JSON data you will pass in an ArrayList and get() the data you want to pass automatically depending on the item clicked like so:

ArrayList<String> jsonList = new ArrayList<String>();
jsonList.add(jsonDataForFirstListitem); // JSON data in String form
jsonList.add(jsonDataForSecondListitem);
// And so on...

Then for your listener, you can simply put it right after you define your list.

ListView list = (ListView) findViewById(R.id.myListView);

list.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> adapter, View view,
                        final int pos, long id) {

                Intent intent = new Intent(context,Finaldeal.class); 
                String jsonData = jsonList.get(pos);
                intent.putExtra("deal", jsonData);
                startActivity(intent);                                     
                }

            });
查看更多
太酷不给撩
3楼-- · 2020-05-03 12:25

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

    }); 

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

查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-05-03 12:25

I know its too late to post this answere but your question is ranking on no.1 position in google search engine, so if someone hasn't find the solution then might be my answere help them. For passing json data to another activity according to Listview position:

Do this inside OnItemClick you need to copy this code:

JSONObject SelectedItem=(yourjsonresponse).getJsonObject(position);

and same as for JSONArray.

then pass SelectedItem.toString in your Intent.

Note : yourjsonresponse is your Main API Response that can be jsonobject or jsonarray.

Thank you

查看更多
登录 后发表回答