Android SimpleCursorAdapter多色行

时间:2015-01-05 08:03:25

标签: android simplecursoradapter

如何为此SimpleCursorAdapter实现多色行,或者如何添加getview?我希望用多种颜色查看我的列表视图。如果我在我的setViewBinder中添加它不能与行一起工作它只适用于文本项目背景而不是完整列表视图行

我的java是:

  @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());

}

我的颜色代码是:

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

1 个答案:

答案 0 :(得分:1)

尝试使用view.getParent()中的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);
      }
}