Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialadnanalvee2
6,862 PointsHow to navigate to a new activity from list item?
I've been going through all over Stackeoverflow and some other android tutorial sites just to make the items in the List clickable in order to navigate to a new activity class. But I couldn't. The code online looks kinda different and most of them are using listview.
I need to make each list item navigate to a new activity class as set by me according to their position.
**This is the method that's sets the adapter but how do I make the intent stuff. Please help.
private void handleAreaListResponse() {
mProgressBar.setVisibility(View.INVISIBLE);
if (mAreaData == null) {
updateDisplayForError();
}
else {
try {
JSONArray jsonPosts = mAreaData.getJSONArray("Areas");
ArrayList<HashMap<String, String>> areaList = new
ArrayList<HashMap<String, String>>();
for (int i = 0; i <jsonPosts.length(); i++) {
JSONObject post = jsonPosts.getJSONObject(i);
//Adding secondary Data with a....
String name = post.getString(KEY_NAME);
name = Html.fromHtml(name).toString();
String areaId = post.getString(KEY_ID);
areaId = Html.fromHtml(areaId).toString();
HashMap<String, String> areaListing = new HashMap<String, String>();
areaListing.put(KEY_NAME, name);
areaListing.put(KEY_ID, areaId);
areaList.add(areaListing);
}
String[] keys = {KEY_NAME, KEY_ID};
int[] ids = {android.R.id.text1,android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, areaList, android.R.layout.simple_list_item_2, keys, ids);
setListAdapter(adapter);
} catch (JSONException e) {
Log.e(TAG, "Exception caught!", e);
}
}
}
2 Answers
Ben Jakuben
Treehouse TeacherKeep going! We cover that in Stage 5. :)
adnanalvee2
6,862 PointsThanks a lot!
adnanalvee2
6,862 Pointsadnanalvee2
6,862 PointsThanks Ben Jakuben; But my question was to navigate to different activity files. All of your list item points to the same "Blogwebview" activity, Here is what I am trying to do, Suppose I have 3 item in my list view, a)walmart b)Kroger c) Walgreens, when I click on walmart I get to see another activity named "walmartActivity" which outputs json from the server in another list and these items are all what walmart sells. Similarly I want to have KorgerActivity and WalgreensActivity.
Here is your code, I wanted to code something that would go with the position or KEY_ID , idk.
adnanalvee2
6,862 Pointsadnanalvee2
6,862 PointsI got a nice tutorial here. I think I will just work through this one! (http://www.androidhive.info/2012/10/android-multilevel-listview-tutorial/)
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherAh. In that case just add a switch statement for
position
and start the new Intent based on that. Something like this: