2011-05-30 74 views
1

我已經看到過類似的其他問題,但沒有什麼能夠解決這些問題。 我的問題是Checkboxes和CheckedTextViews從我的列表中隨機消失。也就是說,它們是第一次加載ListView,並且第一次滾動到底部。任何隨機滾動都會導致CheckBoxes退出並消失。導致CheckBoxes和CheckedTextViews消失的Android ListView

這是我的擴展SimpleCursorAdapter的代碼。

public class CheckBoxCursorAdapter extends SimpleCursorAdapter{ 
    static final String TAG = "CheckBoxCursorAdapter"; 
    final Context contextMain; 
    Cursor c; 
    SQLiteDatabase db; 

    public CheckBoxCursorAdapter(Context context, int layout, Cursor c, 
      String[] from, int[] to, SQLiteDatabase db) { 
     super(context, layout, c, from, to); 
     this.contextMain = context; 
     this.c = c; 
     this.db = db; 
    } 

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

      CheckedTextView cb = (CheckedTextView)view.findViewById(R.id.cb); 
      cb.setOnClickListener(null); 
      TextView cbFull = (TextView)view.findViewById(R.id.fullname); 
      TextView cbAbbrev = (TextView)view.findViewById(R.id.abbrev); 

      cb.setCheckMarkDrawable(android.R.drawable.btn_default); 

      final String abbrv = cursor.getString(cursor.getColumnIndexOrThrow(BuildingOpenHelper.C_ABBRV)); 

      cbFull.setText(cursor.getString(cursor.getColumnIndexOrThrow(BuildingOpenHelper.C_NAME))); 
      cbAbbrev.setText(abbrv); 
      cb.setChecked(cursor.getInt(cursor.getColumnIndexOrThrow(BuildingOpenHelper.C_DISPLAYED)) == 1 ? true : false); 

      Log.v(TAG, "displayed = " + Long.toString(cursor.getInt(cursor.getColumnIndexOrThrow(BuildingOpenHelper.C_DISPLAYED)))); 


      cb.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if(((CheckedTextView) v).isChecked()) {      
         Log.v(TAG, "Calling isChecked==true!"); 
         db.execSQL("UPDATE buildinglist SET displayed='1' WHERE abbrv='"+ abbrv +"'"); 
         ((CheckedTextView) v).setChecked(true); 
        } 
        else { 
         Log.v(TAG, "Calling isChecked==false!"); 
         db.execSQL("UPDATE buildinglist SET displayed='0' WHERE abbrv='"+ abbrv +"'"); 
         //(CheckedTextView) v).setChecked(false); 
         ((CheckedTextView) v).setChecked(false); 
        } 
       }   
      }); 

      final int latitude = cursor.getInt(cursor.getColumnIndexOrThrow(BuildingOpenHelper.C_YCOORD)); 
      final int longitude = cursor.getInt(cursor.getColumnIndexOrThrow(BuildingOpenHelper.C_XCOORD)); 

      view.setOnLongClickListener(new OnLongClickListener() {  
       @Override 
       public boolean onLongClick(View v) { 
       // Log.v(TAG, "Calling isChecked==true!"); 
        // db.execSQL("UPDATE buildinglist SET displayed='1' WHERE abbrv='"+ abbrv +"'"); 
        // ((CheckedTextView) v).setChecked(true); 

        Intent i = new Intent(contextMain,View_map.class); 
        i.putExtra("lat", latitude); 
        i.putExtra("long", longitude); 
        Log.d(TAG,"Latitude onLongClick" + latitude + ""); 
        Log.d(TAG,"Longitude onLongClick" + longitude +""); 

        contextMain.startActivity(i); 
        return false; 
       } 
      }); 
    } 
} 

我正在使用ListActivity,這裏是啓動。

public class Buildings_List extends ListActivity{ 
    BuildingOpenHelper opener; 
    SQLiteDatabase db; 
    SQLiteDatabase dbWrite; 
    static final String[] FROM = {BuildingOpenHelper.C_NAME, BuildingOpenHelper.C_ABBRV, BuildingOpenHelper.C_DISPLAYED}; 
    static final int[] TO = { R.id.fullname, R.id.abbrev, R.id.cb }; 
    private static final String TAG = "Buildings_List"; 
    SimpleCursorAdapter adapt; 
    Cursor myCur = null; 
    Bundle savedInstanceState2; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     savedInstanceState2 = savedInstanceState; 
     super.onCreate(savedInstanceState2); 
     ListView awesome = getListView(); 


     opener = new BuildingOpenHelper(this); 
     db = opener.getReadableDatabase(); 
     dbWrite = opener.getWritableDatabase(); 


     try { 
     myCur = db.query(BuildingOpenHelper.TABLE,null,null,null,null,null,BuildingOpenHelper._ID + " DESC"); 
     } 
     catch(SQLException e){ 
      Log.d(TAG,"Query Went Bad"); 
     } 
     startManagingCursor(myCur); 

     //using depracated SimpleCursorAdapter. Not quite sure what the flags need to be when using updated constructor 
     adapt = new CheckBoxCursorAdapter(this, R.layout.buildinglisting, myCur, FROM, TO, dbWrite); 

     setListAdapter(adapt); 


    } 

我知道這不是onClickListeners是問題,因爲我試圖刪除這些問題仍然存在。我已經閱讀了CheckBoxes的多個帳戶沒有正確回收,但問題仍然存在CheckTextViews。有沒有真的很簡單,我錯過了?

回答