solalito
solalito

Reputation: 1209

Change the appearance of only one ListView items

In my ListView, I want the first item to have a special style, while the other items have the regular layout called by the adapter:

    myAdapt = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, list);

    // Set Appearance of item 1 here
    //

    myListView.setAdapter(myAdapt);

Is there a way to do that? The item cannot be added via a TextView using the addHeaderView method because I might have to delete it at some point, which the adapter will not let me do (if there's actually a way to do that, shoot!).

Upvotes: 1

Views: 112

Answers (2)

Yakiv Mospan
Yakiv Mospan

Reputation: 8224

What you actually need to do is to create your Custom adapter that will support different item types. Than you can do whatever you want with your items : add or remove them from list and call notifyDataSetChanged()

Upvotes: 0

nKn
nKn

Reputation: 13761

Probably the best place where you can customize this is extending an ArrayAdapter and doing so in the getView() method.

This way, you get the layout in the second parameter (usually called convertView, but not necessarily) - basically the row's layout, and the first one (called usually position), references to the current position of the current row. You could do something like this:

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
  ...

  if (position == 0)
    convertView.setBackground(R.id.first_row);
  else
    convertView.setBackground(R.id.any_other_row);

  ...
}

Upvotes: 4

Related Questions