Android自定义ArrayAdapter只有2个ViewHolders

时间:2014-11-21 14:41:29

标签: android listview android-arrayadapter

这个自定义ArrayAdapter到我的ListView正确地填充它的字段,但它设置颜色完全错误。 只有2个ViewHolders正在创建,我在ListView中有最多10个元素,因此其他元素从已创建的持有者中获取颜色。你能帮我解决一下如何使这个工作吗?

public class MistakesAdapter extends ArrayAdapter<Data> {

static class ViewHolder {
    TextView question;
    TextView answer1;
    TextView answer2;
    TextView answer3;
    TextView answer4;

    ViewHolder(View v){
        this.question = (TextView) v.findViewById(R.id.question);
        this.answer1 = (TextView) v.findViewById(R.id.answer1);
        this.answer2 = (TextView) v.findViewById(R.id.answer2);
        this.answer3 = (TextView) v.findViewById(R.id.answer3);
        this.answer4 = (TextView) v.findViewById(R.id.answer4);
    }
}

public MistakesAdapter(Context context, ArrayList<Data> data) {
    super(context, 0, data);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    Data data = getItem(position);
    ViewHolder holder;
    if (v == null) {
        v = LayoutInflater.from(getContext()).inflate(R.layout.mistakes_list,null);
        holder = new ViewHolder(v);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
    try {
        holder.question.setText(data._question);
        holder.answer1.setText(data._answer1);
        holder.answer2.setText(data._answer2);
        holder.answer3.setText(data._answer3);
        holder.answer4.setText(data._answer4);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (!positionWas.contains(data._question)) {
        positionWas.add(data._question);
        colorTextViews(position, holder);
    }
    return v;
}

void colorTextViews(int position, ViewHolder holder) {

   for (int i = stopIndex; i < wrongAnswers.size(); i++) {
       if (wrongAnswers.get(i)) {
            Log.d(TAG, "position " + position + "i " + i);
            indexRight = answersId.get(position);
            indexWrong = userAnswers.get(position);
            stopIndex = i + 1;
            color(position, holder);
            break;
        }
    }
}

void color(int position, ViewHolder holder) {

    Log.d(TAG, "color position: " + position +  ", red: " + indexWrong + ", green " + indexRight);
    switch (indexRight) {
        case 1:
            holder.answer1.setTextColor(Color.GREEN);
            break;
        case 2:
            holder.answer2.setTextColor(Color.GREEN);
            break;
        case 3:
            holder.answer3.setTextColor(Color.GREEN);
            break;
        case 4:
            holder.answer4.setTextColor(Color.GREEN);
            break;
    }

    switch (indexWrong) {
        case 1:
            holder.answer1.setTextColor(Color.RED);
            break;
        case 2:
            holder.answer2.setTextColor(Color.RED);
            break;
        case 3:
            holder.answer3.setTextColor(Color.RED);
            break;
        case 4:
            holder.answer4.setTextColor(Color.RED);
            break;
    }
}

1 个答案:

答案 0 :(得分:0)

了解convertView特性,您必须确保在重新使用convertView之前重置所有可更改UI元素的样式,只有在每行基础上更改视图上的样式(颜色等)时才需要这样做

因此,如果您确保始终执行&#39; colorTextViews(职位,持有人)&#39; (把它放在这个&#39; 如果条件&#39;)根据当前数据状态设置正确的颜色,它将按你想要的方式工作。

或简化如下:

    Answer correctAnswer = ...;

    holder.question.setText(data._question);
    holder.answer1.setText(data._answer1);
    holder.answer1.setTextColor(data._answer1.equals(correctAnswer)? Color.BLUE : Color.RED);
    holder.answer2.setText(data._answer2);
    holder.answer2.setTextColor(data._answer2.equals(correctAnswer)? Color.BLUE : Color.RED);
    holder.answer3.setText(data._answer3);
    holder.answer3.setTextColor(data._answer3.equals(correctAnswer)? Color.BLUE : Color.RED);
    holder.answer4.setText(data._answer4);
    holder.answer4.setTextColor(data._answer4.equals(correctAnswer)? Color.BLUE : Color.RED);