title: Multiple radio-buttons in listview gets selected even if I select one button after scrolling
body: <p>I have a list of cards-views which I am rendering, in each card there is information about that particular route and <code>radio button</code> which when clicked</p>
  
  <ol>
  <li>should give me the position of the route form the complete list of routes </li>
  <li>when clicked on different radio button previous one should be toggled and the one selected should be selected </li>
  </ol>
  
  <p>Problem I am facing </p>
  
  <p>when I select lets say 3rd radio button it selects 3rd items when the list renders the next views which is how the arrayadapter works. even though In my listview I have used <code>android:choiceMode="singleChoice"</code>  still its not selecting single radio button.</p>
  
  <p>Here is the image also go see what the problem is exactly
  <a href="https://i.stack.imgur.com/YgcFM.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YgcFM.jpg" alt="enter image description here"></a></p>
  
  <p>my RouteAdapter class whee I am trying to check which radio-button is pressed</p>
  
  <pre><code>class RouteAdapter extends ArrayAdapter&lt;RouteItem&gt; {
  
      private Context mContext;
  
      RouteAdapter(Activity context, List&lt;RouteItem&gt; routes) {
          super(context, 0, routes);
          this.mContext = context;
      }
  
      @NonNull
      @Override
      public View getView(final int position, final View convertView, @NonNull ViewGroup parent) {
  
          View listItemView = convertView;
          if(listItemView == null) {
              // we are passing false as we don't want to attach it to view just yet,
              // first set the proper name and details then set it.
              listItemView = LayoutInflater.from(getContext()).inflate(R.layout.route_view, parent, false);
          }
          // getting the route at that position
          RouteItem route = getItem(position);
  
          ......
  
          final RadioButton rb = (RadioButton) listItemView.findViewById(R.id.radio_button);
          //todo: fix radio button to save prefs
          rb.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  RouteItem route = getItem(position);
                  Log.d("RouteAdapter.class", "getView: "+ route.getRoute_id());
                  rb.toggle();
              }
          });
          return listItemView;
      }
  }
  </code></pre>
  
  <p>my activity_view_route.xml where I am rendering all the routeItems</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;ListView xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/add_route"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:drawSelectorOnTop="true"
          android:choiceMode="singleChoice"
          tools:context="com.sjain.routeplanner.ViewRouteActivity" /&gt;
  
          &lt;TextView
              android:id="@+id/empty_view"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerInParent="true"
              android:textAppearance="?android:textAppearanceMedium" /&gt;
  
          &lt;ProgressBar
              android:id="@+id/loading_indicator"
              style="@style/Widget.AppCompat.ProgressBar"
              android:indeterminateTint="@color/teal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerInParent="true" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>here is my route_view.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;android.support.v7.widget.CardView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="8dp"
          android:layout_centerHorizontal="true"&gt;
          &lt;RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:padding="8dp"&gt;
  
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/route_name"
                      android:id="@+id/view_name"
                      android:textSize="20sp"
                      android:layout_gravity="center_horizontal"
                      android:layout_marginTop="8dp"
                      android:layout_marginBottom="8dp"/&gt;
  
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:ellipsize="end"
                      android:maxLines="1"
                      android:text="@string/endpoints"
                      android:id="@+id/view_start"
                      android:textSize="18sp"
                      android:layout_below="@id/view_name"/&gt;
  
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:ellipsize="end"
                      android:text="@string/view_waypoints"
                      android:id="@+id/waypoints"
                      android:layout_marginTop="8dp"
                      android:layout_marginBottom="8dp"
                      android:layout_below="@id/view_start"/&gt;
  
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:ellipsize="end"
                      android:maxLines="1"
                      android:text="@string/endpoints"
                      android:id="@+id/view_end"
                      android:textSize="18sp"
                      android:layout_below="@id/waypoints"/&gt;
  
                  &lt;RadioButton
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:id="@+id/radio_button"
                      android:layout_alignParentEnd="true"/&gt;
  
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:id="@+id/view_map"
                      android:padding="8dp"
                      android:background="@color/com_facebook_blue"
                      android:layout_alignBottom="@id/view_end"
                      android:layout_alignParentEnd="true"
                      android:layout_marginEnd="7dp"
                      android:text="@string/view_map"/&gt;
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:id="@+id/view_id"
                      android:visibility="gone"/&gt;
          &lt;/RelativeLayout&gt;
      &lt;/android.support.v7.widget.CardView&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>My routeItem class </p>
  
  <pre><code>class RouteItem {
  
  
      private String route_name;
      private String start_point;
      private String end_point;
      private String route_id;
      private  ArrayList&lt;String&gt; waypoints;
      private  ArrayList&lt;String&gt; waypoint_name;
  
      RouteItem(String route_name, String route_id, String start_point, String end_point, ArrayList&lt;String&gt; waypoints, ArrayList&lt;String&gt; waypoint_name) {
          this.route_name = route_name;
          this.route_id = route_id;
          this.start_point = start_point;
          this.end_point = end_point;
          this.waypoints = waypoints;
          this.waypoint_name = waypoint_name;
      }
  
      String getRoute_name() {
          return route_name;
      }
  
      String getStart_point() {
          return start_point.split(";")[0];
      }
  
      String getEnd_point() {
          return end_point.split(";")[0];
      }
  
      String getEnd_point_coordinate() {
          return end_point.split(";")[1];
      }
  
      String getStart_point_coordinate() {
          return start_point.split(";")[1];
      }
  
      String getRoute_id() {
          return route_id;
      }
  
      ArrayList&lt;String&gt; getWaypoints() {
          return waypoints;
      }
  
      ArrayList&lt;String&gt; getWaypoint_name() {
          return waypoint_name;
      }
  
  }
  </code></pre>
  
  <p>I know there is something like radio-group also but that seems to be out of option here since the list is generated dynamically depending on the number of routes obtained form the server. My problem is similar to this question <a href="https://stackoverflow.com/questions/18842599/android-radio-buttons-in-a-custom-listview-changes-its-state-when-we-scroll-the">Android Radio buttons in a custom listview changes its state when we scroll the list</a> but this is also not properly answered. </p>
  
  <p>If someone can please let me know how to fix this issue and just get the position of the card which is selected.</p>