How to Use Radiogroup in Listview Custom Adapter

How to use RadioGroup in ListView custom adapter?

You need to do two things:

  1. Use mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  2. Make your custom row view implement Checkable. (More info about this here).

Radio Group in custom list view adapter in scrolling value going unchecked

here we go

    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;

}

}

howto group radio buttons in a custom adapter?

I could have been more clear as well. Anyhow, your final assembled XML needs to look like this. You could be doing this in code and that is fine as long as what you're building is returned as such. Each RadioButton must fall within the RadioGroup container.

<RadioGroup
android:id="@+id/rg_configup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
<RadioButton
android:id="@+id/rb_configup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sales"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
</RadioButton>
<RadioButton
android:id="@+id/rb_configup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Regional"
android:layout_above="@+id/rb_configup1"
android:layout_alignLeft="@+id/rb_configup1"
>
</RadioButton>
<RadioButton
android:id="@+id/rb_configup3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Global"
android:layout_above="@+id/rb_configup2"
android:layout_alignLeft="@+id/rb_configup1"
>
</RadioButton>
<RadioButton
android:id="@+id/rb_configup4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ad-hoc"
android:layout_above="@+id/rb_configup3"
android:layout_alignLeft="@+id/rb_configup1"
>
</RadioButton>
</RadioGroup>

Does this make sense, or are you trying to do this in something other than XML?

How to get the values of radio buttons from Custom Listview in Android?

try this :

  rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
int childCount = group.getChildCount();
for (int x = 0; x < childCount; x++) {
RadioButton btn = (RadioButton) group.getChildAt(x);

if (btn.getId() == checkedId) {

System.out.println(btn.getText().toString());

}

}
});

Android: Radio button in custom list view

Here are the key ideas

  • when a RadioButton is checked we must call notifyDataSetChanged(), so that all views get updated.
  • when a RadioButton is checked we must set a selectedPosition, to keep track of which RadioButton is selected
  • Views are recycled inside ListViews. Therefore, their absolute position changes in the ListView. Therefore, inside ListAdapter#getView(), we must call setTag() on each RadioButton. This allows us to determine the current position of the RadioButton in the list when the RadioButton is clicked.
  • RadioButton#setChecked() must be updated inside getView() for new or pre-existing Views.

Here is an example ArrayAdapter I wrote and tested in order to demonstrate these ideas

public class MainActivity extends ListActivity {

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

// I do no use these values anywhere inside the ArrayAdapter. I could, but don't.
final Integer[] values = new Integer[] {1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,};

ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, R.layout.row, R.id.textview, values) {

int selectedPosition = 0;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
RadioButton r = (RadioButton)v.findViewById(R.id.radiobutton);
}
TextView tv = (TextView)v.findViewById(R.id.textview);
tv.setText("Text view #" + position);
RadioButton r = (RadioButton)v.findViewById(R.id.radiobutton);
r.setChecked(position == selectedPosition);
r.setTag(position);
r.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectedPosition = (Integer)view.getTag();
notifyDataSetChanged();
}
});
return v;
}

};
setListAdapter(adapter);
}
}


Related Topics



Leave a reply



Submit