began to learn Android. The question arose, the google did not particularly help.
There is a ListVIEW filled with some of the Items. Each Item consists of TextView, Radiogroup, RadioButton, respectively. Actually, how to get the values selected by the user in the Radiogroup by pressing the button. The amount of Item may be arbitrary. I do not understand how to do it.
It looks like ACTIVITY:
& lt; xml version = "1.0" encoding = "UTF-8"? & gt;
& lt; linearlayout
XMLNS: Android = "http://schemas.android.com/apk/res/android"
XMLNS: App = "http://schemas.android.com/apk/res-auto"
XMLNS: Tools = "http://schemas.android.com/tools"
Android: layout_width = "Match_Parent"
Android: Layout_Height = "Match_Parent"
Android: PaddingBotTom = "@ dimen / activity_vertical_margin"
Android: paddingleft = "@ dimen / activity_horizontal_margin"
Android: paddingright = "@ dimen / activity_horizontal_margin"
Android: paddingtop = "@ dimen / activity_vertical_margin"
Android: Orientation = "Vertical"
App: layout_behavior = "@ String / AppBar_Scrollling_View_Behavior"
Tools: Context = ". QuestionssCrollactivity"
Tools: Showin = "@ layout / activity_questions_scroll"
& gt;
& lt; listview
Android: id = "@ + id / listview"
Android: layout_width = "Match_Parent"
Android: layout_height = "wrap_content"
Android: ChoICEMODE = "NONE"
Android: Layout_Weight = "1"
/ & gt;
& lt; button
Android: layout_width = "Match_Parent"
Android: layout_height = "wrap_content"
Android: Text = "Check answers"
Android: id = "@ + ID / Checkanswers"
/ & gt;
& lt; / linearlayout & gt;
Actually Question.xml is the Item of which listview
& lt; xml version = "1.0" encoding = "UTF-8"? & gt;
& lt; linearlayout
XMLNS: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "Vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: Layout_AlignParentTop = "True"
Android: layout_alignparentleft = "True"
Android: Layout_AlignParentStart = "True"
Android: weightsum = "1" & gt;
& lt; TextView
Android: id = "@ + ID / QuestionNumberTextExTView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: TextAppearance = "? Android: attr / textappeaancemedium"
Android: Text = ""
Android: layout_gravity = "center_horizontal" / & gt;
& lt; TextView
Android: layout_width = "Match_Parent"
Android: layout_height = "wrap_content"
Android: TextAppearance = "? Android: attr / textappeaancemedium"
Android: Text = "Test Question"
Android: id = "@ + id / QuestionTextView"
Android: Layout_Weight = "0.17"
Android: layout_margintop = "15dp"
Android: TextSize = "24SP"
Android: layout_marginbottom = "15dp" / & gt;
& lt; linearlayout
Android: Orientation = "Vertical"
Android: layout_width = "Match_Parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal" & gt;
& lt; radiogroup
Android: id = "@ + id / radiogroup"
Android: layout_width = "Match_Parent"
Android: Layout_Height = "Match_Parent"
& gt;
& lt; RadioButton
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Text = "Test Answer"
Android: id = "@ + ID / AnswerRadioButton1"
Android: Checked = "False"
Android: paddingtop = "5dp"
Android: paddingbottom = "5dp" / & gt;
& lt; RadioButton
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Text = "Test Answer"
Android: id = "@ + ID / AnswerRadioButton2"
Android: Checked = "False" / & gt;
& lt; RadioButton
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Text = "Test Answer"
Android: id = "@ + ID / AnswerRadioButton3"
Android: Checked = "False" / & gt;
& lt; RadioButton
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Text = "Test Answer"
Android: id = "@ + ID / AnswerRadioButton4"
Android: Checked = "False" / & gt;
& lt; / radiogroup & gt;
& lt; / linearlayout & gt;
& lt; / linearlayout & gt;
and adapter
Public Class Questionadapter EXTENDS Arrayadapter & LT; Question & GT; {
Public Questionadapter (CONTEXT CONTEXT, ARRAYLIST & LT; Question & GT; Questions) {
Super (Context, 0, Questions);
}
@Override
Public View GetView (Int Position, View ConvertView, ViewGroup Parent) {
Final Question Question = GetItem (Position);
ViewHOLDER Viewholder;
if (convertView == NULL) {
convertView = layoutinflater.from (getContext (). Inflate (R.Layout.Question, Parent, False);
Viewholder = New Viewholder ();
viewholder.QuestionNumberTextView = (TextView) convertView.findViewByid (R.ID.QuestionNumberTextView);
Viewholder.QuestionTextView = (textView) convertView.findViewByid (R.ID.QuestionTextView);
viewholder.radiogroup = (Radiogroup) convertView.findViewByid (R.ID.RADiOGroup);
Viewholder.answerRadioButton1 = (RadioButton) convertView.findViewByid (R.ID.answerRadioButton1);
Viewholder.answerradioButton2 = (RadioButton) convertView.findViewByid (R.ID.answerRadioButton2);
Viewholder.answerRadioButton3 = (RadioButton) convertView.findViewByid (R.ID.answerRadioButton3);
Viewholder.answerRadioButton4 = (RadioButton) convertView.findViewByid (R.ID.answerRadioButton4);
ConvertView.Settag (Viewholder);
} else {
Viewholder = (Viewholder) convertView.gettag ();
}
List & LT; Answer & GT; Answerlist = question.getanswers ();
If (Answerlist! = NULL) {
viewholder.questionNumberTextView.setText ("Question #" + (Position + 1));
viewholder.QuestionTextView.SetText (Question.getQuestion ());
Viewholder.answerRadioButton1.SetText (Answerlist.get (0) .Getanswer ());
Viewholder.answerRadioButton2.SetText (Answerlist.get (1) .Getanswer ());
Viewholder.answerRadioButton3.settext (Answerlist.get (2) .Getanswer ());
Viewholder.answerRadioButton4.SetText (Answerlist.get (3) .Getanswer ());
}
Return convertView;
}
STATIC CLASS Viewholder {
TextView QuestionNumberTextView;
TextView QuestionTextView;
Radiogroup Radiogroup;
RadioButton AnswerRadioButton1;
RadioButton AnswerradioButton2;
RadioButton AnswerRadioButton3;
RadioButton AnswerRadioButton4;
}
}
Answer 1, Authority 100%
Everything is very simple – hang the listener of clicks in the adapter on the Radiogroup
and register the COLLEC (callback interface) of the data obtained if they need to be transmitted to activat.
To identify the transmitted data (in which Ateme there is a click) through the COLLEK, pass the current value position
from the adapter.
If you need to receive data from Items not in real time (to process each click separately), but, for example, by clicking on some of the selection completion button, then it’s still easier to the COLLECT, information from the listener is going to some kind Storage (collection of data, models) and a method is created in the adapter returning this collection.
When clicking on the button, activat appeals to this method and receives a data set with the selection results.
Also the above collection of data on states in Radiogroup
will be needed to store the current selection and recovery of the ahemes during the scrolling, since the atems are periodically rewrong and if not saving a state, the choice will be lost.
Sample adapter code without a column may look like this:
Public Class Questionadapter EXTENDS Arrayadapter & LT; Question & GT; {
int [] checkedAnswers;
public QuestionAdapter (Context context, ArrayList & lt; Question & gt; questions) {
super (context, 0, questions);
checkedAnswers = new int [questions.size ()];
}
@Override
public View getView (final int position, View convertView, ViewGroup parent) {
final Question question = getItem (position);
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from (getContext ()). inflate (R.layout.question, parent, false);
viewHolder = new ViewHolder ();
viewHolder.questionNumberTextView = (TextView) convertView.findViewById (R.id.questionNumberTextView);
viewHolder.questionTextView = (TextView) convertView.findViewById (R.id.questionTextView);
viewHolder.radioGroup = (RadioGroup) convertView.findViewById (R.id.radioGroup);
// remember which element is selected at a certain position
viewHolder.radioGroup.setOnCheckedChangeListener (new RadioGroup.OnCheckedChangeListener () {
@Override
public void onCheckedChanged (RadioGroup group, int checkedId) {
switch (checkedId) {
case -1: // nothing is selected
checkedAnswers [position] = -1;
break;
case R.id.answerRadioButton1:
checkedAnswers [position] = 0;
break;
case R.id.answerRadioButton2:
checkedAnswers [position] = 1;
break;
case R.id.answerRadioButton3:
checkedAnswers [position] = 2;
break;
case R.id.answerRadioButton4:
checkedAnswers [position] = 3;
break;
default:
break;
}
}
});
viewHolder.answerRadioButton1 = (RadioButton) convertView.findViewById (R.id.answerRadioButton1);
viewHolder.answerRadioButton2 = (RadioButton) convertView.findViewById (R.id.answerRadioButton2);
viewHolder.answerRadioButton3 = (RadioButton) convertView.findViewById (R.id.answerRadioButton3);
viewHolder.answerRadioButton4 = (RadioButton) convertView.findViewById (R.id.answerRadioButton4);
convertView.setTag (viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag ();
}
List & lt; Answer & gt; answerList = question.getAnswers ();
if (answerList! = null) {
viewHolder.questionNumberTextView.setText ("Question #" + (position + 1));
viewHolder.questionTextView.setText (question.getQuestion ());
// restore the selection in the RadioGroup
if (checkedAnswers [position] == -1) viewHolder.radioGroup.clearCheck ();
else viewHolder.radioGroup.check (((RadioButton) viewHolder.radioGroup.getChildAt (checkedAnswers [position])). getId ());
viewHolder.answerRadioButton1.setText (answerList.get (0) .getAnswer ());
viewHolder.answerRadioButton2.setText (answerList.get (1) .getAnswer ());
viewHolder.answerRadioButton3.setText (answerList.get (2) .getAnswer ());
viewHolder.answerRadioButton4.setText (answerList.get (3) .getAnswer ());
}
return convertView;
}
// the method returns an array with the results of the selection in the list
public int [] getCheckedAnswers () {
return checkedAnswers;
}
static class ViewHolder {
TextView questionNumberTextView;
TextView questionTextView;
RadioGroup radioGroup;
RadioButton answerRadioButton1;
RadioButton answerRadioButton2;
RadioButton answerRadioButton3;
RadioButton answerRadioButton4;
}
}
Here we are when clicking in Radiogroup
read which of the RadioButton
click the user and save it the choice, then, when forming Atema, restore the user’s choice to avoid the loss of data entered Due to the testing of the list item.
Method getCheckedanswers ()
Returns an array of values where the position in the array will correspond to the position in the list, the values are as follows:
- -1 – nothing selected
- 0 – The first item is selected in Radiogroup
- 1 – selected second paragraph
- 2 – the third item
- 3 – selected fourth item
is selected
Accept from activist (for example, in the click Handler on the Checkanswers button) as follows:
int [] checkdanswers = adapter.getcheckedanswers ();
PS: This code has nowhere to test, so it is rather a template for demonstrating the solution than the finished solution for the copy-paste.