Solution To Android Listview Checkbox Scroll Problem

Listview in android is an important widgets in android to display data on it. The android listview is a view which is used to group several item and display them in vertical scrollable list.

In listview each item to be displayed is show by Adapter whose work is to pull the content from array, Linked List, HashMap or Database.

In this tutorial I am going to show you how to use check box on listview. The main problem in listView with checkbox is that the state of checkbox gets lost on scroll. So in this tutorial I am going to show you how to maintain the state of checkbox.

The solution for this problem is very simple i.e is you have to store the states for our checkbox. so without further a do let’s get started.

Creating User Interface

solution to android listview problem

activity_main.xml

Now add the row layout. In every listview the row view is very important thing without this view you are not able to show anything on listview.

row_layout.xml

Building Android Application

  • Define Model class Country.java. This will be used to stored data and also to set data to respective view.

  • Now define the Adapter of listView. Here each row of list is set with UI data.

  • Define the view Object in MainActivity.java. Connect user interface with the main java class so the user can interact with it.

  • Now set the Adapter with data. so that we can set data on each row.

  • Define the MainActivity.java i.e combining whole code in java class.

  • Now run the application.

solution to android listview problem solution to android listview problem solution to android listview problem

That’s all friends. If you are facing any problem then comment below will be ready to help you.

You may also like...

18 Responses

  1. Hitesh Tiawri says:

    hey
    i am getting trouble when check box is selected at that time i am setting text but text is not setted. its gets text is randomly check box row
    how to handle set text on paricular check is selected ob that postion

  2. bryan says:

    What is pair in getSelectedCountry? Where would you define this variable?

  3. Hitesh Tiawri says:

    THANKS BUDDY FOR YOUR HELP!!!!!!!

  4. juanalberto says:

    Thank you very much for this tutorial. It help me a lot.

    Greets!

  5. techeda says:

    Thank you so much… working fine

  6. goes says:

    how about using 4 radio button?

    • Tatson Baptista says:

      I don’t think that scroll issue will persist in 4 radio button.

      • goes says:

        i have 4 radio button, when i select the second radio button in first row and i scroll it is still in the selected. but when i select radio button in another row, the radio button at first row will follow it. Can you tell me how to fix it please? or can you check my code and i will send it to you?

        thanks before

  7. Guru says:

    Hi bro… I’m having one problem…Checkbox is unchecked when it is scroll down.. I’m not able to resolve that.. please help me..

  8. This is a great tutorial, very helpful, I have some question for you sir.
    What if I want to make another checkbox outside of the list.
    So basically, its on the activity, not on the adapter.
    And that checkbox function is to select all the checkbox inside of the listview.

    Do you have any idea how to do that sir?
    Thanks in advance 🙂

    • Tatson Baptista says:

      Write a public function in your adapter which checks all the checkbox from the listview. Then call this function in your activity on adapter reference.

  9. I’m having a trouble on getting the data. Basically, I’m using some dynamic data for my list, so I need to get the data from my main activity, individually. The thing is, when I trying to get the items, all of the data is together as a string. I try to use for loop and filter it one by one but. There is this some kind of element that instead of looping each item, its combining every string the element gets. Idk if its iterator, of smth.

    The thing I want to accomplish is, to get each the item I checked individually, not as a whole string. So when I fetch it on my main activity, I can still use and save each of it

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.