2017-02-21 64 views
0

我創建了一個包含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; 
    } 

} 
+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); 
} 
+0

謝謝!它做了詭計。 – Edgar

+0

@埃德加,不客氣 –

0

設置複選框或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); 
} 
相關問題