passing JSON object to another activity on list item click

前端 未结 3 995
长发绾君心
长发绾君心 2021-01-28 18:02

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

\"activity

相关标签:
3条回答
  • 2021-01-28 18:05

    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

    0 讨论(0)
  • 2021-01-28 18:16

    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);                                     
                    }
    
                });
    
    0 讨论(0)
  • 2021-01-28 18:24

    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

    0 讨论(0)
提交回复
热议问题