在用户使用自定义游标适配器在 ListView 中编辑行数据之前获取 ListView 中的行数据
getting the row data in ListView before the user edit it in a ListView with custom cursor adapter
我有一个带有扩展 CursorAdapter 的 ListView。
每行的视图是三个 ImageButton 视图,用于编辑、删除和保存两个 EditText 视图。
两个 EditText 视图显示来自光标数据的字符串,默认情况下它们是禁用的。
当用户点击 edit ImageButton 时,两个 EditText 视图被启用并显示 save ImageButton 让用户编辑字符串并保存它们。
然后我想用用户单击保存 ImageButton 时出现的字符串更新我的 table 行。
我在用户单击编辑 ImageButton 并编辑它们之前使用 old/current 字符串更新 table 的行,因为我的方法使用 SqLiteDatabase.update 方法更新行,并且我使用列名和 old/current 字符串来指定 table 中将更新的行。
我有两个问题:
如何在用户更改之前 get/save old/current 字符串?
如何在用户单击一个按钮时禁用 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;
}
}
- 如何在用户更改之前 get/save old/current 字符串?
您可以从光标访问问题的旧值和答案,因为您尚未提交所做的修改。
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
}
});
}
- 如何防止用户在我的 ListView 中单击超过一行的编辑 ImageButton
这是我的想法,您可以在光标适配器中跟踪当前正在编辑的行。如果有任何这样的行,您可以阻止编辑列表视图中的其他行。然后在完成编辑后将此变量的值设置为 null。
我有一个带有扩展 CursorAdapter 的 ListView。 每行的视图是三个 ImageButton 视图,用于编辑、删除和保存两个 EditText 视图。 两个 EditText 视图显示来自光标数据的字符串,默认情况下它们是禁用的。 当用户点击 edit ImageButton 时,两个 EditText 视图被启用并显示 save ImageButton 让用户编辑字符串并保存它们。 然后我想用用户单击保存 ImageButton 时出现的字符串更新我的 table 行。 我在用户单击编辑 ImageButton 并编辑它们之前使用 old/current 字符串更新 table 的行,因为我的方法使用 SqLiteDatabase.update 方法更新行,并且我使用列名和 old/current 字符串来指定 table 中将更新的行。 我有两个问题: 如何在用户更改之前 get/save old/current 字符串? 如何在用户单击一个按钮时禁用 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;
}
}
- 如何在用户更改之前 get/save old/current 字符串?
您可以从光标访问问题的旧值和答案,因为您尚未提交所做的修改。
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
}
});
}
- 如何防止用户在我的 ListView 中单击超过一行的编辑 ImageButton
这是我的想法,您可以在光标适配器中跟踪当前正在编辑的行。如果有任何这样的行,您可以阻止编辑列表视图中的其他行。然后在完成编辑后将此变量的值设置为 null。