Setting the first item in the listview as default for android

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;
}

Answers


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.


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));
}

Need Your Help

problem with same search tab in multiple html pages

javascript jquery html search-engine dhtml

I have some ten html pages each page has same search tab, when i click search button from any of the page it navigates to index page with search text passed through query string and displays output...

Differentiating between Return and numpad Enter

actionscript-3 flash input actionscript air

I am having some trouble working out how to differentiate the Return key on my keyboard and the Enter key located on the num pad.