用户从列表视图中的一系列选项中选择答案。提交(对或错)后,我打算在列表视图中突出显示正确的答案。
我以为我想到了:
// get the position from the array we fed into the list view that has the correct answer
int correct = curquestion.GetIndexOfCorrectAnswer();
// set that item's background to yellow
lView.getChildAt(correct).setBackgroundColor(Color.YELLOW);
我发现虽然通过观看这个游戏,并且阅读其他人的帖子,其中getChildAt没有提供可靠的结果 - 有时它似乎选择一个随机的孩子设置黄色。有关其他方法的任何建议吗?
答案 0 :(得分:1)
输入要设置颜色的列表项的位置:
adapter.setSelectedPosition(position);
adapter.setNotifyOnChange(true);
listView.invalidate();
并在适配器类中:
//根据选定的状态更改行颜色
if(selectedPos == position){
label.setBackgroundColor(Color.YELLOW);
}else{
label.setBackgroundColor(Color.TRANSPARENT);
}
希望这有帮助。
答案 1 :(得分:0)
您应该实现OnItemClickListener
抽象类(link),您应该使用ListView
方法向setOnItemClickListener()
对象注册。并且,在onItemClick(AdapterView<?> parent, View view, int position, long id)
回调方法中,您可以测试用户是否选择了正确的答案,并且您有用户点击的视图,因此您可以将其着色为黄色。
答案 2 :(得分:0)
设置onSubmit按钮单击以下代码 将正确的整数变量作为用-1初始化的活动字段;
// get the position from the array we fed into the list view that has the correct answer
correct = curquestion.GetIndexOfCorrectAnswer();
adapter.notifyDataSetInvalidated();
并在适配器的getView中执行以下操作:
if(position==correct){
label.setBackgroundColor(Color.YELLOW);
}
答案 3 :(得分:0)
对于任何有兴趣的人,我会在这里发布我的最终代码:
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;
}
}