1

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

2
  • Hi, does your app crash when you click an item in the listview or it just doesn't do anything? Commented Aug 27, 2013 at 11:15
  • It crashes, I get E/AndroidRuntime(1127): FATAL EXCEPTION: main E/AndroidRuntime(1127): java.lang.ClassCastException: java.lang.String cannot be cast to java.util.List E/AndroidRuntime(1127): at com.example.AndroidTest.ShowListActivity$1.onItemClick(ShowListActivity.java:119) E/AndroidRuntime(1127): at android.widget.AdapterView.performItemClick(AdapterView.java:298) Commented Aug 27, 2013 at 12:44

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

This is what i want to achieve, but how do I put it as a tag?
Hmm, i tried overriding like this: ' @Override public View getView (int position, View convertView, ViewGroup parent) { convertView.setTag(0,jsonToAdd); return convertView; }' Where jsonToAdd is the same as in above code, except i made it an instance variable.. now i get NullPointerException when the activity is loaded. The error has something to do with the getview()-method, because if i comment it out it doesnt crash
Do you inflate the convertView if it is null?
At the moment I'm not using any inflators at all, Im just using the default layout.. Do i need to make my own layout in order to pass my data?
|
0

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.

1 Comment

This is good thank you, but my main problem is how to pass the json (as a json object or a string) from my class to the onItemClick-method so I have access to the string i want to put as extra on my activity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.