开发者

Show default selection color for custom listview

I have a listview with a custom BaseAdapter. Each row of the listview has a TextView and a CheckBox.

The problem is when I click (or touch) any row, the textview foreground becomes gray, instead of the default behavior (background -> green, textview foreground -> white).

Here is the code:

row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                style="@style/layout">


    <TextView android:id="@+id/main_lv_item_textView" 
              style="@style/textViewBig"
              android:layout_alignParentLeft="true"/>


    <CheckBox android:id="@+id/main_lv_item_checkBox" 
              style="@style/checkBox"
              android:layout_width="wrap_content"
              android:layout_alignParentRight="true"/>

</RelativeLayout>

Custom Adapter:

public class CustomAdapter extends BaseAdapter {
        private List<Profile> profiles;
        private LayoutInflater inflater;
        private TextView tvName;
        private CheckBox cbEnabled;

        public CustomAdapter(List<Profile> profiles) {
            this.profiles = profiles;
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public int getCount() {
            return profiles.size();
        }

        public Object getItem(int position) {
            return profiles.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            View row = inflater.inflate(R.layout.main_lv_item, null);

            final Profile profile = profiles.get(position);
            tvName = (TextView) row.findViewById(R.id.main_lv_item_textView);
            registerForContextMenu(tvName);
            cbEnabled = (CheckBox) row.findViewById(R.id.main_lv_item_checkBox);
            tvName.setText(profile.getName());
            if (profile.isEnabled()) {
                cbEnabled.setChecked(true);
            }

            tvName.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString(PROFILE_NAME_KEY, profile.getName());
                    Intent intent = new Intent(context, GuiProfile.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });

            tvName.setOnLongClickListener(new OnLongClickListener() {
                public boolean onLongClick(View v) {
                    selectedProfileName = ((TextView) v).getText().toString();
                    return false;
                }
            });

            cbEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (!profile.isEnabled()) {
                        for (Profile profile : profiles) {
                            if (profile.isEnabled()) {
                                profile.setEnabled(false);
            开发者_Go百科                    Database.getInstance().storeProfile(profile);
                            }
                        }
                    }

                    profile.setEnabled(isChecked);
                    Database.getInstance().storeProfile(profile);
                    updateListView();
                }
            });

            return row;
        }
    }

Any help would be appreciated.


Actually, what is happening is not a bug, but an unfortunate typing issue in Android. When you set the background color to R.color.black you set it to an id rather than an actual color. Since colors are just ints, and ids are just ints, it didn't know the difference, and interpreted it to be a color. The actual way to do this is to set it to a color obtained from the Resources class like this:

int black = activity.getResources().getColor(android.R.color.black);
view.setBackgroundColor(black);

Hope that's helpful.


You didn't post the code you use to set the color, however I ran into the same problem and the solution was to do a

setBackgroundColor(0xFF000000); 

instead of a

setBackgroundColor(R.color.black);  

Which appears to be a bug in Android since R.color.black is not gray. Also all of that setup code is getting called many times and only needs to be done once. The argument convertView will be null the first time - thats when you inflate. After that just use convertView.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜