passing JSON object to another activity on list item click

只愿长相守 提交于 2020-01-22 02:27:09

问题


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

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

.

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


回答1:


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




回答2:


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:


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



来源:https://stackoverflow.com/questions/16632072/passing-json-object-to-another-activity-on-list-item-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!