2015-04-05 69 views
1

我是新來的android和試圖創建一個問題的列表視圖,2種類型,mcq和true-false, 與一個問題textview和一個提交按鈕有一個無線電組4個單選按鈕,取決於問題的類型,我隱藏2個單選按鈕,使它看起來像真假問題,一切都在這裏工作正常... ...Android的收音機組裏面的listview不能正常工作

問題出現,當我檢查任何無線電按鈕並向下滾動或向上滾動,該按鈕不保留其狀態,即使在存儲了選擇了哪個選項後,如果找到存儲的選項,我檢查單選按鈕,但它不顯示在UI中檢查,而是顯示在其他選項中顯示問題或某時不同的單選按鈕被檢查。

這裏是我的適配器代碼

public class QuestionAdapter extends BaseAdapter 
{ 

    private ArrayList<QuestionsGetSet> data; 
    private Context acontext; 
    private LayoutInflater inflater; 
    private String TAG="QuestionAdapter"; 

    public QuestionAdapter(Context a, ArrayList<QuestionsGetSet> q) 
    { 
     acontext = a; 
     data = q; 
    } 

    @Override 
    public int getCount() 
    { 
     return data.size(); 
    } 

    @Override 
    public Object getItem(int arg0) 
    { 
     return data.get(arg0); 
    } 

    @Override 
    public long getItemId(int arg0) 
    { 
     return 0; 
    } 

    @Override 
    public View getView(int arg0, View arg1, ViewGroup arg2) 
    { 
     holder hv=null; 
     final int postion = arg0; 
     if (arg1 == null) 
     { 
      inflater = (LayoutInflater) acontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      arg1 = inflater.inflate(R.layout.question_feed, null); 

      hv = new holder(); 
      hv.question = (TextView) arg1.findViewById(R.id.question_content_tv); 
      hv.answer = (TextView) arg1.findViewById(R.id.question_answer_tv); 
      hv.id = (TextView) arg1.findViewById(R.id.question_id_tv); 
      hv.counter = (TextView) arg1.findViewById(R.id.question_counter_tv); 
      hv.r_goup = (RadioGroup) arg1.findViewById(R.id.question_radio_group); 
      hv.option1 = (RadioButton) arg1.findViewById(R.id.question_option_1); 
      hv.option2 = (RadioButton) arg1.findViewById(R.id.question_option_2); 
      hv.option3 = (RadioButton) arg1.findViewById(R.id.question_option_3); 
      hv.option4 = (RadioButton) arg1.findViewById(R.id.question_option_4); 
      hv.submit_btn = (Button) arg1.findViewById(R.id.question_submit_btn); 
      hv.r_goup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
      { 

       @Override 
       public void onCheckedChanged(RadioGroup arg0, int arg1) 
       { 
        Log.d(TAG,"inside oncheckedchanged, position="+postion); 
        data.get(postion).set_ischecked(true); 
        switch (arg1) 
        { 
        case R.id.question_option_1: 
         Log.d(TAG,"Option 1 checked"); 
         data.get(postion).set_whichISchecked(1); 
         break; 
        case R.id.question_option_2: 
         Log.d(TAG,"Option 2 checked"); 
         data.get(postion).set_whichISchecked(2); 
         break; 
        case R.id.question_option_3: 
         Log.d(TAG,"Option 3 checked"); 
         data.get(postion).set_whichISchecked(3); 
         break; 
        case R.id.question_option_4: 
         Log.d(TAG,"Option 4 checked"); 
         data.get(postion).set_whichISchecked(4); 
         break; 
        } 
       } 
      }); 
      arg1.setTag(hv); 
     } 
     else 
     { 
      hv = (holder) arg1.getTag(); 
     } 

     QuestionsGetSet qgs = data.get(postion); 
     if(qgs.get_ischecked()) 
     { 
      Log.d(TAG,"inside if of is checked"); 
      int i=qgs.get_whichISchecked(); 
      Log.d(TAG,"inside if of is checked, which is check="+i); 
      switch(i) 
      { 
      case 1: 
       hv.option1.setChecked(true); 
       hv.option2.setChecked(false); 
       hv.option3.setChecked(false); 
       hv.option4.setChecked(false); 
       break; 
      case 2: 
       hv.option2.setChecked(true); 
       hv.option1.setChecked(false); 
       hv.option3.setChecked(false); 
       hv.option4.setChecked(false); 
       break; 
      case 3: 
       hv.option3.setChecked(true); 
       hv.option1.setChecked(false); 
       hv.option2.setChecked(false); 
       hv.option4.setChecked(false); 
       break; 
      case 4: 
       hv.option4.setChecked(true); 
       hv.option1.setChecked(false); 
       hv.option2.setChecked(false); 
       hv.option3.setChecked(false); 
       break; 
      } 
     } 
     else if(!qgs.get_ischecked()) 
     { 
      Log.d(TAG,"inside else of is checked"); 
      hv.option1.setChecked(false); 
      hv.option2.setChecked(false); 
      hv.option3.setChecked(false); 
      hv.option4.setChecked(false); 
     } 


     /*** below all code is working fine ***/ 

     /// used to create a true-false or mcq type ui, working fine 
     if (qgs.get_isBool()) 
     { 
      hv.option1.setText("True"); 
      hv.option2.setText("False"); 
      hv.option3.setVisibility(View.GONE); 
      hv.option4.setVisibility(View.GONE); 
     } else 
     { 
      hv.option1.setText(qgs.get_option_1()); 
      hv.option2.setText(qgs.get_option_2()); 
      hv.option3.setVisibility(View.VISIBLE); 
      hv.option4.setVisibility(View.VISIBLE); 
      hv.option3.setText(qgs.get_option_3()); 
      hv.option4.setText(qgs.get_option_4()); 
     } 
     /// used to check if question can be submitted or not, working fine 
     if (qgs.get_isvalid() != null) 
     { 
      if (!qgs.get_isvalid()) 
      { 
       hv.submit_btn.setText("Question Closed"); 
       hv.submit_btn.setBackgroundResource(R.drawable.btn_invalid); 
      } else 
      { 
       hv.submit_btn.setText("Submit"); 
       hv.submit_btn.setBackgroundResource(R.drawable.btn_valid); 
      } 
     } 
     /// used to check if answer is present, then showed to user, working fine 
     if (qgs.get_answer() != null) 
     { 
      hv.answer.setVisibility(View.VISIBLE); 
      hv.answer.setText(qgs.get_answer()); 
     } else 
     { 
      hv.answer.setVisibility(View.GONE); 
      hv.answer.setText(""); 
     } 
     hv.question.setText(qgs.get_question()); 
     hv.id.setText(qgs.get_id() + ""); 
     hv.counter.setText(qgs.get_counter() + ""); 

     return arg1; 
    } 

    class holder 
    { 
     TextView question, answer, id, counter; 
     Button submit_btn; 
     RadioGroup r_goup; 
     RadioButton option1, option2, option3, option4; 
    } 
} 

這裏是QuestionGetSet代碼

public class QuestionsGetSet 
{ 
    private String question,answer,option_1,option_2,option_3,option_4; 
    private Boolean isBool=false,isvalid=false; 
    private long id,counter; 

    private Boolean isChecked=false; 
    private int whichISchecked; 

    public void set_ischecked(Boolean a) 
    { 
     this.isChecked=a; 
    } 
    public Boolean get_ischecked() 
    { 
     return this.isChecked; 
    } 
    public void set_whichISchecked(int a) 
    { 
     this.whichISchecked=a; 
    } 
    public int get_whichISchecked() 
    { 
     return this.whichISchecked; 
    } 


    public void set_question(String q) 
    { 
     this.question=q; 
    } 
    public String get_question() 
    { 
     return this.question; 
    } 
    public void set_answer(String q) 
    { 
     this.answer=q; 
    } 
    public String get_answer() 
    { 
     return this.answer; 
    } 
    public void set_option_1(String q) 
    { 
     this.option_1=q; 
    } 
    public String get_option_1() 
    { 
     return this.option_1; 
    } 
    public void set_option_2(String q) 
    { 
     this.option_2=q; 
    } 
    public String get_option_2() 
    { 
     return this.option_2; 
    } 
    public void set_option_3(String q) 
    { 
     this.option_3=q; 
    } 
    public String get_option_3() 
    { 
     return this.option_3; 
    } 
    public void set_option_4(String q) 
    { 
     this.option_4=q; 
    } 
    public String get_option_4() 
    { 
     return this.option_4; 
    } 

    public void set_isBool(Boolean q) 
    { 
     this.isBool=q; 
    } 
    public Boolean get_isBool() 
    { 
     return this.isBool; 
    } 
    public void set_isvalid(Boolean q) 
    { 
     this.isvalid=q; 
    } 
    public Boolean get_isvalid() 
    { 
     return this.isvalid; 
    } 

    public void set_id(long q) 
    { 
     this.id=q; 
    } 
    public long get_id() 
    { 
     return this.id; 
    } 
    public void set_counter(long q) 
    { 
     this.counter=q; 
    } 
    public long get_counter() 
    { 
     return this.counter; 
    } 
} 

回答

0

在QuestionAdapter類

不要,如果條件只用別的

外匯使用別的

更換

else if(!qgs.get_ischecked()) 

else 
+0

對不起兄弟,這不是正確的語法。 else語句沒有條件,即使只有「else」也不能解決問題。 – user3024990 2015-04-05 15:20:01

相關問題