I am writing an Android application that has a ListView with CheckedTextView elements. This is basically a question with a variable number of answers. If you select one answer, you can click "Next" and see the next question.
I wrote all the code to save the answers and move on to the next questions, and I also provided the user with a “previous” button to return to the previous question. If the previous question already has an answer, I want this answer to be selected. And this is where the problems begin.
Currently, I can get the position of the selected answer, and I call listView.setItemChecked(pos, true), but the switch is not selected.
if(selectedAnswer != null) {
int pos = mAnswerAdapter.getPosition(selectedAnswer);
if(pos != -1) {
listAnswers.setItemChecked(pos, true);
}
}
Only when I do something else, for example, by dragging and dropping the status bar, does the view seem to update and draw the selected radio state.
I populate the list with answers as follows:
mAnswerAdapter = new AnswerArrayAdapter(getContext(), R.layout.listitem_answer, currentQuestion.getAnswers(), user.getLanguage());
listAnswers.setAdapter(mAnswerAdapter);
mAnswerAdapter.notifyDataSetChanged();
For reference:
Layout list_item:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeightSmall"
android:drawableStart="@drawable/questionnaire_answer_radio"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:textColor="@android:color/white"
android:id="@+id/questionnaire_answer_checkbox"/>
Adapter:
package be.iminds.mresist;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import java.util.List;
import be.iminds.mresist.models.QuestionnaireDefinition;
import butterknife.BindView;
import butterknife.ButterKnife;
public class AnswerArrayAdapter extends ArrayAdapter<QuestionnaireDefinition.Answer> {
private String mLang;
private Context mContext;
public AnswerArrayAdapter(Context context, int textViewResourceId, List<QuestionnaireDefinition.Answer> answers, final String lang) {
super(context, textViewResourceId, answers);
this.mContext = context;
this.mLang = lang;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
AnswerArrayAdapter.ViewHolder holder = null;
QuestionnaireDefinition.Answer item = getItem(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.listitem_answer, parent, false);
holder = new AnswerArrayAdapter.ViewHolder(convertView);
convertView.setTag(holder);
} else
holder = (AnswerArrayAdapter.ViewHolder) convertView.getTag();
holder.answerText.setText(item.getAnswer(mLang));
return convertView;
}
static class ViewHolder {
@BindView(R.id.questionnaire_answer_checkbox) CheckedTextView answerText;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
Corresponding fragmented code (the first call to openQuestion occurs onStart):
private void openQuestion(int questionIdx)
{
currentQuestionIdx = questionIdx;
final Question currentQuestion = getCurrentQuestion();
txtQuestionTitleCounter.setText(String.format(getString(R.string.questionnaire_counter), questionIdx + 1, qAssignment.getDefinition().getQuestions().size()));
txtQuestion.setText(currentQuestion.getQuestion(user.getLanguage()));
mAnswerAdapter = new AnswerArrayAdapter(getContext(), R.layout.listitem_answer, currentQuestion.getAnswers(), user.getLanguage());
listAnswers.setAdapter(mAnswerAdapter);
mAnswerAdapter.notifyDataSetChanged();
if(!isLastQuestion(currentQuestionIdx) && qAssignment.getDefinition().getQuestions().size() > 1)
mNextButton.setVisibility(View.VISIBLE);
else
mNextButton.setVisibility(View.GONE);
if(!isFirstQuestion(currentQuestionIdx) && qAssignment.getDefinition().getQuestions().size() > 1)
mPreviousButton.setVisibility(View.VISIBLE);
else
mPreviousButton.setVisibility(View.GONE);
markPreviousAnswer(currentQuestion);
}
private void markPreviousAnswer(Question currentQuestion) {
if(qAssignment.getAnswerValues() != null && qAssignment.getAnswerValues().containsKey(currentQuestion.getQuestionKey())) {
Integer value = qAssignment.getAnswerValues().get(currentQuestion.getQuestionKey());
QuestionnaireDefinition.Answer selectedAnswer = null;
for(QuestionnaireDefinition.Answer answer: currentQuestion.getAnswers()) {
if(answer.getValue() == value) {
selectedAnswer = answer;
break;
}
}
if(selectedAnswer != null) {
int pos = mAnswerAdapter.getPosition(selectedAnswer);
if(pos != -1) {
listAnswers.setItemChecked(pos, true);
}
}
}
}
Selected CheckedTextView selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="oval">
<size android:width="16dp" android:height="16dp"/>
<solid android:color="@color/colorPrimaryDarker"/>
<stroke android:color="@android:color/white" android:width="2dp"/>
</shape>
</item>
<item android:state_checked="true">
<shape android:shape="oval">
<size android:width="16dp" android:height="16dp"/>
<solid android:color="@color/colorPrimaryDarker"/>
<stroke android:color="@android:color/white" android:width="2dp"/>
</shape>
</item>
<item>
<shape android:shape="oval" android:innerRadius="10dp">
<size android:width="16dp" android:height="16dp"/>
<stroke android:color="@android:color/white" android:width="1dp"/>
</shape>
</item>
</selector>