我的活动布局包含我动态创建的 ListView 类型列表(我没有使用实际的 ListView)。单击添加按钮,创建包含 EditText 和删除按钮的行。用户的输入从 EditText 保存到 SQLite 表中。除删除按钮外,一切正常。我可以删除视图,但数据仍保留在表中。模拟器在单击删除按钮时崩溃。
该表仅包含“_Id”列和“Comment”列。我是 Android 开发的初学者,但对 SQLite 尤其缺乏经验;这是我的第一次尝试。抱歉,如果我添加了太多代码。我添加了整个数据源类,但我觉得我太过分了。如果您想看更多,请告诉我,因为我永远不知道我所附的所有内容是否有用。
这是我将一行的 EditText 输入保存到数据库的方法。
public Comment createComment(String comment) {
ContentValues values = new ContentValues();
values.put(SQLiteHelper.COLUMN_COMMENT, comment);
long insertId = database.insert(SQLiteHelper.TABLE_COMMENTS, null,
values);
Cursor cursor = database.query(SQLiteHelper.TABLE_COMMENTS,
allColumns, SQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
Comment newComment = cursorToComment(cursor);
cursor.close();
return newComment;
}
这是我从数据库中删除一行数据的尝试。
public void deleteComment(String comment) {
ContentValues values = new ContentValues();
values.put(SQLiteHelper.COLUMN_COMMENT, comment);
database.delete(SQLiteHelper.TABLE_COMMENTS, SQLiteHelper.COLUMN_COMMENT
+ " = " + values, null);
}
这是我的评论课。不确定是否有必要考虑到评论只是 EditText 中的一个字符串。我从在线资源中借了很多代码,所以我不完全清楚应该如何将它们放在一起。
public class Comment {
private long id;
private String comment;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
// Will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
return comment;
}
}
这是我的主要活动中动态创建视图的一些代码。
// onClick handler for the "Add new" button;
public void onAddNewClickedStrengths(View v) {
// Inflate a new row and hide the button self.
inflateEditRowStrengths(null);
v.setVisibility(View.GONE);
}
// Helper for inflating a row
private void inflateEditRowStrengths(String name) {
idCount++;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.list_row, null);
final ImageButton deleteButton = (ImageButton) rowView
.findViewById(R.id.button);
final EditText editText = (EditText) rowView
.findViewById(R.id.editText);
editText.setId(idCount);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
comment = datasource.createComment(editText.getText().toString());
}
}
});
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datasource = new CommentsDataSource(getBaseContext());
datasource.open();
List<Comment> values = datasource.getAllComments();
for (int i = 1; i < values.size(); i++){
inflateEditRowStrengths(values.get(i).toString());
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datasource.deleteAllComments();
List<Comment> values = datasource.getAllComments();
for (int i = 0; i < values.size(); i++){
inflateEditRowStrengths(values.get(i).toString());
}
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String toFind = editText.getText().toString();
datasource.deleteComment(toFind);
mContainerViewStrengths.removeView(rowView);
}
});
if (name != null && !name.isEmpty()) {
editText.setText(name);
} else {
mExclusiveEmptyView = rowView;
deleteButton.setVisibility(View.INVISIBLE);
}
// A TextWatcher to control the visibility of the "Add new" button and
// handle the exclusive empty view.
editText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if (s.toString().isEmpty()) {
mAddButtonStrengths.setVisibility(View.GONE);
deleteButton.setVisibility(View.INVISIBLE);
if (mExclusiveEmptyView != null
&& mExclusiveEmptyView != rowView) {
mContainerViewStrengths.removeView(mExclusiveEmptyView);
editText.getText();
}
mExclusiveEmptyView = rowView;
} else {
if (mExclusiveEmptyView == rowView) {
mExclusiveEmptyView = null;
}
mAddButtonStrengths.setVisibility(View.VISIBLE);
deleteButton.setVisibility(View.VISIBLE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
// Inflate at the end of all rows but before the "Add new" button
mContainerViewStrengths.addView(rowView, mContainerViewStrengths.getChildCount() - 1);
}