2011-12-09 55 views
2

我有一個列表視圖每個Textviews和一個複選框的集合組成的項目。 我存儲複選框的狀態在DB,並從上clickListener。它更新它對於那些visible.By默認控件正常工作的所有複選框的處於選中狀態。複選框刷新問題與自定義適配器

如果有10個項目和屏幕可以容納7,然後當我取消選中第一個和滾動到第10個項目,並再次回到第一個滾動。第一個失去它的狀態(它再次被檢查)。我檢查了數據庫的行狀態,這是正確的反映。但是BindView中的提取總是讓我錯誤的狀態。我無法確定問題出在哪裏。我重視這種審查沿列表適配器...

//名單適配器代碼

公共類ListAdaptor擴展的CursorAdapter {

private LayoutInflater mInflater; 
Cursor dataCursor; 
Context context; 
ListView mLv; 

private static final String TAG = "Delete"; 

public ListAdaptor(Context context, Cursor cursor, ListView lv) 
{ 
    super(context, cursor); 
    this.context = context; 
    mLv = lv; 
    mInflater = LayoutInflater.from(context); 
} 

@Override 
public void bindView(View view, Context context, final Cursor cursor) { 

    // Get the stored tag for the view 

    CheckBox tmp_Chk = (CheckBox)view.findViewById(R.id.chkbox); 
    String selText = cursor.getString(11); 

    // Debug Message 
    int val = cursor.getPosition(); 

    tmp_Chk.setChecked(false); 

    SparseBooleanArray sba = mLv.getCheckedItemPositions(); 
    if(sba != null) 
    if(sba.get(cursor.getPosition())) 
     tmp_Chk.setChecked(true); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) 
    { 
     View newView = mInflater.inflate(R.layout.listviewlyt, null); 
     return newView; 
    } 

} 

// Item layout 

<?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="wrap_content" 
android:orientation="horizontal"> 

<CheckBox 
    android:id="@+id/chkbox" 
    android:focusable="false" 
    android:clickable="false" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

</CheckBox> 


<TextView 
    android:id="@+id/label" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@+id/label" 
    android:textSize="18sp" 
    android:textColor="#000000" > 
</TextView> 



</LinearLayout> 

// List control code in the Main Activity 

lv.setOnItemClickListener(new OnItemClickListener() { 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    int lv_Pos = ListView.INVALID_POSITION; 
    CheckBox tmp_Chk = (CheckBox)view.findViewById(R.id.chkbox); 
    if (lv_Pos != ListView.INVALID_POSITION) { 
     if(tmp_Chk.isChecked()){ 
      Check_Uncheck(Integer.toString(lv_Pos + 1), 1); 
    } 
    else if(!tmp_Chk.isChecked()){ 
     Check_Uncheck(Integer.toString(lv_Pos + 1), 0); 
    } 
} 

public void Check_Uncheck(String deleteItem , int select) 
{ 
    // Initialize database 
    DB dbAdapters = DB.getDBAdapterInstance(TabActivity.this); 
    dbAdapters.openDataBase(); 

    ContentValues cv_InitialValues = new ContentValues(); 
    cv_InitialValues.put("Selection", select); 

    dbAdapters.b_UpdateRecordInDB("Items", cv_InitialValues, "_id=?", new String[] {deleteItem}); 
    dbAdapters.close(); 
} 

});

//在主要活動

<ListView 
    android:id="@+id/LV_Instore_CartTab" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:textSize="2px" 
    android:layout_weight="1" 
    android:choiceMode="multipleChoice"/> 
+0

我這個在這裏「http://stackoverflow.com/a/8410557/525978回答說:」如果這樣做不是幫助..我知道...我可以去你的代碼... – havexz

+0

一個快速的問題,其他值檢索正確嗎?或者它只是提供問題的複選框? – Vamsi

+0

@Vamsi,其他值正確檢索。 – Phoenix

回答

1

列表視圖XML屬性您會希望在您的適配器覆蓋getView。當列表滾動時,這個函數被視爲視圖進入視圖。 convertView變量是當您滾動並正在重用時剛剛出現的視圖。該convertView可以爲空,因此,你應該檢查它是否是空和膨脹新行佈局,如果它爲空,否則,你可以使用convertView就像你,如果你已經膨脹了。現在發生的事情就是視圖正在被重用,但是您並未將狀態設置回getView函數中的方式。在這個函數中,您應該使用傳入的位置變量來確定列表中哪個項目與視圖連接。如果您將檢查狀態存儲在對象列表中,則可以使用該位置從列表中獲取正確的項目。使用您從列表中檢索的對象來選中或取消選中該行中的複選框。

0

我沒有真正意識到CompundButton,但你可以嘗試下面的代碼片段,

tmp_Chk.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      if (!tmp_Chk.isChecked()) { 
       tmp_Chk.setChecked(false); 
       Check_Uncheck(); 
      } else { 
       Check_Uncheck(); 
       tmp_Chk.setChecked(true); 

      } 
     } 
    }); 

    if (cursor.getInt(11) == 0) { 
     tmp_Chk.setChecked(false); 
    } else { 
     tmp_Chk.setChecked(true); 
    } 
+0

我也試過這個。沒有運氣.. – Phoenix

相關問題