将复选框添加到列表视图时出现问题

时间:2011-10-20 01:45:43

标签: android listview checkbox

我正在尝试为listview布局添加一个复选框。我查看了各种论坛帖子,但我仍然遇到一些麻烦。首先,我无法捕捉复选框的点击次数。其次,我不确定如何将每个文本框映射到每个动态列表项。这是代码(源自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);
}
}

1 个答案:

答案 0 :(得分: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.
}