2010-03-09 12 views
7

साथ ListView में मैं जब एक कर्सर एडाप्टर का उपयोग चेकबॉक्स राज्य को बचाने के लिए एक रास्ता खोजने नहीं कर सकते। बाकी सब कुछ ठीक काम करता है, लेकिन अगर मैं एक चेकबॉक्स पर क्लिक करें यह जब यह पुनर्नवीनीकरण दोहराया जाता है। Ive सरणी एडाप्टर के उपयोग के उदाहरण देखा है, लेकिन अनुभव im की मेरी कमी की वजह से लग रहा यह मुश्किल एक कर्सर एडाप्टर का उपयोग कर में अनुवाद करने के लिए। किसी ने मुझे कैसे इसके बारे में जाने के लिए का एक उदाहरण दे सकता है। किसी भी मदद की सराहना की।बचाने एंड्रॉयड चेकबॉक्स राज्य कर्सर एडाप्टर

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

मैं आप का उपयोग की सिफारिश करेंगे एंड्रॉयड के अंतर्निहित बहु-विकल्प सूची (CHOICE_MODE_MULTIPLE) के लिए समर्थन।

List11.java एसडीके नमूना यह दर्शाता है। तुम भी मेरी ट्यूटोरियल में से एक यह here का उपयोग करता है से एक परियोजना पा सकते हैं।

आप अभी भी अपने स्वयं के लेआउट के साथ इस तकनीक का उपयोग कर सकते हैं, जब तक कि आप android:id="@android:id/text1" के साथ एक CheckedTextView शामिल के रूप में अपने SDK के साथ android.R.layout.simple_list_item_multiple_choice संसाधन में दिखाया गया है, जो जहाजों की एक प्रति।

इसके अलावा, this question और this question और this question और this question देखें।

+1

मैं कैसे अपने खुद के लेआउट का प्रयोग करेंगे? चेकटेक्स्टव्यू को पंक्ति का शीर्ष स्तर होने की आवश्यकता नहीं है? जब मैं एक चेकटेक्स्टव्यू का उपयोग करता हूं, तो मुझे एक दृश्य (कैमरा से फोटो) दिखाने के लिए एक छवि दृश्य जैसी छवियों को जोड़ने का तरीका ढूंढना प्रतीत नहीं होता है। – Ricardo

+0

सबसे पहले, 'चेकड टेक्स्टव्यू' 'एंड्रॉइड का समर्थन करता है: ड्रॉरेबल लाइफ 'और किन, जिसका आप उपयोग कर सकते हैं। दूसरा, AFAIK, जब तक कि उचित 'एंड्रॉइड: आईडी' (ऊपर देखें) है, यह किसी अन्य लेआउट के अंदर हो सकता है (उदाहरण के लिए,' LinearLayout' में 'ImageView' भी शामिल है)। – CommonsWare

+0

अब मुझे इस थ्रेड http://www.mail-archive.com/[email protected]/msg06941.html जैसी ही समस्या है। मैं एंड्रॉइड कैसे सेट करूं: पीछे कोड से drawableLeft संपत्ति? – Ricardo

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

आशा है कि यह आपको या किसी अन्य व्यक्ति की मदद करता है जो इस प्रश्न पर ठोकर खाता है।

इसके अलावा, मेरे जावा नोबनेस को क्षमा करें ... मैंने कुछ हफ्ते पहले ही जावा शुरू किया है।

1

getView विधि पर चेक बॉक्स घोषित और मुख्य गतिविधि पर इस कोड

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

        } 
       }); 

का उपयोग उसके बाद यू जाँच करने के लिए चेकबॉक्स की स्थिति सच है जो है

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
  1. चेकबॉक्स "res/layout/main.xml" फ़ाइल खोलें, LinearLayout के अंदर 3 "चेकबॉक्स" और एक बटन जोड़ें।

फ़ाइल: res/layout/main।एक्सएमएल

<?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(); 

     } 
    }); 

    } 
} 
संबंधित मुद्दे