2

用户从列表视图中的一系列选项中选择一个答案。提交(正确或错误)后,我打算在列表视图中突出显示正确答案。

我以为我想通了:

 // 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 不能提供可靠的结果 - 有时它似乎会随机选择一个孩子设置为黄色。对其他方法有什么建议吗?

4

4 回答 4

1

输入要设置颜色的列表项的位置:

 adapter.setSelectedPosition(position);
 adapter.setNotifyOnChange(true);
 listView.invalidate();

在适配器类中:

// 根据选中状态改变行颜色

if(selectedPos == position){
    label.setBackgroundColor(Color.YELLOW);
 }else{
    label.setBackgroundColor(Color.TRANSPARENT);
 }

希望这可以帮助。

于 2012-07-24T05:49:14.110 回答
0

您应该实现OnItemClickListener抽象类(链接),您应该使用方法向ListView对象注册它setOnItemClickListener()。而且,在onItemClick(AdapterView<?> parent, View view, int position, long id)回调方法中,您可以测试用户是否选择了正确的答案,并且您有用户点击的视图,因此您可以将其涂成黄色。

于 2012-07-24T05:43:17.760 回答
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);
 }
于 2012-07-24T06:01:30.787 回答
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;
}

}

于 2012-08-18T03:37:57.230 回答