In my last article, I have shown you a basic example of intent. In this article, I will show you a more practical example of Intents. Scenario is that a list item itself contains more information in the form of a list.
When the list item is selected, it should open another list.
In the customized list example, I showed you a list with your favorite cartoon characters. Once you select a character, it should open another list with related characters.
ListView
layout will hold the main characters.
cartoon_list_view.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/taskListParent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>
List item layout contains just a text field. We will dynamically set the cartoon’s image using setCompoundDrawablesWithIntrinsicBounds
.
cartoon_list_item_view.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" /> </LinearLayout>
Our model contains Cartoon
bean. It has reference to the image resource and the related cartoons object.
Cartoon:
package com.javarticles.android; import java.util.List; public class Cartoon { private String mName; private int mCartoonPicRes; private List<Cartoon> mOtherCartoons; public Cartoon(String name, int picRes, List<Cartoon> otherCartoons) { mName = name; mCartoonPicRes = picRes; mOtherCartoons = otherCartoons; } public Cartoon(String name, int picRes) { mName = name; mCartoonPicRes = picRes; } public String getName() { return mName; } public int getCartoonPicRes() { return mCartoonPicRes; } public List<Cartoon> getOtherCartoons() { return mOtherCartoons; } }
In the getView
, we set the cartoon image using setCompoundDrawablesWithIntrinsicBounds
.
CartoonListAdapter:
package com.javarticles.android; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; public class CartoonListAdapter extends ArrayAdapter<Cartoon> { private List<Cartoon> cartoonItems; private Context context; public CartoonListAdapter(Context context, int textViewResourceId, List<Cartoon> cartoonItems) { super(context, textViewResourceId, cartoonItems); this.context = context; this.cartoonItems = cartoonItems; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { LayoutInflater vi = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = vi.inflate(R.layout.cartoon_list_item_view, null); } Cartoon cartoon = cartoonItems.get(position); if (cartoon != null) { // name TextView nameTextView = (TextView) view.findViewById(R.id.name); nameTextView.setText(cartoon.getName()); nameTextView.setCompoundDrawablesWithIntrinsicBounds(0, cartoon.getCartoonPicRes(), 0, 0); } return view; } }
CartoonListActivity
represents the main list view. Let’s go through the steps followed in onCreate
.
- Call
setContentView
to set the list view’s layout. - Create
CartoonListAdapter
adapter and set it to the list view usingsetListAdapter
. This contains the list ofCartoon
objects to display. - Set an item listener using
listView.setOnItemClickListener
to start the list activity that will display the related cartoons. - In the item listener callback,
OnItemClickListener.onItemClick
, we explicitly pass in the related list activity classOtherCartoonsListActivity
toIntent
during its creation.Intent intent = new Intent(CartoonListActivity.this, OtherCartoonsListActivity.class);
- In the
Intent
object, we put in an extra value, to let the related list activity know which cartoon was selected. - The extra value contains “cartoon” as key and name of the selected cartoon as the value.
intent.putExtra("cartoon", cartoonSelected.getName());
- We call
startActivity(intent)
to start the related list activity.
CartoonListActivity:
package com.javarticles.android; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; public class CartoonListActivity extends ListActivity { private CartoonListAdapter cartoonListAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cartoon_list_view); cartoonListAdapter = new CartoonListAdapter(this, R.layout.cartoon_list_item_view, new ArrayList(CARTOONS.values())); setListAdapter(cartoonListAdapter); ListView listView = getListView(); listView.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(CartoonListActivity.this, OtherCartoonsListActivity.class); Cartoon cartoonSelected = cartoonListAdapter.getItem(position); intent.putExtra("cartoon", cartoonSelected.getName()); startActivity(intent); finish(); }}); } public static List getOtherCartoonList(String character) { Cartoon cartoon = CARTOONS.get(character); return cartoon.getOtherCartoons(); } private static final Map<String, Cartoon> CARTOONS = new HashMap<String, Cartoon>(); static { List tintinCartoons = new ArrayList(4); tintinCartoons.add(new Cartoon("Snowy", R.drawable.snowy_small)); tintinCartoons.add(new Cartoon("Captain Haddock", R.drawable.captain_haddock_small)); tintinCartoons.add(new Cartoon("Thomson & Thopson", R.drawable.thomson_thompson_small)); tintinCartoons.add(new Cartoon("Professor Calculus", R.drawable.professor_calculus_small)); Cartoon tintin = new Cartoon("Tintin", R.drawable.tintin_thumb, tintinCartoons); List asterixCartoons = new ArrayList(4); asterixCartoons.add(new Cartoon("Obelix", R.drawable.obelix_small)); asterixCartoons.add(new Cartoon("Bicarbonatofsoda", R.drawable.bicarbonatofsoda_small)); asterixCartoons.add(new Cartoon("Idefix", R.drawable.idefix_small)); asterixCartoons.add(new Cartoon("Troubadix", R.drawable.troubadix_small)); Cartoon asterix = new Cartoon("Asterix", R.drawable.asterix_thumb, asterixCartoons); CARTOONS.put("Tintin", tintin); CARTOONS.put("Asterix", asterix); } }
In OtherCartoonsListActivity
, we get the Intent
object passed in from the parent list activity and then get the extra value to know the cartoon selected.
Intent intent = getIntent(); String cartoon = intent.getStringExtra("cartoon");
Once we know the cartoon selected, we will get its related cartoons, create the adapter and set it to the list view.
List cartoonList = CartoonListActivity.getOtherCartoonList(cartoon); cartoonListAdapter = new CartoonListAdapter(this, R.layout.cartoon_list_item_view, cartoonList); setListAdapter(cartoonListAdapter);
OtherCartoonsListActivity:
package com.javarticles.android; import java.util.List; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; public class OtherCartoonsListActivity extends ListActivity { private CartoonListAdapter cartoonListAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // set the layout of the Activity setContentView(R.layout.cartoon_list_view); // get the intent that we have passed from ActivityOne Intent intent = getIntent(); // get the extra value String cartoon = intent.getStringExtra("cartoon"); Log.d("otherCartoon", "Get other cartoons for " + cartoon); List cartoonList = CartoonListActivity.getOtherCartoonList(cartoon); cartoonListAdapter = new CartoonListAdapter(this, R.layout.cartoon_list_item_view, cartoonList); setListAdapter(cartoonListAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); switch (id) { case R.id.back: startActivity(new Intent(this, CartoonListActivity.class)); finish(); } return super.onOptionsItemSelected(item); } }
To return back to the main list, you need to click on the back button from the action bar.
case R.id.back: startActivity(new Intent(this, CartoonListActivity.class)); finish();
Let’s launch the app and test it.
The first page is the main List:
On selecting Asterix, related characters of Asterix are shown in the child list:
On selecting TinTin, related characters of Tintin are shown in the child list:
Download Source Code
In this article, I showed you how to use intent to display child lists.
intentExample.zip