简体   繁体   中英

How to pass data through ListView, Android - Java

I'm stuck again.. I've read tons of answers here on stackoverflow that touches what I want to achieve, but I've been unable to solve my problem.

I took this tutorial on ListViews, and got that working. However in my application I want each row in the listview to have a non-visible parameter storing a JSON-object, that I can pass forward to the next view i want to push when a row is clicked. My code looks like this at the moment (after in vain trying to send the JSON in a List)

private class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<List, Integer> mIdMap = new HashMap<List, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,
                              List<String> objects, JSONObject jsonToAdd) {
        super(context, textViewResourceId, objects);

     /*   for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
        } */
        JSONArray keyNames = null;
        keyNames = jsonToAdd.names();

        for (int i = 0; i < objects.size(); ++i) {
            JSONObject tempJSON =null;
            HashMap<String, String> tempHashMap = new HashMap<String, String>();
            List<String> tempList = new ArrayList<String>();
            try{
                tempJSON = jsonToAdd.getJSONObject(keyNames.get(i).toString());
            } catch (JSONException e){
                System.out.println("error");
            }
            tempHashMap.put(objects.get(i),tempJSON.toString());
            tempList.add(0, objects.get(i));
            tempList.add(1,tempJSON.toString());
            mIdMap.put(tempList, i);
        }
    }


    @Override
    public long getItemId(int position) {
        List<String> itemList = new ArrayList<String>();
        String item = getItem(position);

        return position;//mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

And this is how I call it

final StableArrayAdapter adapter = new StableArrayAdapter(this,
            android.R.layout.simple_list_item_1, listViewSource,forms);
    listview.setAdapter(adapter);




    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            JSONObject forms = null;
            JSONObject jsonToPass = null;
            try {
                List<String> stuff = (List)parent.getItemAtPosition(position);
                forms = new JSONObject(stuff.get(1));
                jsonToPass = forms.getJSONObject(keyNames.get(position).toString());
            } catch (JSONException e) {
                e.printStackTrace();  
            }

           Intent anotherActivityIntent = new Intent(ShowListActivity.this, CollectionPointListActivity.class);
            anotherActivityIntent.putExtra("object",jsonToPass.toString());
            startActivity(anotherActivityIntent); 
        }
    });

Obviously this is not the way to do it, how can I pass a data object from my main class, so that I can access it in "onItemClick()"? What is the best way to achieve what I'm trying to do?

Regards,

Daniel

Put the JSON as a tag of the row view. Later from the method onItemClick(AdapterView parent, View view , int position, long id) get the tag from the view parameter which is your JSON.

From the code that you have written above it would be better to just get the data in String form in other Activity and then make a JSONObject out of it.

Intent anotherActivityIntent = new Intent(ShowListActivity.this, CollectionPointListActivity.class);
anotherActivityIntent.putExtra("object",jsonToPass.toString());
startActivity(anotherActivityIntent);

You are already passing the string content. So, in CollectionPointListActivity get the String content and create a JSONObject out of it as shown below.

JSONObject jsonObj = new JSONObject(getIntent().getExtras().getString("object"));

This should give you the JSONObj.

But ideally, you should create model classes for the response that you are getting and just implement Serializable for model classes and you will be able to directly pass the model class object. Moreover, there are Json marshalling libraries available such as JacksonJson and Gson which are easy to use and does the work for us.

Anyways, I hope this helps you out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM