1

I want a ListView which displays name and info about each item using simple_list_item_2, so it looks something like this:

SWITZERLAND
a country with many delicious cheeses

BRITAIN
famous for its clocktower

AUSTRALIA
in Australia, everything is upside down!

The text in caps is meant to represent the large black text in each ListView element and the lower caps text represents the grey, small type of that element.

However, I want the user to be able to add his own countries, so the first element would show up as

ADD NEW COUNTRY...

Without any small-type text. I can use simple_list_item_2 and simply set the small text to "", but this aligns the large text awkwardly. I want this special "add new country" element to look exactly like simple_list_item_1.

However, the decision to use simple_list_item_1 or simple_list_item_2 is made in the initialization of my adapter:

MyAdapter adapter = new MyAdapter(
            this,
            android.R.layout.simple_list_item_2, 
            myListOfListViewElements);

And then this decision is realized when I populate the ListView:

myListView.setAdapter(adapter);

So how can I use both kinds of simple_list_item in one ListView?

Superbest
  • 25,318
  • 14
  • 62
  • 134

2 Answers2

0

You could always your "Add new country" as a header view by calling addHeaderView(View view) on the ListView before you call setAdapter(adapter) / setListAdapter(adapter). Attach an onClick listener to that header view to capture any clicks

That way your "Add new country" will show as the first entry in your listview

dymmeh
  • 22,247
  • 5
  • 53
  • 60
0

For the specific case where you're dealing with just a single 'special' item, @dymmeh's answer is definitely a good and easy to implement approach. For the more generic case, where you need to deal with multiple different items that need not necessarily sit at the top of the ListView, you can refer to the following two methods in an adapter:

  • getItemViewType(int position)
  • getViewTypeCount()

Based on the return value of the first method, Android will feed you the desired convertView in the adapter's getView(...) method. A common approach is to simply return the layout ids as type (e.g. R.layout.row_layout) , but you can really use anything you like, provided the values uniquely identify a type.

After that, you'll need to do a little bit of extra work to inflate the correct layouts when constructing your ViewHolder/RowWrapper. For each view type, you'll need to pick the right layout if convertView == null. There will be at least one view type (which is the default case) and at most getViewTypeCount(). An example illustrating the idea can be found here.

More details on the view type methods:

On a side note: I'd actually consider not making the 'add...' option part of the ListView, but rather position it on top of or above it so that it's conveniently accessible; i.e. no need to scroll back to the top first.

Community
  • 1
  • 1
MH.
  • 45,303
  • 10
  • 103
  • 116