2011-10-20 66 views
0

我想添加一個複選框到列表視圖的佈局。我已經看了各種論壇帖子,但我仍然有一些麻煩搞清楚。首先,我無法捕獲複選框的點擊。其次,我不確定如何將每個文本框映射到每個動態列表項目。這裏是(從http://www.vogella.de/articles/AndroidSQLite/article.html派生)代碼:添加複選框到列表視圖的問題

WaysToSaveList.java

這裏的waystosave_list.xml文件:

WaysToSaveActivity.java文件

public class WaysToSaveActivity extends Activity { 
    private EditText mTitleText; 
    private EditText mBodyText; 
    private Long mRowId; 
    private WaysToSaveDbAdapter mDbHelper; 
    private Spinner mCategory; 

    @Override 
    protected void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     mDbHelper = new WaysToSaveDbAdapter(this); 
     mDbHelper.open(); 
     setContentView(R.layout.waystosave); 
     mCategory = (Spinner) findViewById(R.id.category); 
     mTitleText = (EditText) findViewById(R.id.waystosave_edit_summary); 
     mBodyText = (EditText) findViewById(R.id.waystosave_edit_description); 
     Button confirmButton = (Button) findViewById(R.id.waystosave_edit_button); 
     mRowId = null; 
     Bundle extras = getIntent().getExtras(); 
     mRowId = (bundle == null) ? null : (Long) bundle  
     .getSerializable(WaysToSaveDbAdapter.KEY_ROWID); 
     if (extras != null) { 
      mRowId = extras.getLong(WaysToSaveDbAdapter.KEY_ROWID); 
     } 

     populateFields(); 

     confirmButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
       setResult(RESULT_OK); 
       finish(); 
      } 
     }); 
    } 

    private void populateFields() { 
     String strChecked = null; 
     Log.v("AppStatus", "Now entering populateFields"); 
     if (mRowId != null) { 
      Cursor todo = mDbHelper.fetchWaysToSave(mRowId); 
      startManagingCursor(todo); 
      String category = todo.getString(todo 
      .getColumnIndexOrThrow(WaysToSaveDbAdapter.KEY_CATEGORY)); 
     for (int i = 0; i < mCategory.getCount(); i++) { 
      String s = (String) mCategory.getItemAtPosition(i); 
      if (s.equalsIgnoreCase(category)) { 
       mCategory.setSelection(i); 
      } 
     } 

     mTitleText.setText(todo.getString(todo  
     .getColumnIndexOrThrow(WaysToSaveDbAdapter.KEY_SUMMARY))); 
     mBodyText.setText(todo.getString(todo 
     .getColumnIndexOrThrow(WaysToSaveDbAdapter.KEY_DESCRIPTION))); 
     } 
    } 

    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     saveState(); 
     outState.putSerializable(WaysToSaveDbAdapter.KEY_ROWID, mRowId); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     saveState(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     populateFields(); 
    } 

    private void saveState() { 
     String category = (String) mCategory.getSelectedItem(); 
     String summary = mTitleText.getText().toString(); 
     String description = mBodyText.getText().toString(); 
     String checked = "-1"; 
     if (mRowId == null) { 
      long id = mDbHelper.createWaysToSave(category, summary, description, 
      checked); 
      if (id > 0) { 
       mRowId = id; 
     } 
    } 

Waystosave_row.xml文件

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

<ImageView 
    android:id="@+id/icon" 
    android:src="@drawable/addwaytosave" 
    android:layout_marginLeft="4px" 
    android:layout_marginRight="8px" 
    android:layout_height="40px" 
    android:layout_marginTop="8px" 
    android:layout_width="30px"> 
</ImageView> 

<TextView 
    android:text="@+id/TextView01" 
    android:layout_height="wrap_content" 
    android:id="@+id/label" 
    android:textSize="20px" 
    android:layout_marginTop="6px" 
    android:layout_width="wrap_content" 
    android:textColor="@color/black"> 
</TextView> 

<CheckBox 
    android:id="@+id/check" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true"> 
</CheckBox> 

</LinearLayout> 

waystosave_list.xml

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

    <LinearLayout style="@style/TitleBar"> 
     <ImageButton style="@style/TitleBarAction" 
      android:contentDescription="@string/description_home" 
      android:src="@drawable/title_home" 
      android:onClick="onClickHome" /> 
     <ImageView style="@style/TitleBarSeparator" /> 
     <TextView style="@style/TitleBarText" /> 
     <ImageButton style="@style/TitleBarAction" 
      android:contentDescription="@string/description_about" 
      android:src="@drawable/about" 
      android:onClick="onClickAbout" /> 
    </LinearLayout> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="14px" 
     android:text="What are ways you save energy? Press the 'Menu' button to insert 
     your tip." /> 
    <ListView 
     android:id="@android:id/list" 
     android:textSize="18px" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
    </ListView> 
    <TextView 
     android:id="@android:id/empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="18px" 
     android:text="@string/no_waystosave" />  
</LinearLayout> 

WaysToSaveDbAdapter.java文件

public class WaysToSaveDbAdapter { 
    // Database fields 
public static final String KEY_ROWID = "_id"; 
public static final String KEY_CATEGORY = "category"; 
public static final String KEY_SUMMARY = "summary"; 
public static final String KEY_DESCRIPTION = "description"; 
public static final String KEY_CHECKED = "checked"; 
private static final String DATABASE_TABLE = "todo"; 
private Context context; 
private SQLiteDatabase database; 
private WaysToSaveDbHelper dbHelper; 

public WaysToSaveDbAdapter(Context context) { 
    this.context = context; 
} 

// http://www.vogella.de/articles/AndroidSQLite/article.html 

public WaysToSaveDbAdapter open() throws SQLException { 
    dbHelper = new WaysToSaveDbHelper(context); 
    database = dbHelper.getWritableDatabase(); 
    return this; 
} 

public void close() { 
    dbHelper.close(); 
} 

public long createWaysToSave(String category, String summary, String description, 
    String checked) { 
    ContentValues initialValues = createContentValues(category, summary, 
    description, checked); 
     return database.insert(DATABASE_TABLE, null, initialValues); 
} 

public boolean updateWaysToSave(long rowId, String category, String summary, 
String description, String checked) { 
    ContentValues updateValues = createContentValues(category, summary, 
    description, checked); 
    return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "=" 
    + rowId, null) > 0; 
} 

public boolean deleteWaysToSave(long rowId) { 
    return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
} 

public Cursor fetchAllWaysToSave() { 
    return database.query(DATABASE_TABLE, new String[] { KEY_ROWID, 
    KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION, KEY_CHECKED }, null, null, null, 
    null, null); 
} 

public Cursor fetchWaysToSave(long rowId) throws SQLException { 
Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] { 
KEY_ROWID, KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION, KEY_CHECKED }, 
KEY_ROWID + "=" + rowId, null, null, null, null, null); 
    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 

     return mCursor; 
} 

private ContentValues createContentValues(String category, String summary, String 
    description, String checked) { 
    ContentValues values = new ContentValues(); 
    values.put(KEY_CATEGORY, category); 
    values.put(KEY_SUMMARY, summary); 
    values.put(KEY_DESCRIPTION, description); 
    values.put(KEY_CHECKED, checked); 
    return values; 
} 
} 

WaysToSaveDbHelper.java文件

public class WaysToSaveDbHelper extends SQLiteOpenHelper { 
    private static final String DATABASE_NAME = "applicationdata"; 
private static final int DATABASE_VERSION = 1; 

// Database creation sql statement 
private static final String DATABASE_CREATE = "create table todo (_id integer 
    primary key autoincrement, " + "category text not null, summary text not null, 
    description text not null, checked text not null);"; 

public WaysToSaveDbHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase database) { 
    database.execSQL(DATABASE_CREATE); 
} 

@Override 
public void onUpgrade(SQLiteDatabase database, int oldVersion, 
int newVersion) { 
    database.execSQL("DROP TABLE IF EXISTS todo"); 
    onCreate(database); 
} 
} 

回答

0

你基本上將不得不讓自己的列表適配器。由於您使用的是光標,因此您需要修改CursorAdapter類。在你的bindView方法你會做這樣的事情:

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

    //assign onClick listener to the checkbox 
    CheckBox checkBox = (Checkbox)view.findViewById(R.id.check); 
    //assign an onClickListener to the checkbox 
    checkbox.setOnClickListener(new View.onClickListener(){ 
    public void onClick(View view){ 
     //do what ever you want here 
    } 
    }); 

    TextView label = (TextView)view.findViewById(R.id.label); 
    //assign whatever lable you want. remeber, you have the cursor pointing 
    //to what this row is supposed to be displaying. 
} 
+0

感謝您的迴應。我無法得到這個工作,我正在尋找其他的實施選項。當我嘗試使用我的自定義適配器時,應用程序崩潰。 – user836200

+0

我編輯了我的答案。不知道它是否會有所幫助,但是如果能夠使其發揮作用,那麼現在的變化應該會讓事情變得更好,更好。 –

相關問題