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