Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 向listview添加复选框时出现问题_Android_Listview_Checkbox - Fatal编程技术网

Android 向listview添加复选框时出现问题

Android 向listview添加复选框时出现问题,android,listview,checkbox,Android,Listview,Checkbox,我正在尝试向listview布局添加复选框。我看了各种论坛帖子,但我还是有一些问题想清楚。首先,我无法捕获复选框的单击。其次,我不知道如何将每个文本框映射到每个动态列表项。以下是源于以下内容的代码: WaysToSaveList.java 以下是waystosave_list.xml文件: WaysToSaveActivity.java文件 public class WaysToSaveActivity extends Activity { private EditText mTitle

我正在尝试向listview布局添加复选框。我看了各种论坛帖子,但我还是有一些问题想清楚。首先,我无法捕获复选框的单击。其次,我不知道如何将每个文本框映射到每个动态列表项。以下是源于以下内容的代码:

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

基本上,您必须制作自己的列表适配器。因为您使用的是游标,所以需要对该类进行子cass。在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.
}

谢谢你的回复。我无法让它工作,我正在寻找其他的实现选项。当我试图使用自定义适配器时,应用程序崩溃。我编辑了答案。不确定这是否会有帮助,但如果你能让它工作起来的话,现在的改变应该会让事情变得不同和更好。
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);
}
}
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.
}