2

I have achieved something similar for particular items within my ListView in this question.

However, now, I want the user to be able to click on items, which will essentially highlight any item the user has tapped. I keep a list of the tapped items in my application, and should be able to reference this from the ViewBinder when scrolling occurs, however, since I am never adapting anything to the LinearLayout that makes up the ListView itself, I am not sure how to get the LinearLayout object in order to properly set its background color.

I need to be able to do this, because as you may know, when scrolling, Android re-uses the list items, which is fine unless you are changing formatting (coloring, etc.) of individual list items. In that case, you end up with list items that are colored/formatted incorrectly. I am using a ViewBinder to solve the issue when it comes to the TextViews within my list items, but do not know how to do something similar for the background of the ListView itself.

Community
  • 1
  • 1
finiteloop
  • 4,444
  • 8
  • 41
  • 64
  • 1
    To make sure I understand... you just want to change the background of individual ListView items? You know about making a custom adapter and overriding getView to provide a custom view for the individual items? – Nathan Fig Jul 05 '11 at 18:09
  • @Nathan Fig, I know how to change the background, however I want to know how to ensure that the background remains as it should as the user scrolls up and down the list. I know custom adapters are possible, but haven't attempted doing it. I thought doing a SimpleCursorAdapter with a ViewBinder would be a better approach. Maybe I should reconsider? If so, you are welcome to post it as an answer, with an explanation of how to do that. It sounds like a potentially viable solution to my problem. – finiteloop Jul 05 '11 at 18:11
  • +1 On the custom adapter. Don't be afraid. – dmon Jul 05 '11 at 18:23
  • Looks great. I will be trying this within the next couple o days. – finiteloop Jul 06 '11 at 10:33

2 Answers2

1

Create a custom row layout- e.g. custom_row.xml, and arrange any views needed, just as you would in a normal layout for an activity (so in this case you would probably provide a textview for the text, and perhaps an icon to the left of it).

Then create your custom adapter by extending an existing adapter, and override the getView method as such. Here's an example that uses a layout custom_row with both a title and subtitle:

class CustomAdapter<T> extends ArrayAdapter<T> {

/** List item title */
protected TextView mTitle;
/** List item subtitle */
protected TextView mSubtitle;

/**
 * @param context
 *            Current context
 * @param items
 *            Items being added to the adapter
 */
public CustomAdapter(final Context context, final List<T> items) {
    super(context, R.layout.custom_row, items);
}

/** Construct row */
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        final LayoutInflater li = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        view = li.inflate(R.layout.custom_row, null);
    }
    mTitle = (TextView) view.findViewById(R.id.custom_row_title);
    mSubtitle = (TextView) view.findViewById(R.id.custom_row_subtitle);
    return view;
}
}

As demonstrated, you can grab the items specified in the custom_row layout you created through the inflater service. Then you can manipulate the objects as needed.

Nathan Fig
  • 14,970
  • 9
  • 43
  • 57
  • This looks great. I will try this soon and come back to follow up. Thanks! – finiteloop Jul 06 '11 at 10:33
  • Does using getview eliminate the need for the view binder, or is this used in addition to it? – finiteloop Jul 06 '11 at 11:42
  • I just noticed in your other question you posted you were using a SimpleCursorAdapter, so you might subclass that instead of ArrayAdapter as used in my example (especially if you're going to keep using ViewBinder). Never having used ViewBinder before- I think you could eliminate the need for it, or else might have to at least override bindView() since you're using a custom view. But you can probably leave ViewBinder altogether and just override setViewText() as suggested by CommonsWare in his answer to your other question. – Nathan Fig Jul 06 '11 at 12:32
  • I do not have access to my code at the moment, but if I remember correctly, I override bindView in my ViewBinder. If this exists within the adapter as well, it sounds like I should be able to just move it into the adapter. It is still unclear to me the point at which I can determind what color to set the background to. In BindView I don't have access to the list item, and in getView I dont have access to the item (in this case, the cursor) that will be bound to the view. Or am I missing something? – finiteloop Jul 06 '11 at 12:59
  • Ah, just noticed the getItem(int position) function call. I should be able to use that within the getView function to determine what background to apply to the text. – finiteloop Jul 06 '11 at 13:02
  • A bit of a style question, should I apply coloring and formatting in bindView or in getView? Should bindView just add text to the fields, or should it format as well? My intuition is to format everything in getView, since that is where I have to set the background for the list item, – finiteloop Jul 06 '11 at 13:04
  • My intuition would be the same, but I've never worked with bindView before. However, you might consider using something like a statelist drawable for the background, and just switch between already defined background states. See http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList – Nathan Fig Jul 06 '11 at 19:40
  • This is essentially what I did. I created a custom CursorAdapter and overrode newView and bindView. – finiteloop Jul 11 '11 at 18:06
0

I believe one way is to use a state list where the default state has a transparent background, and the selected state has the background you want.

For example, see Romain Guy's answer here: Changing background color of ListView items on Android

See also: Android ListView State List not showing default item background

Community
  • 1
  • 1
Lorne Laliberte
  • 6,261
  • 2
  • 35
  • 36