I have a ListView with extended CursorAdapter. The view of each row is three ImageButton views for edit,delete and save two EditText views. The two EditText views show strings from the cursor data, and they are disabled by default. When the user click edit ImageButton the two EditText views enabled and the save ImageButton is shown to let user edit the strings and let him save them. Then I want to update my table row with the strings which are present when the user click save ImageButton. I am updating the row of the table by using the old/current strings present before the user click the edit ImageButton and edit them, because my method uses the SqLiteDatabase.update method to update the row, and I use the column name and the old/current string to specify which row will update in the table. I have two questions: how can I get/save the old/current strings before the user change them? how to disable all rows of ListView when user clicks a button and enable them when he clicks another button? My custom view for ListView rows:
<?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>
My extended 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;
}
}
You can access the old values of the question and the answer from the cursor because you have not committed the modification made.
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
}
});
}
This is from top of my head, you can keep track of the row currently being edited in your cursor adapter. If there is any such row, you can prevent editing of other rows in the list view. And then set the value this variable to null once done editing.