In grid view selecting views present on screen but on scroll down I am unable to select it
I am using gridview
and on item select I make the selected item background to green and on deselected transparent but if I scroll down to select more items it randomly select and deselect the items, please help to fix it.
studentGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
desiredBackgroundColor = Color.rgb(0, 255, 0);
try {
Student student = (Student) parent.getItemAtPosition(position);
String stuName = student.getStudent_name();
String stuId = student.getStudent_nid();
view = studentGridView.getChildAt(position);
ColorDrawable viewColor = (ColorDrawable) view.getBackground();
clickCount++;
if (viewColor == null) {
view.setBackgroundColor(desiredBackgroundColor);
mainclickCount = clickCount;
//clickCount++;
//mainclickCount = clickCount;
// Toast.makeText(getApplicationContext(), "firstclick: " + clickCount, Toast.LENGTH_SHORT).show();
Log.d("clickCounttt", "" + mainclickCount);
studentselList.add(stuName);
studentselnidList.add(stuId);
Log.d("viewColor", "" + viewColor);
Log.d("studentselListdee", studentselList.toString());
if (mainclickCount > 0) {
doneIv.setVisibility(View.VISIBLE);
} else {
doneIv.setVisibility(View.GONE);
}
String studentselListStr = studentselList.toString().replace("[", "").replace("]", "");
showstudentTv.setText(studentselListStr);
if (isStudentClick == true) {
showstudentTv.setText(studentselListStr);
} else if (isStaffClick == true) {
showstaffTv.setText(studentselListStr);
}
return;
}
int currentColorId = viewColor.getColor();
Log.d("currentColorId", "" + currentColorId);
if (currentColorId == desiredBackgroundColor) {
view.setBackgroundColor(Color.TRANSPARENT);
clickCount--;
mainclickCount = clickCount;
studentselList.remove(stuName);
studentselnidList.remove(stuId);
String studentselListStr = studentselList.toString().replace("[", "").replace("]", "");
showstudentTv.setText(studentselListStr);
if (isStudentClick == true) {
showstudentTv.setText(studentselListStr);
} else if (isStaffClick == true) {
showstaffTv.setText(studentselListStr);
}
} else {
view.setBackgroundColor(desiredBackgroundColor);
clickCount++;
mainclickCount = clickCount;
studentselList.add(stuName);
String studentselListStr = studentselList.toString().toString().replace("[", "").replace("]", "");
showstudentTv.setText(studentselListStr);
}
Log.d("clickCountmainn", "" + mainclickCount);
Log.d("studentselListtt", studentselList.toString());
if (mainclickCount > 0) {
doneIv.setVisibility(View.VISIBLE);
} else {
doneIv.setVisibility(View.GONE);
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});
xml file
<GridView
android:id="@+id/studentGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:descendantFocusability="afterDescendants"
android:scrollingCache="false"
android:horizontalSpacing="1dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp"
android:layout_marginBottom="@dimen/margin_1"
android:layout_below="@+id/classselLayout"/>
<RelativeLayout
android:id="@+id/doneLayout"
android:layout_width="@dimen/margin_150"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:id="@+id/done"
android:layout_width="@dimen/margin_40"
android:layout_height="@dimen/margin_40"
android:src="@drawable/done1"
android:visibility="visible"
android:layout_alignParentLeft="true"/>
<ImageView
android:id="@+id/cancel"
android:layout_width="@dimen/margin_40"
android:layout_height="@dimen/margin_40"
android:src="@drawable/cross"
android:layout_alignParentRight="true"/>
</RelativeLayout>