1

我有一个用于 ListView 的 SimpleCursor Adapter 并想要实现一个 Checkbox。一切正常,只是在我的 ListView 上随机(镜像)检查了复选框。我知道它与回收的 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 布局,并尝试将复选框状态保存到一个数组中(使用 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;
}
4

0 回答 0