I have a ListView, the layout of each row is from this XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<ImageButton
android:id="@+id/study_entry"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_weight="0"
android:background="@drawable/pen_in_trash"
android:layout_gravity="center"
android:focusable="false"
/>
<ImageButton
android:id="@+id/save_edited_entry"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_weight="0"
android:background="@drawable/ic_save_black_24dp"
android:layout_gravity="center"
android:focusable="false"
android:enabled="false"
/>
<EditText
android:id="@+id/answer_field"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:minLines="1"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:enabled="false"
/>
<EditText
android:id="@+id/question_field"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:minLines="1"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:enabled="false"
/>
<ToggleButton
android:id="@+id/edit_entry"
android:layout_width="45dp"
android:layout_height="45dp"
android:text="New ToggleButton"
android:layout_weight="0"
android:background="@drawable/ic_edit_black_24dp"
android:focusable="false"
android:layout_gravity="center"
android:textOff="ON"
android:textOn="OFF"
android:textStyle="bold"
android:textColor="#FFFFFF"/>
<ImageButton
android:id="@+id/delete_entry"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_weight="0"
android:src="@drawable/ic_delete_black_24dp"
android:layout_gravity="center"
android:focusable="false"/>
</LinearLayout>
and I am using custom CursorAdapter to provide the data to the ListView:
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, Cursor cursor) {
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));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = LayoutInflater.from(context).inflate(R.layout.field_entries_spinner, parent, false);
final EditText editQuestion, editAnswer;
editQuestion = (EditText) view.findViewById(R.id.question_field);
editAnswer = (EditText) view.findViewById(R.id.answer_field);
final String question = cursor.getString(1);
final String answer = cursor.getString(2);
final ImageButton studyEntry,saveEditedEntry,deleteEntry;
studyEntry = (ImageButton) view.findViewById(R.id.study_entry);
saveEditedEntry = (ImageButton) view.findViewById(R.id.save_edited_entry);
final ToggleButton editEntry = (ToggleButton) view.findViewById(R.id.edit_entry);
deleteEntry = (ImageButton) view.findViewById(R.id.delete_entry);
editEntry.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if ( isChecked ) {
editQuestion.setEnabled(true);
editAnswer.setEnabled(true);
saveEditedEntry.setEnabled(true);
studyEntry.setEnabled(false);
} else {
editQuestion.setEnabled(false);
editAnswer.setEnabled(false);
saveEditedEntry.setEnabled(false);
studyEntry.setEnabled(true);
}
}
});
studyEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FieldActivity.this,StudyActivity.class);
intent.putExtra(StudyActivity.CURRENT_FIELD,getCurrentField());
intent.putExtra(StudyActivity.CURRENT_QUESTION,editQuestion.getText().toString());
intent.putExtra(StudyActivity.CURRENT_ANSWER,editAnswer.getText().toString());
startActivity(intent);
}
});
deleteEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteEntryData[0] = getCurrentField();
deleteEntryData[1] = editQuestion.getText().toString();
deleteEntryData[2] = editAnswer.getText().toString();
DeleteEntryDialogFragment fragment = new DeleteEntryDialogFragment();
fragment.show(getSupportFragmentManager(), DeleteEntryDialogFragment.DELETE_ENTRY_DIALOG_FRAGMENT);
}
});
saveEditedEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editQuestion.setEnabled(false);
editAnswer.setEnabled(false);
saveEditedEntry.setEnabled(false);
editEntry.setChecked(false);
DatabaseOperations databaseOperations = new DatabaseOperations(FieldActivity.this);
if ( !editQuestion.getText().toString().equals(question) || !editAnswer.getText().toString().equals(answer) ) {
databaseOperations.editFieldEntry(getCurrentField(), question, editQuestion.getText().toString(),
answer, editAnswer.getText().toString());
}
CustomCursorAdapter.this.changeCursor(new DatabaseOperations(FieldActivity.this).getFieldEntrys(getCurrentField()));
searchFor.setText("");
}
});
return view;
}
}
My SQLiteOpenHelper child is big to insert but those the two method that correspond to my question:
public void editFieldEntry(String fieldName, String currentQuestion, String newQuestion, String currentAnswer, String newAnswer) {
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
if ( !currentQuestion.equals(newQuestion) ) {
Cursor cursor = sqLiteDatabase.query(fieldName + Field.TABLE_NAME_LINK_SUFFIX,
new String[]{fieldName + Field.COLUMN_NAME_QUESTION_LINK_SUFFIX},
fieldName + Field.COLUMN_NAME_QUESTION_LINK_SUFFIX + " = ?",
new String[]{currentQuestion},
null,
null,
null);
if (cursor.getCount() > 1) {
insertQuestion(fieldName, newQuestion);
insertQuestionAnswerLink(fieldName, newQuestion, currentAnswer);
} else {
editQuestion(fieldName,currentQuestion,newQuestion);
}
}
if ( !currentAnswer.equals(newAnswer) ) {
Cursor cursor = sqLiteDatabase.query(fieldName + Field.TABLE_NAME_LINK_SUFFIX,
new String[]{ fieldName + Field.COLUMN_NAME_ANSWER_LINK_SUFFIX},
fieldName + Field.COLUMN_NAME_ANSWER_LINK_SUFFIX + " = ?",
new String[]{currentAnswer},
null,
null,
null);
if (cursor.getCount() > 1) {
insertQuestion(fieldName, newAnswer);
insertQuestionAnswerLink(fieldName, currentQuestion, newAnswer);
} else {
editAnswer(fieldName,currentAnswer,newAnswer);
}
}
}
public Cursor getFieldEntrys(String fieldName) {
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT " + fieldName + Field.TABLE_NAME_LINK_SUFFIX + "._id As" +
" _id, " + fieldName + Field.COLUMN_NAME_QUESTION_SUFFIX + Field.COMMA +
fieldName + Field.COLUMN_NAME_ANSWER_LINK_SUFFIX +
" From " + fieldName + Field.TABLE_NAME_QUESTION_SUFFIX +
" INNER JOIN " + fieldName + Field.TABLE_NAME_LINK_SUFFIX + " ON " +
fieldName + Field.COLUMN_NAME_QUESTION_SUFFIX +
" = " + fieldName + Field.COLUMN_NAME_QUESTION_LINK_SUFFIX + "", null);
return cursor;
}
What I want is when I click the edit ToggleButton the two EditText in the same row become enabled to let the user edit its values. then when the user click the save ImageButton the EditTexts become disabled and the new values of the EditTexts replace the old values. I have problems with this listView: First: when I press the edit ToggleButton then click on EditText to start editing, the keyboard appear but the the row that I have clicked its edit ToggleButton and then Clicked its EditText, this row its EditTexts become disabled and what become enabled is the first row of the ListView or the last row of the ListView or rarely another row in the middle of the ListView, just the keyboard appears my desired row is disabled and any of those rows is enabled!!! Second: when I click the edit ToggleButton of the desired row again to enable the EditTexts of the desired row after the keyboard have appeared, the enabled rows become at least two rows, the desired one and at least one of the undesired rows!!! Third: after editing the EditTexts in the desired row (while the undesired row(s) are still enabled) then click the save ImageButton, the desired action is that the new values of the EditTexts replaces the old values using calls to the methods that manipulate the database, which is declared in the DatavaseOperations which extends the SQLiteOpenHelper that I created, like databaseOperations.editFieldEntry() and DatabaseOperations.getFieldEntrys(). What happen is that the last row in the ListView is deleted or sometimes another row is deleted, I do not Know how? and Why? and the new values of the EditTexts does not replace the old values. Now what can I do to enable only the desired row the edit ToggleButton is clicked? Why those strange action happen when I try to save the edited EditTexts values? and how to prevent those strange actions and make the new values replace the old values?
Aucun commentaire:
Enregistrer un commentaire