2010-03-09 90 views
7

的ListView我不能找到一种方法,使用游标适配器时保存的复选框的状态。其他一切正常,但如果我点击复选框,它会在重复使用时重复。我见过使用阵列适配器的例子,但由于我缺乏经验,我发现很难将它翻译成使用游标适配器。有人能给我一个如何去做的例子。任何帮助赞赏。Android的保存复选框状态与光标适配器

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; 
} 
} 

回答

3

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

List11.java SDK示例演示了这一点。您还可以从我的教程中找到一个项目,使用它here

只要包含CheckedTextViewandroid:id="@android:id/text1",您仍然可以在自己的布局中使用此技术,如android.R.layout.simple_list_item_multiple_choice资源中所示,该资源的副本随SDK一起提供。

另外,参见this questionthis questionthis questionthis question

+1

我怎么会用我自己的布局? checkedtextview不需要成为行的顶层?我似乎找不到一种方法来添加我需要的视图,例如当我使用checkedtextview时,ImageView显示图像(来自相机的照片)。 – Ricardo 2010-03-10 01:01:54

+0

首先,'CheckedTextView'支持'android:drawableLeft'和kin,你可以使用它。第二,AFAIK,只要它具有正确的'android:id'(见上文),它可以在其他布局内(例如,也包含'ImageView'的LinearLayout')。 – CommonsWare 2010-03-10 02:38:32

+0

现在我似乎遇到了与此线程相同的问题 http://www.mail-archive.com/[email protected]/msg06941.html。我将如何从代码背后设置android:drawableLeft属性? – Ricardo 2010-03-10 04:35:25

4

我有同样的问题我自己:如何在切换自定义布局多选CheckedTextView(即不使用android.R.layout.simple_list_item_multiple_choice

以下为我工作。我的例子是一个自定义视图,它提供了一个从SimpleCursorAdapter扩展的适配器。

我的自定义视图(row_contact.xml):

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" android:layout_width="fill_parent"> 

    <CheckedTextView 
    android:id="@android:id/text1" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:gravity="center_vertical" 
    android:paddingLeft="6dip" 
    android:paddingRight="6dip" 
    android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
    /> 

    <TextView 
    android:text="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/tvNumber" 
    android:layout_gravity="bottom" 
    android:paddingLeft="6dip" 
    android:paddingRight="6dip" 
    /> 

</FrameLayout> 

适配器正在ListActivity.OnCreate创建,调用setupViews():

private void setupViews() { 
    bOk  = (Button) findViewById(R.id.ButtonOK); 
    bCancel = (Button) findViewById(R.id.ButtonCancel); 
    FListView = getListView(); 
    // 
    bOk.setText("Select"); 
    // 
    FListView.setItemsCanFocus(false); 
    FListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
    // 
    bOk.setOnClickListener(this); 
    bCancel.setOnClickListener(this); 
    // 
    ContentResolver content = getContentResolver(); 
    Cursor cursor = ApplicationHelper.MobilePhoneContacts(content); 
    startManagingCursor(cursor); 

    ListAdapter adapter = new CheckedCursorAdapter(SelectContacts.this, R.layout.row_contact, cursor,     
     new String[] {People.NAME, People.NUMBER},    
     new int[] {android.R.id.text1, R.id.tvNumber});   
    setListAdapter(adapter); 
    } 

的定义适配器:

public class CheckedCursorAdapter extends SimpleCursorAdapter { 

    Activity context; 
    Cursor c; 

    public CheckedCursorAdapter(Activity context, int rowContact, Cursor cursor, String[] strings, int[] is) { 
     super(context, rowContact, cursor, strings, is); 
     this.context = context; 
     this.c = cursor; 

    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     ContactRowWrapper wrapper; 

     if (row == null) { 
     LayoutInflater inflater=context.getLayoutInflater(); 
     row = inflater.inflate(R.layout.row_contact, null); 
     // 
     wrapper = new ContactRowWrapper(row); 
     row.setTag(wrapper); 
     } else { 
     wrapper = (ContactRowWrapper)row.getTag(); 
     } 
     c.moveToPosition(position); 
     wrapper.getTextView().setText(c.getString(c.getColumnIndex(Contacts.People.NUMBER))); 
     wrapper.getcheckBox().setText(c.getString(c.getColumnIndex(Contacts.People.NAME))); 
     wrapper.getcheckBox().setChecked(getListView().isItemChecked(position)); 
     // 
     return row; 
    } 

    } 

的代码对我来说,重要的一点是让复选框的工作是:

wrapper.getcheckBox().setChecked(getListView().isItemChecked(position)); 

希望这可以帮助你或任何其他人绊倒这个问题。

此外,请原谅我的Java noobness ......我才开始的Java几个星期前。

1

声明上getView法CheckBox和使用此代码

CheckBox checkBtn =(CheckBox) convertView.findViewById(R.id.phCheckBox); 
       checkBtn.setChecked(Boolean.parseBoolean((vList.get(position).getCheckBtnStatus()))); 

      checkBtn.setOnClickListener(new OnClickListener() 
       { 

        public void onClick(View arg0) 
        { 

         if(checkBtn.isChecked()==true) 
          { 
          vList.get(position).setCheckBtnStatus("true"); 

         System.out.println("vList.get(position)---"+vList.get(position).getCheckBtnStatus()+" -"+position); 
          } 
         else 
         { 
          vList.get(position).setCheckBtnStatus("false"); 
         System.out.println("else vList.get(position)---"+vList.get(position).getCheckBtnStatus()+"-"+position); 
         } 

        } 
       }); 

之后在主活动u必须检查哪些复选框的位置是真实的

save.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 


      for (int i = 0; i < newList.size(); i++) { 
       UserData v1 = newList.get(i); 
       status = Boolean.parseBoolean(v1.getCheckBtnStatus()); 

       if (status == true) { 

          //write ur code here 
       } 

      }}); 
-1
  • 复选框 打开“RES /布局/ main.xml中”文件中,添加3“复选框”和一个按钮,所述的LinearLayout内部。
  • 文件:RES /布局/主。XML

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="vertical" > 
    
        <CheckBox 
         android:id="@+id/chkIos" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/chk_ios" /> 
    
        <CheckBox 
         android:id="@+id/chkAndroid" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/chk_android" 
         android:checked="true" /> 
    
        <CheckBox 
         android:id="@+id/chkWindows" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/chk_windows" /> 
    
        <Button 
         android:id="@+id/btnDisplay" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/btn_display" /> 
    
    </LinearLayout> 
    
    0
    3. Code Code 
    Attach listeners inside your activity “onCreate()” method, to monitor following events : 
    
    If checkbox id : “chkIos” is checked, display a floating box with message “Bro, try Android”. 
    If button is is clicked, display a floating box and display the checkbox states. 
    File : MyAndroidAppActivity.java 
    
    package com.mkyong.android; 
    
    import android.app.Activity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.CheckBox; 
    import android.widget.Toast; 
    
    public class MyAndroidAppActivity extends Activity { 
    
        private CheckBox chkIos, chkAndroid, chkWindows; 
        private Button btnDisplay; 
    
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    
        addListenerOnChkIos(); 
        addListenerOnButton(); 
        } 
    
        public void addListenerOnChkIos() { 
    
        chkIos = (CheckBox) findViewById(R.id.chkIos); 
    
        chkIos.setOnClickListener(new OnClickListener() { 
    
         @Override 
         public void onClick(View v) { 
           //is chkIos checked? 
         if (((CheckBox) v).isChecked()) { 
          Toast.makeText(MyAndroidAppActivity.this, 
           "Bro, try Android :)", Toast.LENGTH_LONG).show(); 
         } 
    
         } 
        }); 
    
        } 
    
        public void addListenerOnButton() { 
    
        chkIos = (CheckBox) findViewById(R.id.chkIos); 
        chkAndroid = (CheckBox) findViewById(R.id.chkAndroid); 
        chkWindows = (CheckBox) findViewById(R.id.chkWindows); 
        btnDisplay = (Button) findViewById(R.id.btnDisplay); 
    
        btnDisplay.setOnClickListener(new OnClickListener() { 
    
          //Run when button is clicked 
         @Override 
         public void onClick(View v) { 
    
         StringBuffer result = new StringBuffer(); 
         result.append("IPhone check : ").append(chkIos.isChecked()); 
         result.append("\nAndroid check : ").append(chkAndroid.isChecked()); 
         result.append("\nWindows Mobile check :").append(chkWindows.isChecked()); 
    
         Toast.makeText(MyAndroidAppActivity.this, result.toString(), 
           Toast.LENGTH_LONG).show(); 
    
         } 
        }); 
    
        } 
    }