ListView SimpleCursorAdapter复选框

时间:2013-09-11 21:23:50

标签: android listview checkbox simplecursoradapter

我有一个ListView的SimpleCursor适配器,想要实现一个Checkbox。除了在ListView上随机(镜像)选中Checkbox之外,一切正常。 我知道它与回收的ListView行有关,但我不能让它工作。这是我的代码:

private void displayListView() {


Cursor cursor = dbHelper.fetch...

// The desired columns to be bound
String[] columns = new String[] {
ProduktAdapter.KEY_CODE,
ProduktAdapter.KEY_NAME,
ProduktAdapter.KEY_CONTINENT,
ProduktAdapter.KEY_REGION,
ProduktAdapter.KEY_PHOTO
};

// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.code,
R.id.name,
R.id.continent,
R.id.region,
R.id.list_image
};



// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.produkt_info,
cursor,
columns,
to,
0);

dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

   public boolean setViewValue(View view, Cursor cursor,
           int columnIndex) {
       if (view.getId() == R.id.list_image) {
           String _name = cursor.getString(columnIndex);

               File file = new File(Environment.getExternalStorageDirectory()

                        .......

           Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

           ((ImageView) view).setImageBitmap(bitmap);


           return true;   


       } else {
           return false;
       } 
   }});


final ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);  
listView.setAdapter(dataAdapter); 

我有一个CHOICE_MODE_MULTIPLE布局,并试图将Checkbox State保存到一个数组(使用getView),但仍然没有让它工作。 (类似的东西):

public View getView(final int pos, View inView, ViewGroup parent) { 

    if (inView == null) { 
       LayoutInflater inflater = (LayoutInflater) context 
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       inView = inflater.inflate(R.layout.kuehlschrankliste, null); 
    } 

    final CheckBox cBox = (CheckBox) inView.findViewById(R.id.check); 
    cBox.setOnClickListener(new OnClickListener() { 

       public void onClick(View v) { 

          if(cBox.isChecked()) { 
              itemChecked.set(pos, true); 
              // do some operations here 
          }   
          else{ 
              itemChecked.set(pos, false); 
              // do some operations here 
          }
      }
  });
    cBox.setChecked(itemChecked.get(pos));
           return inView;
}