我有两种答案的调查:
1)打开(复选框或单选按钮)+(用户可输入文字) 2)普通(复选框或单选按钮)
当我有一个像20+元素这样的大型列表时,ListView会显示错误的答案类型。
我猜问题是getItem(),因为答案的类型取决于它的数据。
我已经尝试了很多方法来解决这个问题,甚至没有提到所有的stackoverflow链接。 也许,有人可以帮助我。
public class AnswerAdapter extends ArrayAdapter<Answer> {
public interface ActionsListener {
void onCheckClicked(View v, Answer answer, int position);
void onRadioClicked(View v, Answer answer, int position);
void onOpenClicked(View v, Answer answer, int position);
}
private Display mDisplay;
private LayoutInflater mInflater;
private Context mContext;
private ActionsListener mActionsListener;
private String language;
private ArrayList<Answer> mList;
public AnswerAdapter(Context context, ArrayList<Answer> list, String language, ActionsListener actionsListener) {
super(context, R.layout.item_answer, list);
mList = list;
mInflater = LayoutInflater.from(context);
mContext = context;
mActionsListener = actionsListener;
this.language = language;
mDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
}
@Nullable
@Override
public Answer getItem(int position) {
return super.getItem(this.getPosition(mList.get(position)));
}
@Override
public View getView(final int position, final View convertView,
ViewGroup parent) {
ViewHolder holder;
View row = convertView;
if (convertView == null) {
row = mInflater.inflate(R.layout.item_answer, parent, false);
holder = new ViewHolder();
holder.answerTitle = (TextView) row.findViewById(R.id.answer_title);
holder.answerRadio = (AppCompatRadioButton) row.findViewById(R.id.answer_radio);
holder.answerCheck = (AppCompatCheckBox) row.findViewById(R.id.answer_check);
holder.answerPicture = (ImageView) row.findViewById(R.id.answer_picture);
holder.answerOpen = (EditText) row.findViewById(R.id.answer_open);
holder.answerRank = (Button) row.findViewById(R.id.answer_rank);
row.setTag(holder);
} else
holder = (ViewHolder) row.getTag();
final Answer answer = getItem(position);
final ViewHolder viewHolderFinal = holder;
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.answer_check:
mActionsListener.onCheckClicked(v, answer, position);
notifyDataSetChanged();
break;
case R.id.answer_radio:
mActionsListener.onRadioClicked(v, answer, position);
notifyDataSetChanged();
break;
case R.id.answer_title:
case R.id.answer_picture:
if (viewHolderFinal.answerCheck.isEnabled() && viewHolderFinal.answerRadio.isEnabled())
if (answer.getPolyAnswer()) {
viewHolderFinal.answerCheck.setChecked(!viewHolderFinal.answerCheck.isChecked());
onClick(viewHolderFinal.answerCheck);
} else {
viewHolderFinal.answerRadio.setChecked(true);
onClick(viewHolderFinal.answerRadio);
}
break;
case R.id.answer_open:
mActionsListener.onOpenClicked(v, answer, position);
if (viewHolderFinal.answerCheck.isEnabled() && viewHolderFinal.answerRadio.isEnabled()) {
if (answer.getPolyAnswer()) {
onClick(viewHolderFinal.answerCheck);
} else {
onClick(viewHolderFinal.answerRadio);
}
}
break;
}
}
};
holder.answerTitle.setOnClickListener(onClickListener);
holder.answerPicture.setOnClickListener(onClickListener);
holder.answerOpen.setOnClickListener(onClickListener);
holder.answerOpen.setHint(language); //language[6]
holder.answerTitle.setText(answer.getTitle());
holder.answerTitle.setTextColor(ContextCompat.getColor(mContext, R.color.colorUIWhite));
if (answer.getPolyAnswer()) {
holder.answerCheck.setVisibility(View.VISIBLE);
holder.answerCheck.setChecked(answer.getCheck());
holder.answerCheck.setEnabled(answer.getEnabled());
holder.answerCheck.setOnClickListener(onClickListener);
} else {
holder.answerRadio.setVisibility(View.VISIBLE);
holder.answerRadio.setChecked(answer.getCheck());
holder.answerRadio.setOnClickListener(onClickListener);
}
if (answer.getIsOpenAnswer()) {
holder.answerOpen.setVisibility(View.VISIBLE);
holder.answerOpen.setText(answer.getOpenAnswer());
holder.answerOpen.setTextColor(ContextCompat.getColor(mContext, R.color.colorUIWhite));
holder.answerOpen.setHintTextColor(ContextCompat.getColor(mContext, R.color.colorUIWhite));
holder.answerOpen.setError(answer.getOpenAnswerError());
}
return row;
}
private static class ViewHolder {
TextView answerTitle;
AppCompatRadioButton answerRadio;
AppCompatCheckBox answerCheck;
ImageView answerPicture;
EditText answerOpen;
Button answerRank;
}
}
答案 0 :(得分:2)
查看您的getView()
方法,我假设您最终也会遇到同时显示收音机和支票同时可见的情况。
因为getView()
可以回收视图(当convertView不为null时),任何显示元素都会保持其可见性。
所以你应该在你的answer.getIsOpenAnswer()
添加一个else,否则你将answerOpen的可见性设置为INVISIBLE或GONE(取决于你的设计)。
在你的answer.getPolyAnswer()的if语句中,你需要将answerRadio设置为INVISIBLE或GONE,并在你的其他地方answerCheck to INVISIBLE或GONE。