简体   繁体   中英

Android SimpleCursorAdapter multicolor row

How can i implement multicolor row for this SimpleCursorAdapter or how can i add getview? i want view my list view with multiple color. if i add it in my setViewBinder not work with row it only work with text item background not full list view row

My java is:

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) {     
    setHasOptionsMenu(true);
     return inflater.inflate(R.layout.media_select, container, false);
}

@Override 
public void onViewCreated (View view, Bundle savedInstanceState) {

    try {
        mAdapter = new SimpleCursorAdapter(
                getActivity(),

                R.layout.media_select_row,
                createCursor(""),
                new String[] {
                    MediaStore.Audio.Media.ARTIST,
                    MediaStore.Audio.Media.ALBUM,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media._ID},
                    new int[] {
                    R.id.row_artist,
                    R.id.row_album,
                    R.id.row_title,
                    R.id.row_icon,
                    R.id.row_options_button});

        setListAdapter(mAdapter);

        getListView().setItemsCanFocus(true);
        getListView().setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent,
                    View view,
                    int position,
                    long id) {
                 Cursor c = mAdapter.getCursor();
                 String title = c.getString(c.getColumnIndexOrThrow(
                         MediaStore.Audio.Media.TITLE));

            }                         
        });

    } catch (SecurityException e) {
    } catch (IllegalArgumentException e) {
    }


    mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view,
                Cursor cursor,
                int columnIndex) {
            if (view.getId() == R.id.row_options_button){
                ImageView iv = (ImageView)view;
                iv.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        getActivity().openContextMenu(v);
                    }
                });
                return true;
            } else if (view.getId() == R.id.row_icon) {
                setSoundIconFromCursor((ImageView) view, cursor);
                return true;
            }

            return false;
        }
    });
    registerForContextMenu(getListView());

}

And my color code is:

      if (position % 2 == 0) {
            view.setBackgroundResource(R.drawable.list_bg2);
        } else {
            view.setBackgroundResource(R.drawable.list_bg);
        }

try it as using view.getParent() in setViewValue :

public boolean setViewValue(View view,Cursor cursor,
                int columnIndex) {
     if (position % 2 == 0) {
        ((ViewGroup)view.getParent()).setBackgroundResource(R.drawable.list_bg2);
      } else {
        ((ViewGroup)view.getParent()).setBackgroundResource(R.drawable.list_bg);
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM