passing JSON object to another activity on list item click

前端 未结 3 993
长发绾君心
长发绾君心 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: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 jsonList = new ArrayList();
    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);                                     
                    }
    
                });
    

提交回复
热议问题