我正在使用带有光标适配器的列表视图,每个列表视图行有一个可点击的选项,一旦用户点击任何这些项目我突出显示绿色的正确选项,如果用户选择了错误选项,则选择一个红色。
现在,当我滚动列表视图时,它突出显示另一行绿色和红色,即使我没有选择这些选项。
我知道问题是当列表视图重用行然后它正在发生但是如何避免它们,任何正文都可以建议我。
下面是参考代码
new java.awt.Color(0, 0, 0, 0)
答案 0 :(得分:0)
选择项目后,您应将其保存到数据库中。每行的状态,无论是否被检查,都应由BindView
设置,具体取决于数据库中的数据
答案 1 :(得分:0)
示例代码。这段代码是指示,它不会编译,但应该显示你的想法。想法是:
每次使用前都必须初始化视图。否则你会的 处于先前使用状态的视图。在bindView中你应该这样做: 清除所有元素的背景,如果用户已经设置背景 选择了答案(因为你需要保留用户选择)。
示例代码。我删除了代码的某些部分以缩短代码。
public class RoundsListAdapter extends CursorAdapter {
private String mOptionOne = "option_1";
private String mOptionTwo = "option_2";
private String mOptionThree = "option_3";
private String mOptionFour = "option_4";
private LayoutInflater mLayoutInflator;
private Context mContext;
private String[] mAnswers;
public RoundsListAdapter(Context context, Cursor c, boolean autoQuery,int windowHeight, int windowWidth) {
super(context, c,autoQuery);
mLayoutInflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//init array with answer. need to do the same or similar in swapCursor
mAnswers = new String[c.getCount()];
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View customView = mLayoutInflator.inflate(R.layout.talk_row_list,null);
return customView;
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
final CustomButtonFlat option_1 = (CustomButtonFlat)view.findViewById(R.id.talks_answerA);
...
//Define current position
final currentPosition = cursor.getPosition();
String optionA = cursor.getString(cursor.getColumnIndexOrThrow(TalkTable.TALK_OPTION_1));
...
//reset background color and background resources
option_1.setBackgroundColor(DEFAULT_COLOR_HERE);
option_1.setBackgroundResource(DEFAULT_RESOURCE_HERE_OR_ZERO)
...
//same for other "option_X"
option_4.setBackgroundColor(DEFAULT_COLOR_HERE);
option_4.setBackgroundResource(DEFAULT_RESOURCE_HERE_OR_ZERO)
if(optionA == null || optionA.length()<=0){
option_1.setVisibility(View.GONE);
option_1SepartorView.setVisibility(View.GONE);
}else{
option_1.setVisibility(View.VISIBLE);
option_1SepartorView.setVisibility(View.VISIBLE);
option_1.setText(cursor.getString(cursor.getColumnIndexOrThrow(TalkTable.TALK_OPTION_1)));
}
...
// only set these listeners if MCQ in the talks are available
final String mcqAnswer = cursor.getString(cursor.getColumnIndexOrThrow(TalkTable.TALK_ANSWER));
Log.d(TAG," Talk mcq answer is = " + mcqAnswer);
//init color and background for already answered questions
if (mcqAnswer.equals(mOptionOne){
if (mcqAnswer.equals(mAnswers[currenPosition])){
option_1.setBackgroundResource(R.color.correctMCQAnswer);
} else {
option_1.setBackgroundColor(Color.RED); // settings wrong color
option_2.setBackgroundResource(R.color.correctMCQAnswer);
}
} if (mcqAnswer.equals(mOptionTwo)){
//the same but for optionTwo
}
...
//add same conditions for each option
option_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//save choice. For others it will be mOptionTwo, mOptionThree, mOptionFour
mAnswers[currenPosition] = mOptionOne;
if(mcqAnswer.equalsIgnoreCase(mOptionOne)){
option_1.setBackgroundResource(R.color.correctMCQAnswer);
}else if(mcqAnswer.equalsIgnoreCase(mOptionTwo)){
option_1.setBackgroundColor(Color.RED); // settings wrong color
option_2.setBackgroundResource(R.color.correctMCQAnswer);
// setting correct color for correct answer
Log.d(TAG, " Setting button color answer 2");
}else
...
}
});
...
//Other listeners
}
}