保存的Andr​​oid国家复选框的ListView中使用光标适配器光标、适配器、复选框、国家

2023-09-12 21:50:50 作者:美痞

我不能找到一种方法使用游标适配器时保存复选框状态。其他一切工作正常,但如果我点击复选框是重复其回收时。我见过使用数组适配器的例子,但因为我缺乏经验的IM发现很难把它翻译成使用游标适配器。可能有人给我如何去做一个例子。任何帮助AP preciated。

 私有类PostImageAdapter扩展的CursorAdapter {

    私有静态最终诠释S = 0;
    私人诠释布局;
    位图BM = NULL;
    私人字符串PostNumber;
    TourDbAdapter mDbHelper;


    公共PostImageAdapter(上下文的背景下,INT布局,光标C,的String []从,INT []键,字符串postid){

        超(背景下,C);
        this.layout =布局;
        PostNumber = Postid;

     mDbHelper =新TourDbAdapter(上下文);
     mDbHelper.open();

    }

    @覆盖
    公共查看NewView的(上下文的背景下,最终的光标C,父母的ViewGroup){

     ViewHolder持有人;

     LayoutInflater充气= getLayoutInflater();
     查看排= inflater.inflate(R.layout.image_post_row,NULL);

   持有人=新ViewHolder();

   holder.Description =(TextView中)row.findViewById(R.id.item_desc);
   holder.cb =(复选框)row.findViewById(R.id.item_checkbox);
   holder.DateTaken =(TextView中)row.findViewById(R.id.item_date_taken);
   holder.Photo =(ImageView的)row.findViewById(R.id.item_thumb);

   row.setTag(保持器);

 INT DateCol = c.getColumnIndex(TourDbAdapter.KEY_DATE);
 字符串日期= c.getString(DateCol);

 INT DescCol = c.getColumnIndex(TourDbAdapter.KEY_CAPTION);
 字符串描述= c.getString(DescCol);

 INT FileNameCol = c.getColumnIndex(TourDbAdapter.KEY_FILENAME);
 最后字符串文件名= c.getString(FileNameCol);

 INT PostRowCol = c.getColumnIndex(TourDbAdapter.KEY_Post_ID);
 字符串的RowID = c.getString(PostRowCol);

 字符串路径=SD卡/ Tourabout /大拇指/+文件名+.JPG;
 位图BM = BitmapFactory.de codeFILE(路径,NULL);

 holder.Photo.setImageBitmap(BM);
 holder.DateTaken.setText(日期);
 holder.Description.setText(说明);

 holder.cb.setOnClickListener(新OnClickListener(){
    @覆盖
 公共无效的onClick(视图v){
    复选框的CBox =(复选框)V;
    如果(cBox.isChecked()){

      mDbHelper.UpdatePostImage(文件名,PostNumber);

    }
    否则如果(!cBox.isChecked()){
      mDbHelper.UpdatePostImage(文件名,);

    }

  }
});
返回行;

};

    @覆盖
    公共无效bindView(查看行,上下文的背景下,最终的光标C){

     ViewHolder持有人;
     支架=(ViewHolder)row.getTag();

      INT DateCol = c.getColumnIndex(TourDbAdapter.KEY_DATE);
         字符串日期= c.getString(DateCol);

         INT DescCol = c.getColumnIndex(TourDbAdapter.KEY_CAPTION);
         字符串描述= c.getString(DescCol);

         INT FileNameCol = c.getColumnIndex(TourDbAdapter.KEY_FILENAME);
      最后字符串文件名= c.getString(FileNameCol);

      INT PostRowCol = c.getColumnIndex(TourDbAdapter.KEY_Post_ID);
         字符串的RowID = c.getString(PostRowCol);

      字符串路径=SD卡/ Tourabout /大拇指/+文件名+.JPG;
         位图BM = BitmapFactory.de codeFILE(路径,NULL);

        文件X = NULL;

         holder.Photo.setImageBitmap(BM);
         holder.DateTaken.setText(日期);
         holder.Description.setText(说明);

         holder.cb.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
         复选框的CBox =(复选框)V;
         如果(cBox.isChecked()){

               mDbHelper.UpdatePostImage(文件名,PostNumber);

         }
         否则如果(!cBox.isChecked()){
               mDbHelper.UpdatePostImage(文件名,);

         }

        }
       });

    }

}

静态类ViewHolder {
  TextView的说明;
  ImageView的照片;
  复选框CB;
  TextView的DateTaken;
}
}
 

解决方案

我会建议你使用Android的内置的多项选择列表的支持( CHOICE_MODE_MULTIPLE )。

的List11.java SDK示例演示了这一点。您也可以找到我的教程一个使用项目它这里。

您仍可以使用此技术与自己的布局,只要你有一个 CheckedTextView 机器人:ID =@机器人:ID / text1中的如图所示 android.R.layout.simple_list_item_multiple_choice 资源中,它随你的SDK,副本

此外,请参阅this问题和这个问题和this问题和这个问题。

I cant find a way to save the checkbox state when using a Cursor adapter. Everything else works fine but if i click on a checkbox it is repeated when it is recycled. Ive seen examples using array adapters but because of my lack of experience im finding it hard to translate it into using a cursor adapter. Could someone give me an example of how to go about it. Any help appreciated.

private class PostImageAdapter extends CursorAdapter {

    private static final int s = 0;
    private int layout;
    Bitmap bm=null;
    private String PostNumber;
    TourDbAdapter mDbHelper;


    public PostImageAdapter (Context context, int layout, Cursor c, String[] from, int[] to, String Postid) {

        super(context, c);
        this.layout = layout;
        PostNumber = Postid;

     mDbHelper = new TourDbAdapter(context);
     mDbHelper.open();

    }

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

     ViewHolder holder;

     LayoutInflater inflater=getLayoutInflater();
     View row=inflater.inflate(R.layout.image_post_row, null);       

   holder = new ViewHolder();

   holder.Description = (TextView) row.findViewById(R.id.item_desc);
   holder.cb = (CheckBox) row.findViewById(R.id.item_checkbox);
   holder.DateTaken = (TextView) row.findViewById(R.id.item_date_taken);
   holder.Photo = (ImageView) row.findViewById(R.id.item_thumb);

   row.setTag(holder);

 int DateCol = c.getColumnIndex(TourDbAdapter.KEY_DATE);
 String Date = c.getString(DateCol);

 int DescCol = c.getColumnIndex(TourDbAdapter.KEY_CAPTION);
 String Description = c.getString(DescCol);    

 int FileNameCol = c.getColumnIndex(TourDbAdapter.KEY_FILENAME); 
 final String FileName = c.getString(FileNameCol);

 int PostRowCol = c.getColumnIndex(TourDbAdapter.KEY_Post_ID); 
 String RowID = c.getString(PostRowCol);

 String Path = "sdcard/Tourabout/Thumbs/" + FileName + ".jpg";    
 Bitmap bm = BitmapFactory.decodeFile(Path, null); 

 holder.Photo.setImageBitmap(bm);
 holder.DateTaken.setText(Date);
 holder.Description.setText(Description);

 holder.cb.setOnClickListener(new OnClickListener() {  
    @Override
 public void onClick(View v) {
    CheckBox cBox = (CheckBox) v;
    if (cBox.isChecked()) {

      mDbHelper.UpdatePostImage(FileName, PostNumber);

    } 
    else if (!cBox.isChecked()) {    
      mDbHelper.UpdatePostImage(FileName, "");

    }

  }
});
return row;

};

    @Override
    public void bindView(View row, Context context, final Cursor c) {  

     ViewHolder holder;
     holder = (ViewHolder) row.getTag();   

      int DateCol = c.getColumnIndex(TourDbAdapter.KEY_DATE);
         String Date = c.getString(DateCol);

         int DescCol = c.getColumnIndex(TourDbAdapter.KEY_CAPTION);
         String Description = c.getString(DescCol);    

         int FileNameCol = c.getColumnIndex(TourDbAdapter.KEY_FILENAME); 
      final String FileName = c.getString(FileNameCol);

      int PostRowCol = c.getColumnIndex(TourDbAdapter.KEY_Post_ID); 
         String RowID = c.getString(PostRowCol);

      String Path = "sdcard/Tourabout/Thumbs/" + FileName + ".jpg";    
         Bitmap bm = BitmapFactory.decodeFile(Path, null); 

        File x = null;

         holder.Photo.setImageBitmap(bm);
         holder.DateTaken.setText(Date);
         holder.Description.setText(Description);

         holder.cb.setOnClickListener(new OnClickListener() {  
        @Override
        public void onClick(View v) {
         CheckBox cBox = (CheckBox) v;
         if (cBox.isChecked()) {

               mDbHelper.UpdatePostImage(FileName, PostNumber);

         } 
         else if (!cBox.isChecked()) {    
               mDbHelper.UpdatePostImage(FileName, "");

         }

        }
       });

    }

}  

static class ViewHolder{
  TextView Description;
  ImageView Photo;
  CheckBox cb;
  TextView DateTaken;
}
}

解决方案

I would recommend you use Android's built-in support for multiple-choice lists (CHOICE_MODE_MULTIPLE).

The List11.java SDK sample demonstrates this. You can also find a project from one of my tutorials that uses it here.

You can still use this technique with your own layout, so long as you include a CheckedTextView with android:id="@android:id/text1" as shown in the android.R.layout.simple_list_item_multiple_choice resource, a copy of which ships with your SDK.

Also, see this question and this question and this question and this question.