0

I've tried looking online for a good solution to this but I can't seem to find one.

There's a listview of accounts. A selected account is indicated by a lighter blue color for its background but only when pressed. This is where I got the code from: Android ListView selected item stay highlighted

In the onCreate method, I have an onItemClickListener method and if I do listview.setSelection(0); above the onItemClick method, the default item is the first item in the listview. The system knows that the very first account in the list is the current item that is selected but how do I show it visually?

Essentially, I want to somehow do view.setSelected(true); for the first item in the listview.

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parentAdapter,
                    View view, int position, long id) {
                view.setSelected(true);
                Account account= accounts.get(position);
                accountNumber = account.getAccountNumber();
            }
        });

Thank you!

Edit: Here's the getView from our custom adapter

public final View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    rowView = inflater.inflate(rowResourceId, parent, false);
    textView = (TextView) rowView.findViewById(R.id.textView);
    textView.setTextColor(Color.BLACK);
    textView.setText(objects.get(position).debug());
    return rowView;
}
Community
  • 1
  • 1
user1883614
  • 905
  • 3
  • 16
  • 30

2 Answers2

1

The correct way would be to have a custom array adapter and to override the getView method. For the first item you would set the background:

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

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    rowView = inflater.inflate(rowResourceId, parent, false);

    if(position == 0){
        //you might remove this check if your develop for new api's only
        int sdk = android.os.Build.VERSION.SDK_INT;
        if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            rowView.setBackgroundDrawable();
        } else {
            rowView.setBackground();
        }
    }

    textView = (TextView) rowView.findViewById(R.id.textView);
    textView.setTextColor(Color.BLACK);
    textView.setText(objects.get(position).debug());

    return rowView;
}

Read more about it here: Vogella - Using lists in android This solution might be better as forcing the selection. Where selection is an action that is performed by a user, you actually just want to highlight the first row, so manipulate that in your get view method.

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • But see, my view.setSelected takes care of changing the background color for me... and we are using a custom adapter. I'll post the code for the current getView right now. – user1883614 Apr 12 '14 at 19:37
  • @user1883614 right, don't force the selection, in your getView method you might try what I suggested in my answer, for position 0 set the background – DarkLeafyGreen Apr 12 '14 at 19:39
  • What is rowlayout? getResources()? and drawable.ready? Sorry, eclipse is marking these as errors... – user1883614 Apr 12 '14 at 19:45
  • Huh, that almost worked! Thank you! I'll play around with it a bit more to get it to exactly what I need. – user1883614 Apr 12 '14 at 19:52
  • Figured it out but I can't post it for a few hours, but I'll post my solution then! Thank you! – user1883614 Apr 12 '14 at 20:20
0

So I cheated a bit in terms of code and hard-coded it but it works now!

For getView in the custom adapter:

if (position == 0) {
        textView.setBackgroundColor(Color.rgb(173, 211, 224));
}

In the onCreate method of the actual activity, for setOnItemClickListener:

if (position != 0) {
View rowView = listview.getChildAt(0);
    TextView textView = (TextView) rowView.findViewById(R.id.textView);
textView.setBackgroundColor(Color.rgb(227, 236, 239));
} else {
    View rowView = listview.getChildAt(0);
    TextView textView = (TextView) rowView.findViewById(R.id.textView);
textView.setBackgroundColor(Color.rgb(173, 211, 224));
}
user1883614
  • 905
  • 3
  • 16
  • 30