Reputation: 4082
I am creating a number of ( dynamic ) radiogroup in a linear layout. Each having 4 radio buttons in it. There are Some TextViews
inside that linearLayout.
My question is On a button press, I need to get all the radioGroup for fetching data.
I tried the following, but getting the whole count including textViews
LinearLayout ll=(LinearLayout)findViewById(R.id.questions_lay);
int childcount = ll.getChildCount();
Any idea ? Thanks in advance
Upvotes: 2
Views: 2458
Reputation: 106
The following code is for TextView. You can use this for any type of view.
//Recursively search for TextView in given ViewGroup
public int getTextViewCount(ViewGroup viewGroup) {
int count = 0;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View childView = viewGroup.getChildAt(i);
if (childView instanceof TextView) {
count++;
}
if (childView instanceof ViewGroup) {
count += getTextViewCount((ViewGroup) childView);
}
}
return count;
}
Upvotes: 0
Reputation: 13520
You can use
public void getViewType(ViewGroup viewGroup)
{
for (int index = 0; index < viewGroup.getChildCount(); index++)
{
View childView = viewGroup.getChildAt(index);
if (childView instanceof RadioGroup)
{
System.err.println("RadioGroup " + index);
getViewType((ViewGroup) childView);
}
else if (childView instanceof RadioButton)
{
System.err.println("RadioButton " + index);
}
else if (childView instanceof TextView)
{
System.err.println("TextView " + index);
}
}
}
When you get the instanceOf
RadioGroup
you call getViewType
again if you want to access the RadioButton
within that RadioGroup
Upvotes: 2
Reputation: 2224
Look at this tutorial, how they are fetching data through "radio_button".
For example to fetch data using radio button use this:
RadioButton rBtnUp = (RadioButton) findViewById(R.id.xml_file_forUPID);
RadioButton rBtnDown = (RadioButton) findViewById(R.id.XML_file_forDOWNID);
TextView txtCount = (TextView) findViewById(R.id.txtCountId);
txtCount.setText(String.valueOf(count)); // Display initial count
Button btnCount = (Button) findViewById(R.id.btnCountId);
// Process the button on-click event
btnCount.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (rBtnUp.isChecked()) { // Counting up
count++;
} else if (rBtnDown.isChecked()) { // Counting down
count--;
}
txtCount.setText(String.valueOf(count));
}
Upvotes: 0