在Listview中检索已检查CheckBoxes的项目

时间:2010-02-14 01:19:35

标签: android checkbox android-listview

我有ListActivity,我正在使用自定义CursorAdapter。

在列表的每个项目中,我还有复选框......

现在,当我按下它时,我的列表屏幕上有一个烫发按钮,

它应该找到所有“已选中”的复选框,并对项目执行某些操作,并选中复选框。

如何检索所有已检查的?

我已经完成了focusable:false,所以我可以使用OnClickListener,但我不知道那么有多远 这个.. 谢谢,

一些代码: 这是我的ListActivity类:

 final String columns[] = new String[] { MyUsers.User._ID,
                MyUsers.User.MSG, MyUsers.User.LOCATION };

          int[] to = new int[] { R.id.toptext,   R.id.bottomtext,R.id.ChkBox,
                R.id.Location};

          Uri myUri = Uri.parse("content://com.idan.datastorageprovider/users");

          Cursor cursor = getContentResolver().query(myUri, columns, null, null, null);  

          startManagingCursor(cursor);  

           ListCursorAdapter myCursorAdapter=new ListCursorAdapter(this,R.layout.listitem, cursor, columns, to);

           this.setListAdapter(myCursorAdapter);

这是我的Custom Cursor适配器类:

public class ListCursorAdapter extends SimpleCursorAdapter

{

         private Context context;
    private int layout;

    public ListCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to)
    {
        super(context, layout, c, from, to);

        this.context = context;

        this.layout = layout;

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent)
    {

        Cursor c = getCursor();

        final LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(layout, parent, false);
                   return v;
          }

@Override

    public void bindView(View v, Context context, Cursor c)
    {
        TextView topText = (TextView) v.findViewById(R.id.toptext);
        if (topText != null)
        {
            topText.setText("");
        }

        int nameCol = c.getColumnIndex(MyUsers.User.MSG);
        String name = c.getString(nameCol);
        TextView buttomTxt = (TextView) v.findViewById(R.id.bottomtext);
        if (buttomTxt != null)
        {
            buttomTxt.setText("Message: "+name);
        }

        nameCol = c.getColumnIndex(MyUsers.User.LOCATION);
        name = c.getString(nameCol);
        TextView location = (TextView) v.findViewById(R.id.Location);
        if (locationLinkTxt != null)
        {
            locationLinkTxt.setText(name);
        }

        }

1 个答案:

答案 0 :(得分:0)

        //Modify to meet your needs
        String[] from = new String[]{ YourDbAdapter.KEY_WORDS_ROWID, YourDbAdapter.KEY_WORDS_ROWID};
        int[] to = new int[]{ R.id.row_item_checkbox};

        mAdapter = new SimpleCursorAdapter(this, R.layout.row_item_with_checkbox, yourDbCursor, from, to);


      mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

       @Override
       public boolean setViewValue(View view, Cursor cursor, int columnIndex) {


        if(view.getId() == R.id.myCheckbox )
        { 

         CheckBox cb = (CheckBox) view;
         cb.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {

           final long id = cursor.getLong(columnIndex); //Your row id
                               //You can do the necessary work here such as adding to an List or somehting

          }
         });


         return true; 
        } 

        return false;
       }


      });