我有一个带有扩展CursorAdapter的ListView。 每行的视图是三个ImageButton视图,用于编辑,删除和保存两个EditText视图。 两个EditText视图显示游标数据中的字符串,默认情况下它们被禁用。 当用户单击编辑ImageButton时,启用了两个EditText视图,并显示了保存ImageButton以允许用户编辑字符串并让他保存它们。 然后我想用用户点击保存ImageButton时出现的字符串更新我的表行。 我在用户单击编辑ImageButton之前使用旧的/当前字符串更新表的行并编辑它们,因为我的方法使用SqLiteDatabase.update方法更新行,并且我使用列名称和旧的/ current string指定将在表中更新的行。 我有两个问题: 如何在用户更改之前获取/保存旧/当前字符串? 如何在用户单击按钮时禁用ListView的所有行,并在单击另一个按钮时启用它们? 我对ListView行的自定义视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageButton
android:id="@+id/save_edited_entry"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_weight="0"
android:src="@drawable/ic_save_black_24dp"
android:layout_gravity="center"
android:scaleType="centerInside"
android:visibility="invisible"
android:focusable="false"/>
<EditText
android:id="@+id/answer_field"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:enabled="false"
android:layout_gravity="center"/>
<EditText
android:id="@+id/question_field"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:enabled="false"
android:layout_gravity="center"/>
<ImageButton
android:id="@+id/edit_entry"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_weight="0"
android:src="@drawable/ic_create_white_24dp"
android:layout_gravity="center"
android:scaleType="centerInside"
android:focusable="false"/>
<ImageButton
android:id="@+id/delete_entry"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_weight="0"
android:src="@drawable/ic_clear_black_24dp"
android:layout_gravity="center"
android:scaleType="centerInside"
android:focusable="false"/>
</LinearLayout>
我的扩展CursorAdpater:
public class CustomCursorAdapter extends CursorAdapter {
public CustomCursorAdapter( Context context, Cursor cursor, int flags ) {
super(context,cursor,flags);
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
final EditText editQuestion, editAnswer;
editQuestion = (EditText) view.findViewById(R.id.question_field);
editAnswer = (EditText) view.findViewById(R.id.answer_field);
editQuestion.setText(cursor.getString(1));
editAnswer.setText(cursor.getString(2));
final ImageButton saveEditedEntry, editEntry, deleteEntry;
editEntry = (ImageButton) view.findViewById(R.id.edit_entry);
deleteEntry = (ImageButton) view.findViewById(R.id.delete_entry);
saveEditedEntry = (ImageButton) view.findViewById(R.id.save_edited_entry);
editEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editQuestion.setEnabled(true);
editAnswer.setEnabled(true);
saveEditedEntry.setVisibility(View.VISIBLE);
}
});
saveEditedEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = LayoutInflater.from(context).inflate(R.layout.aspect_entries_spinner, parent, false);
return view;
}
}
答案 0 :(得分:0)
您可以访问问题的旧值和光标的答案,因为您尚未提交修改。
public void bindView(View view, Context context, final Cursor cursor) {
final EditText editQuestion, editAnswer;
editQuestion = (EditText) view.findViewById(R.id.question_field);
editAnswer = (EditText) view.findViewById(R.id.answer_field);
final question = cursor.getString(1);
final answer = cursor.getString(2);
editQuestion.setText(cursor.getString(1));
editAnswer.setText(cursor.getString(2));
final ImageButton saveEditedEntry, editEntry, deleteEntry;
editEntry = (ImageButton) view.findViewById(R.id.edit_entry);
deleteEntry = (ImageButton) view.findViewById(R.id.delete_entry);
saveEditedEntry = (ImageButton) view.findViewById(R.id.save_edited_entry);
editEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editQuestion.setEnabled(true);
editAnswer.setEnabled(true);
saveEditedEntry.setVisibility(View.VISIBLE);
}
});
saveEditedEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//You can use the question and answer here
}
});
}
这是我的头脑,您可以跟踪光标适配器中当前正在编辑的行。如果存在任何此类行,则可以阻止编辑列表视图中的其他行。然后在完成编辑后将此变量的值设置为null。