title: Android: Get values of listview with multiple row layout
body: <p>I have a listview with multiple row layouts (radio group, edit text, text view), with multiple choice mode. I want to retrieve it's selected data (user-selected or typed data) on a submit button.</p>
  
  <p>This is the code from my main file:</p>
  
  <pre><code>public class GraduatingSurvey extends Fragment {
  
  
      private static final String LOGTAG = "log" ;
      public String stdcode = "024-15-16079";
      Button submit;
      RadioGroup radioGroup;
      RadioButton radioButton;
      EditText comment;
  
      ArrayList&lt;GraduatingSurveyModel&gt; graduatingModelList;
      ListView listView;
      View view;
  
      public GraduatingSurvey() {
          // Required empty public constructor
      }
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          view =  inflater.inflate(R.layout.fragment_graduating_survey, container, false);
          submit = (Button) view.findViewById(R.id.btn_graduate_submit);
          listView = (ListView) view.findViewById(R.id.graduate_list);
          graduatingModelList = new ArrayList&lt;GraduatingSurveyModel&gt;();
  
          //handle submit form Event.
          submit.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  final int child=listView.getAdapter().getCount();
                  Log.i("radio", "child: " + child);
                  SparseBooleanArray checked = listView.getCheckedItemPositions(); //Is returning empty
                  Log.i("radio", "sparseArray: " + checked);
                  for(int i=0;i&lt;child;i++) {
                      int type = listView.getAdapter().getItemViewType(i);
                      Log.i("radio", "type: " + type);
                      if(type == 2){
                          // Want to retrieve data here
  
                      }
                      if(type == 0)
                      {
  
                      }
                      if(type == 1){
  
                      }
  
                  }
  
              }
          });
  
          NetAsync(view);
          return view;
      }
  </code></pre>
  
  <p>This is the adapter:</p>
  
  <pre><code>public class GraduatingSurveyAdapter extends ArrayAdapter&lt;GraduatingSurveyModel&gt; {
  
      private static final int TYPE_ITEM1 = 0;
      private static final int TYPE_ITEM2 = 1;
      private static final int TYPE_ITEM3 = 2;
      int type;
      ArrayList&lt;GraduatingSurveyModel&gt; ArrayListSurvey;
      int Resource;
      Context context;
      LayoutInflater vi;
      public GraduatingSurveyAdapter(Context context, int resource, ArrayList&lt;GraduatingSurveyModel&gt; objects) {
          super(context, resource, objects);
          ArrayListSurvey = objects;
          Resource = resource;
          this.context = context;
          vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
  
      @Override
      public int getItemViewType(int position) {
  
          if (position == 14){
              type = TYPE_ITEM1;
          } else if  (position == 23 || position == 24 || position == 25){
              type = TYPE_ITEM2;
          }
          else
          {
              type= TYPE_ITEM3 ;
          }
          return type;
      }
  
      @Override
      public int getViewTypeCount() {
          return 3;
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
  
          ViewHolder holder;
          //first Item or row is being created hence ConvertView is NULL
          if(convertView == null){
  
              if (type == TYPE_ITEM2) {
                  convertView = vi.inflate(R.layout.graduates_survey_comment,null);
                  holder = new ViewHolder();
                  holder.tv_question = (TextView) convertView.findViewById(R.id.graduate_question);
                  holder.tv_comment = (TextView) convertView.findViewById(R.id.graduate_comment);
              }
              else if (type == TYPE_ITEM1) {
                  convertView = vi.inflate(R.layout.graduates_survey_subheader,null);
                  holder = new ViewHolder();
                  holder.tv_question = (TextView) convertView.findViewById(R.id.graduate_question);
              }
             else {
                  //infalte layout of normaltype
  
                  //Inflating items only once
                  convertView = vi.inflate(Resource,null);
                  holder = new ViewHolder();
  
                  holder.tv_question = (TextView) convertView.findViewById(R.id.graduate_question);
                  holder.tv_comment = (TextView) convertView.findViewById(R.id.graduate_comment);
                  holder.rg = (RadioGroup) convertView.findViewById(R.id.graduate_radioGroup);
                  holder.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                      @Override
                      public void onCheckedChanged(RadioGroup group, int checkedId) {
                         /* RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
                          Log.i("radio", "onCheckedChanged: " + radioButton + " " + checkedId);
                          notifyDataSetChanged();*/
                          Integer pos = (Integer) group.getTag();
                          GraduatingSurveyModel element = ArrayListSurvey.get(pos);
                          switch (checkedId) {
                              case R.id.graduate_sAgree:
                                  element.current = GraduatingSurveyModel.ANSWER_ONE_SELECTED;
                                  break;
                              case R.id.graduate_agree:
                                  element.current = GraduatingSurveyModel.ANSWER_TWO_SELECTED;
                                  break;
                              case R.id.graduate_uncertain:
                                  element.current = GraduatingSurveyModel.ANSWER_THREE_SELECTED;
                                  break;
                              case R.id.graduate_disagree:
                                  element.current = GraduatingSurveyModel.ANSWER_FOUR_SELECTED;
                                  break;
                              case R.id.graduate_sDisagree:
                                  element.current = GraduatingSurveyModel.ANSWER_FIVE_SELECTED;
                                  break;
                              default:
                                  element.current = GraduatingSurveyModel.NONE; // Something was
  
                          }
  
  
                      }
                  });
              }
  
              convertView.setTag(holder);
          }else{
              //reuse code. (Inflate items only once then reuse them.)
              holder = (ViewHolder) convertView.getTag();
          }
  
          // get info from ArrayList(that came through JSON), and set it in the View.
  
          if (type == TYPE_ITEM1) {
              holder.tv_question.setText(ArrayListSurvey.get(position).getQuestion());
          }
          else if (type == TYPE_ITEM2) {
              holder.tv_question.setText(ArrayListSurvey.get(position).getQuestion());
          }
          else{
              holder.rg.setTag(new Integer(position));
              holder.tv_question.setText(ArrayListSurvey.get(position).getQuestion());
              if(ArrayListSurvey.get(position).current != GraduatingSurveyModel.NONE){
                  RadioButton r = (RadioButton) holder.rg.getChildAt(ArrayListSurvey.get(position).current);
                  r.setChecked(true);
              }else{
                  holder.rg.clearCheck();
              }
          }
          return convertView;
      }
      static class ViewHolder{
          private TextView tv_question;
          private TextView tv_comment;
          private LinearLayout lv_choices;
          private RadioGroup rg;
  
      }
  }
  </code></pre>
  
  <p>This is my model:</p>
  
  <pre><code>public class GraduatingSurveyModel {
  
      public String question;
      public String comment;
      public String choices;
      public String subHeading;
  
  
      public int current = NONE; // hold the answer picked by the user, initial is NONE(see below)
      public static final int NONE = 1000; // No answer selected
      public static final int ANSWER_ONE_SELECTED = 0; // first answer selected
      public static final int ANSWER_TWO_SELECTED = 1; // second answer selected
      public static final int ANSWER_THREE_SELECTED = 2; // third answer selected
      public static final int ANSWER_FOUR_SELECTED = 3; // forth answer selected
      public static final int ANSWER_FIVE_SELECTED = 4; // forth answer selected
  
  
      public GraduatingSurveyModel() {
  
      }
  
      public String getQuestion() {
          return question;
      }
  
      public void setQuestion(String question) {
          this.question = question;
      }
  
      public String getComment() {
          return comment;
      }
  
      public void setComment(String comment) {
          this.comment = comment;
      }
  
      public String getChoices() {
          return choices;
      }
  
      public void setChoices(String choices) {
          this.choices = choices;
      }
  
      public String getSubHeading() {
          return subHeading;
      }
  
      public void setSubHeading(String subHeading) {
          this.subHeading = subHeading;
      }
  }
  </code></pre>
  
  <p>And this is the XML layout of the main fragment:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:padding="16dp"
      android:background="@color/dialog_background"
      android:orientation="vertical"
      tools:context="iulms.iunc.edu.pk.iqraiulms.GraduatingSurvey"
      android:id="@+id/main_layout"&gt;
  
      &lt;ListView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/graduate_list"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_above="@+id/btn_graduate_submit"
          /&gt;
      &lt;Button
          android:id="@+id/btn_graduate_submit"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="submit"
          android:layout_alignParentBottom="true"
          /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>These are the layouts for the different types of rows in listview</p>
  
  <p>Row type 1:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/dialog_background"
      android:orientation="vertical"
      android:paddingBottom="16dp"
      android:paddingTop="16dp"&gt;
  
      &lt;TextView
          android:id="@+id/graduate_question"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="15dp"
          android:text="1. Here comes questions?"
          android:textSize="13dp" /&gt;
  
     &lt;!-- &lt;EditText
          android:id="@+id/graduate_comment"
          style="@style/GraduateTextBox"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="10dp"
          android:background="@drawable/textbox"
          android:ems="10"
          android:hint="Comment"
          android:inputType="textMultiLine"
          android:lines="3" /&gt;--&gt;
  
      &lt;LinearLayout
          android:id="@+id/graduate_choices"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"&gt;
  
          &lt;RadioGroup
              android:id="@+id/graduate_radioGroup"
              android:layout_width="match_parent"
              android:layout_height="match_parent"&gt;
  
              &lt;RadioButton
                  android:id="@+id/graduate_sAgree"
                  style="@style/GraduateRadioButton"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="@drawable/checkbox_selector"
                  android:button="@android:color/transparent"
                  android:checked="false"
                  android:text="Strongly Agree" /&gt;
  
              &lt;RadioButton
                  android:id="@+id/graduate_agree"
                  style="@style/GraduateRadioButton"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="@drawable/checkbox_selector"
                  android:button="@android:color/transparent"
                  android:checked="false"
                  android:text="Agree" /&gt;
  
              &lt;RadioButton
                  android:id="@+id/graduate_uncertain"
                  style="@style/GraduateRadioButton"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="@drawable/checkbox_selector"
                  android:button="@android:color/transparent"
                  android:checked="false"
                  android:text="Uncertain"
                  /&gt;
  
              &lt;RadioButton
                  android:id="@+id/graduate_disagree"
                  style="@style/GraduateRadioButton"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="@drawable/checkbox_selector"
                  android:button="@android:color/transparent"
                  android:checked="false"
                  android:text="Disagree" /&gt;
  
              &lt;RadioButton
                  android:id="@+id/graduate_sDisagree"
                  style="@style/GraduateRadioButton"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="@drawable/checkbox_selector"
                  android:button="@android:color/transparent"
                  android:checked="false"
                  android:text="Strongly Disagree" /&gt;
          &lt;/RadioGroup&gt;
      &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Row type 2</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/dialog_background"
      android:orientation="vertical"
      android:paddingBottom="16dp"
      android:paddingTop="16dp"&gt;
  
      &lt;TextView
          android:id="@+id/graduate_question"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="15dp"
          android:text="1. Here comes questions?"
          android:textSize="13dp" /&gt;
  
      &lt;EditText
          android:id="@+id/graduate_comment"
          style="@style/GraduateTextBox"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="10dp"
          android:background="@drawable/textbox"
          android:ems="10"
          android:hint="Comment"
          android:inputType="textMultiLine"
          android:lines="3" /&gt;
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Row type 3:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/dialog_background"
      android:orientation="vertical"
      android:paddingBottom="16dp"
      android:paddingTop="16dp"&gt;
  
      &lt;TextView
          android:id="@+id/graduate_question"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="15dp"
          android:text="1. Here comes questions?"
          android:textSize="15dp"
          android:textColor="@color/primary_dark"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>How can I retrieve the data submitted in the main fragment?</p>
  
  <p>Thanks</p>