我創建了一個包含TextView列表的ListView。我想根據某些條件在每個ListView項目內添加一個額外的CheckBox或EditText。我如何設置特定CheckBox和EditText的可見性?如何根據條件更改ListView項目中CheckBox的可見性?
這是我的代碼,目前它會影響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;
}
}
你嘗試過我回答? –