2017-01-04 24 views
1

這是我的自定義列表視圖類 whem我正在scroling了我們的列表視圖它會選中旱廁選擇單選按鈕無線電集團定製的滾動值會列表視圖適配器選中

private String[] question_name; 
private String[] op11, op22, op33, op44; 
private Activity context; 
RadioGroup rg=null; 
private HashMap<Integer,String>selectitem=new HashMap<>(); 

public QustionAdapter(Activity context, String[] queation, String[] op1, String[] op2, 
         String[] op3, String[] op4) { 
    super(context, R.layout.qustion_custom_list, queation); 
    this.context = context; 
    this.question_name = queation; 
    this.op11 = op1; 
    this.op22 = op2; 
    this.op33 = op3; 
    this.op44 = op4; 
} 
@Override 
public View getView(final int position, View convertView, ViewGroup parent) 
{ 
    LayoutInflater inflater = context.getLayoutInflater(); 

    View listViewItem = inflater.inflate(R.layout.qustion_custom_list, null, false); 

    TextView questionname=(TextView)listViewItem.findViewById(R.id.qustion_tb); 
    RadioButton op1=(RadioButton)listViewItem.findViewById(R.id.op1); 
    RadioButton op2=(RadioButton)listViewItem.findViewById(R.id.op2); 
    RadioButton op3=(RadioButton)listViewItem.findViewById(R.id.op3); 
    RadioButton op4=(RadioButton)listViewItem.findViewById(R.id.op4); 
    rg=(RadioGroup) listViewItem.findViewById(R.id.radio_group); 


    // Toast.makeText(context,"Done :"+rg.getCheckedRadioButtonId(),Toast.LENGTH_LONG).show(); 
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      Model model=new Model(); 

      RadioButton rb=(RadioButton) group.findViewById(checkedId); 
      // Toast.makeText(context,"Done :"+rb.getText(),Toast.LENGTH_LONG).show(); 
      String sel=rb.getText().toString(); 
      selectitem.put(position,sel); 
      model.setSelectResult(selectitem); 
     } 
    }); 


    questionname.setText(question_name[position]); 
    op1.setText(op11[position]); 
    op2.setText(op22[position]); 
    op3.setText(op33[position]); 
    op4.setText(op44[position]); 

    return listViewItem; 
}' 

當我選擇第一無線電組按下它的選擇,但是當我回到收音機組時,它會滾動它的選擇。

+0

你在列表中保存檢查與否。 –

+0

是的,因爲滾動後它會被取消選擇 –

+0

您應該使用POJO類,它將包含問題,選項並進行檢查。更新檢查選中的更改偵聽器。 –

回答

1

在這裏我們去

public class CurrentAffairs extends AppCompatActivity { 
    public static int correct, wrong, marks; 
    DbH db; 
    ArrayList<Question> mcqs; 
    Cursor cur; 
    ListView lv; 
    CustomAdapter c; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.current_affairs); 

     //casting 
     lv = (ListView) findViewById(R.id.lv); 
     mcqs = new ArrayList<>(); 


    enter code here 
     try { 
      db = new DbH(this); 
     } catch (IOException e2) { 

      e2.printStackTrace(); 
     } 

     try { 
      db.createdatabase(); 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 

     db.opendatabase(); 
     cur = db.data(); 

     try { 

      while (cur.moveToNext()) { 
       String mcqss = cur.getString(1); 
       String opta = cur.getString(2); 
       String optb = cur.getString(3); 
       String optc = cur.getString(4); 
       String optd = cur.getString(5); 
       String answ = cur.getString(6); 

       //put data in a listview 
       Question question = new Question(); 
       question.question = mcqss; 
       question.option1 = opta; 
       question.option2 = optb; 
       question.option3 = optc; 
       question.option4 = optd; 
       question.correctanxer = answ; 
       mcqs.add(question); 



       c = new CustomAdapter(CurrentAffairs.this, R.layout.row, R.id.mcqsText, mcqs); 
       lv.setAdapter(c); 


      } 
     } finally { 
      cur.close(); 
     } 
     Button btnshow = (Button) findViewById(R.id.btnshowresult); 
     btnshow.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       StringBuffer sb = new StringBuffer(); 
       sb.append("Correct Answer = " + " " + correct); 
       sb.append(" " + "Wrong Answer = " + " " + wrong); 
       sb.append(" " + "Final Score = " + " " + correct * 5); 

       Toast.makeText(CurrentAffairs.this, sb, Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 



    class CustomAdapter extends ArrayAdapter<Question> { 

     public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<Question> objects) { 
      super(context, resource, textViewResourceId, objects); 


     } 

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 

      LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View v = inflater.inflate(R.layout.row, parent, false); 
      TextView mcqsText = (TextView) v.findViewById(R.id.mcqsText); 
      TextView ans2 = (TextView) v.findViewById(R.id.answer2); 
      TextView anss = (TextView) v.findViewById(R.id.answer); 
      RadioGroup rg = (RadioGroup) v.findViewById(R.id.radioGroup); 

      RadioButton opt1 = (RadioButton) v.findViewById(R.id.optA); 
      RadioButton opt2 = (RadioButton) v.findViewById(R.id.optB); 
      RadioButton opt3 = (RadioButton) v.findViewById(R.id.optC); 
      RadioButton opt4 = (RadioButton) v.findViewById(R.id.optD); 

      Question question = mcqs.get(position); 

      mcqsText.setText(question.question); 
      opt1.setText(question.option1); 
      opt2.setText(question.option2); 
      opt3.setText(question.option3); 
      opt4.setText(question.option4); 
      anss.setText(question.selectedanxer); 
      ans2.setText("Correct answer is = " + question.correctanxer); 

      String test=opt1.getText().toString(); 
      String test2=question.selectedanxer+""; 

      if (test.equals(""+test2)){ 
       opt1.setChecked(true); 
      } 
      if (test2.equals(opt2.getText()+"")){ 
       opt2.setChecked(true); 
      } 
      if (test2.equals(opt3.getText()+"")){ 
       opt3.setChecked(true); 
      } 
      if (test2.equals(opt4.getText()+"")){ 
       opt4.setChecked(true); 
      } 




      rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(RadioGroup group, int checkedId) { 


        Question question1=mcqs.get(position); 
        RadioButton radioButton= (RadioButton) group.findViewById(checkedId); 
        mcqs.get(position).selectedanxer=radioButton.getText().toString(); 
        notifyDataSetChanged(); 



       } 
      }); 

      return v; 
     } 
    } 

    public class Question { 
     String question; 
     String option1; 
     String option2; 
     String option3; 
     String option4; 

     String selectedanxer; 
     String correctanxer; 

    } 

} 
1

您必須使用具有布爾值的所有對象的模型類來檢查和取消選中true false,並設置getter和setter方法,並訪問get view()方法中的值,並選中或取消選中值爲視圖。

 @Override 
     public void onBindViewHolder(final MyViewHolder holder, final inposition)    {  
     final int pos = position; 
     holder.title.setText(list.get(pos).getName()); 
     if (islongpressed) 
     { 
      holder.checkBox.setVisibility(View.VISIBLE); 
     } 
     else holder.checkBox.setVisibility(View.GONE); 

     holder.checkBox.setChecked(list.get(pos).isSelected()); 


try { 
    holder.checkBox.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      CheckBox cb = (CheckBox) v; 

      AudioFile contact =list.get(pos); 

      contact.setSelected(cb.isChecked()); 

      list.get(pos).setSelected(cb.isChecked()); 

     } 
    }); 


} 
+0

請你可以發送代碼 –

+0

我已編輯後審查它。這可以幫助你 –