我正在创建一个包含TextView列表的ListView。我想根据某些条件在每个ListView项中添加一个额外的CheckBox或EditText。如何设置特定CheckBox和EditText的可见性?
这是我的代码,目前它会影响ListView中所有CheckBoxes和EditTexts的可见性。
private class AdapterQuestions extends ArrayAdapter<Question> {
private List<Question> listQuestions;
public AdapterQuestions(Context context, List<Question> list_questions){
super(context, R.layout.listitem_question, list_questions);
listQuestions = list_questions;
}
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = LayoutInflater.from(getContext());
View item = inflater.inflate(R.layout.listitem_question, null);
TextView tvQuestionText = (TextView)item.findViewById(R.id.tvQuestionText);
tvQuestionText.setText("[" + listQuestions.get(position).getQuestionId().toString() + "] " + listQuestions.get(position).getQuestionTxt());
CheckBox cb = (CheckBox) item.findViewById(R.id.cbQuestionCheckbox);
EditText et = (EditText) item.findViewById(R.id.etQuestionMultiline);
if(listQuestions.get(position).getQuestionType().toString()=="1"){
cb.setVisibility(View.VISIBLE);
} else if(listQuestions.get(position).getQuestionType().toString()=="4"){
et.setVisibility(View.VISIBLE);
}
return item;
}
}
答案 0 :(得分:1)
比较Strings
时,您必须使用以下方法:
.equals()
即:
if(listQuestions.get(position).getQuestionType().toString().equals("1") ){
cb.setVisibility(View.VISIBLE);
} else if(listQuestions.get(position).getQuestionType().toString().equals("4") ){
et.setVisibility(View.VISIBLE);
}
答案 1 :(得分:0)
将CheckBox或EditText设置为不可见,如:
if(listQuestions.get(position).getQuestionType().toString()=="1"){
cb.setVisibility(View.VISIBLE);
et.setVisibility(View.INVISIBLE);
} else if(listQuestions.get(position).getQuestionType().toString()=="4"){
et.setVisibility(View.VISIBLE);
cb.setVisibility(View.INVISIBLE);
}