Home » Android » java – Android Gridview not holding selection after scroll-Exceptionshub

java – Android Gridview not holding selection after scroll-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

Hi I need help to find why the selection in my android gridview is not working. I am trying to add ability in my app to allow users to highlight selected items. It is working (i.e. the selected item is highlighted) but as soon as I scroll the selection disappears. Also If I select 6 cell it highlights 8th cell.

Below is the code:
XML

   <TableRow
        android:layout_width="wrap_content"
        android:layout_height="@dimen/student_activity_first_row"
        android:layout_marginTop="10dp">

        <RelativeLayout
            android:id="@+id/relLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="30dp">

            <!--grid view for students goes here-->


            <GridView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/grid_view"
                android:layout_marginTop="90dip"
                android:layout_width="fill_parent"
                android:layout_height="330dp"
                android:numColumns="auto_fit"
                android:horizontalSpacing="20dp"
                android:verticalSpacing="20dp"
                android:gravity="center"
                android:stretchMode="columnWidth"
                android:layout_marginBottom="20dp"
                >

            </GridView>


        </RelativeLayout>


    </TableRow>

And below is GridView binding and selection:

private void BindData(String filter) {
    GridView gridView = findViewById(R.id.grid_view);
    CommonUtils utils = new CommonUtils();
    String dropdate = utils.getTodaysDate();

    final Booking h = new Booking();

    bookingsList = new ArrayList<Booking>();

    bookingsList = h.getAllBookingsForClassroom(ASCActivity.this, room_id, dropdate,filter);
    adapter = new BookingGridViewAdapter(ASCActivity.this, bookingsList,true);
    gridView.setAdapter(adapter);
    final SharedPreferences pref =  this.getSharedPreferences(SETTINGS_PREFS_NAME, Context.MODE_PRIVATE);




    // OnClick
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,

                                int position, long id) {

            GridView gridView = ASCActivity.this.findViewById(R.id.grid_view);

            final int size = gridView.getChildCount();
            CommonUtils utils = new CommonUtils();

            for(int i = 0; i < size; i++) {
                ViewGroup gridChild = (ViewGroup) gridView.getChildAt(position);

                if (gridChild != null) {
                    LinearLayout tv = (LinearLayout) gridChild.findViewById(R.id.parentPanel);
                    int color = ((ColorDrawable)tv.getBackground()).getColor();
                    if(color == -4591){
                        tv.setBackgroundColor(Color.parseColor("#ffee12"));
                    }else{
                        tv.setBackgroundColor(Color.parseColor("#ffee11"));
                    }
                    break;
                }
            }

    }

}
How to&Answers:

BindData() will be called when you scrolled top or bottom and views will be reset to initial so maintain a status array or status boolean in Booking model and compare boolean value to check selected or not