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 trial

Android Build a Blog Reader Android App Using Intents to Display and Share Posts Opening a Webpage in the Browser

adnanalvee2
adnanalvee2
6,862 Points

How 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);
            }

        }

    }

Ben Jakuben

2 Answers

adnanalvee2
adnanalvee2
6,862 Points

Thanks 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.

protected void onListItemClick(ListView l, View v, int position, long id) 
{ super.onListItemClick(l, v, position, id); try { JSONArray jsonPosts = mBlogData.getJSONArray("posts"); JSONObject jsonPost = jsonPosts.getJSONObject(position); String blogUrl = jsonPost.getString("url");

        Intent intent = new Intent(this, BlogWebViewActivity.class);
        intent.setData(Uri.parse(blogUrl));
        startActivity(intent);
    }
    catch (JSONException e) {
        logException(e);
    }
}
adnanalvee2
adnanalvee2
6,862 Points

I 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
Ben Jakuben
Treehouse Teacher

Ah. In that case just add a switch statement for position and start the new Intent based on that. Something like this:

Intent intent;
switch(position) {
  case 0:
    // Walmart Activity
    intent = new Intent(this, WalmartActivity.class);
    break;
  case 1:
    // Kroger Activity
    intent = new Intent(this, KrogerActivity.class);
    break;
  case 2:
    // Walgreens Activity
    intent = new Intent(this, WalgreensActivity.class);
    break;
}
startActivity(intent);