興味のある方のために、最終的なコードをここに投稿します。
lView.setEnabled(false);
btnsubmit.setEnabled(true);
int correct = curquestion.GetIndexOfCorrectAnswer();
// tell the system the correct answer
adapters.SetSelectedPosition(correct);
//adapters.setNotifyOnChange(true);
adapters.notifyDataSetChanged();
上記のコードはアクティビティで呼び出され、以下はハンドラーのカスタム クラスです。あなたがおそらく理解できる他のxmlピース。
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
import com.example.walpdroid2.R;
public class QuizDataAdapter extends ArrayAdapter<Answer>
{
Answer [] objects = null;
Context checkcontext;
int _selectedIndex;
int checkedviewresourceid;
int uncheckedviewresourceid;
int selectedPos = -1;
public QuizDataAdapter(Context checkcontext, int checkresourceid, int uncheckresourceid, Answer[] objects)
{
super(checkcontext, checkresourceid, objects);
this.checkedviewresourceid = checkresourceid;
this.uncheckedviewresourceid = uncheckresourceid;
this.checkcontext = checkcontext;
this.objects = objects;
}
public int getCount()
{
return this.objects.length;
}
public Answer getItem(int position)
{
return this.objects[position];
}
public long getItemId(int position)
{
return position;
}
public void setSelected(int index) {
if (index == -1) {
// unselected
}
else {
// selected index...
}
_selectedIndex = index;
// notify the model that the data has changed, need to update the view
notifyDataSetChanged();
}
public void SetSelectedPosition(int pos)
{
this.selectedPos = pos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
CheckedTextView holder = null;
if((row == null) || (selectedPos == position))
{
LayoutInflater inflater = ((Activity)checkcontext).getLayoutInflater();
if(selectedPos == position)
{
row = inflater.inflate(checkedviewresourceid, parent, false);
holder = (CheckedTextView)row.findViewById(R.id.CheckRow);
}
else
{
row = inflater.inflate(uncheckedviewresourceid, parent, false);
holder = (CheckedTextView)row.findViewById(R.id.UnCheckRow);
}
holder.setText(objects[position].getAnswer());
row.setTag(holder);
}
else
{
holder = (CheckedTextView)row.getTag();
holder.setText(objects[position].getAnswer());
}
return row;
}
}