android


How to fix Android ListView Rendering Error where the checkboxes of the list items disappear?


I am developing an android app where I use a listview which has a checkbox in each row item. But when it scrolls up or down, random rows remove the checkboxes which ends in no checkboxes showing in the entire list. I have tried many solutions given in online tutorials, but none of them work. All of them show it using a ViewHolder class. Could you please help me?
Here is all the code I can provide:
List Item XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<ImageView
android:id="#+id/user_image_view"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_margin="16dp"
tools:src="#mipmap/ic_account_circle_bue" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="16dp"
android:layout_centerVertical="true">
<TextView
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="19dp"
android:textStyle="bold"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#000000"
android:paddingRight="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/user_roll_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Roll Number"
android:textSize="15dp" />
<TextView
android:id="#+id/user_department"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:text="Department"
android:textSize="15dp"
android:maxLines="1"
android:ellipsize="end"/>
</LinearLayout>
</LinearLayout>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
UsersSelection (Model Class):
package com.app.shubhamjhunjhunwala.heritagecompanion_students;
/**
* Created by shubham on 09/05/17.
*/
public class UsersSelection {
public Users users;
public boolean isSelected;
public UsersSelection (Users users, boolean isSelected) {
this.users = users;
this.isSelected = isSelected;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
Users.java (Custom class):
package com.app.shubhamjhunjhunwala.heritagecompanion_students;
/**
* Created by shubham on 23/01/17.
*/
public class Users {
String name;
String department;
String roll;
String dpDownloadUri;
Users() {}
public Users(String name, String department, String roll, String dpDownloadUri) {
this.name = name;
this.department = department;
this.roll = roll;
this.dpDownloadUri = dpDownloadUri;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getRoll() {
return roll;
}
public void setRoll(String roll) {
this.roll = roll;
}
public String getDpDownloadUri() {
return dpDownloadUri;
}
public void setDpDownloadUri(String dpDownloadUri) {
this.dpDownloadUri = dpDownloadUri;
}
}
Custom Array Adapter for List View:
package com.app.shubhamjhunjhunwala.heritagecompanion_students;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.concurrent.ExecutionException;
/**
* Created by shubham on 09/05/17.
*/
public class UsersSelectionAdapter extends ArrayAdapter<UsersSelection> {
public Context context;
public String roll;
public boolean isForGroup;
public List<GroupMembers> groupMembersList;
public List<UsersSelection> list;
public CheckBox checkBox;
public UsersSelectionAdapter(Context context, int resource, List<UsersSelection> objects) {
super(context, resource, objects);
this.context = context;
list = objects;
}
public class ViewHolder {
TextView nameTextView;
TextView rollNumberTextView;
TextView departmentTextView;
ImageView dpImageView;
CheckBox checkBox;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences sharedPreferences = ((Activity) getContext()).getSharedPreferences("HCS", 0);
roll = sharedPreferences.getString(AuthenticationActivity.ROLL_SHARED_PREFERENCE_KEY, "");
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.user_list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.nameTextView = (TextView) convertView.findViewById(R.id.user_name);
viewHolder.departmentTextView = (TextView) convertView.findViewById(R.id.user_department);
viewHolder.rollNumberTextView = (TextView) convertView.findViewById(R.id.user_roll_number);
viewHolder.dpImageView = (ImageView) convertView.findViewById(R.id.user_image_view);
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (int) buttonView.getTag();
list.get(getPosition).setSelected(buttonView.isSelected());
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.user_name, viewHolder.nameTextView);
convertView.setTag(R.id.user_department, viewHolder.departmentTextView);
convertView.setTag(R.id.user_roll_number, viewHolder.rollNumberTextView);
convertView.setTag(R.id.user_image_view, viewHolder.dpImageView);
convertView.setTag(R.id.checkbox, viewHolder.checkBox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkBox.setTag(position);
/*TextView nameTextView = (TextView) convertView.findViewById(R.id.user_name);
TextView departmentTextView = (TextView) convertView.findViewById(R.id.user_department);
TextView rollNumberTextView = (TextView) convertView.findViewById(R.id.user_roll_number);
ImageView dpImageView = (ImageView) convertView.findViewById(R.id.user_image_view);
checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);*/
UsersSelection usersSelection = getItem(position);
Users user = usersSelection.getUsers();
if (!isForGroup) {
viewHolder.checkBox.setVisibility(View.GONE);
} else {
if (user.getRoll().equals(roll)) {
viewHolder.checkBox.setVisibility(View.GONE);
}
}
viewHolder.nameTextView.setText(user.getName());
viewHolder.nameTextView.setTag(usersSelection);
viewHolder.departmentTextView.setText(user.getDepartment());
viewHolder.departmentTextView.setTag(usersSelection);
viewHolder.rollNumberTextView.setText(user.getRoll());
viewHolder.rollNumberTextView.setTag(usersSelection);
viewHolder.checkBox.setChecked(list.get(position).isSelected());
viewHolder.checkBox.setTag(usersSelection);
Bitmap bitmap = null;
if (!user.getDpDownloadUri().equals("")) {
DPAsyncTask dpAsyncTask = new DPAsyncTask(context, viewHolder.dpImageView.getContext(), viewHolder.dpImageView, user);
dpAsyncTask.execute();
} else {
Glide.clear(viewHolder.dpImageView);
viewHolder.dpImageView.setImageDrawable(getContext().getResources().getDrawable(R.mipmap.ic_account_circle_bue));
}
return convertView;
}
public boolean isForGroup() {
return isForGroup;
}
public void setForGroup(boolean forGroup) {
isForGroup = forGroup;
}
public List<GroupMembers> getGroupMembersList() {
return groupMembersList;
}
public void setGroupMembersList(List<GroupMembers> groupMembersList) {
this.groupMembersList = groupMembersList;
}
public CheckBox getCheckBox() {
return checkBox;
}
public void setCheckBox(CheckBox checkBox) {
this.checkBox = checkBox;
}
}
Thank You for going through my issue. Hope you help :)
if (!isForGroup) {
viewHolder.checkBox.setVisibility(View.GONE);
} else {
if (user.getRoll().equals(roll)) {
viewHolder.checkBox.setVisibility(View.GONE);
}
}
You only ever make the CheckBox GONE. You never reset it to be VISIBLE.
Remember that the point behind ListView, other AdapterView classes, and RecyclerView is to recycle views. You cannot, and should not, assume that your widgets are in any particular state in getView(). If you modify a widget (e.g., change its visibility), you need to modify that state all the time.
So, change the above code to:
if (!isForGroup || user.getRoll().equals(roll)) {
viewHolder.checkBox.setVisibility(View.GONE);
} else {
viewHolder.checkBox.setVisibility(View.VISIBLE);
}
or something like that, so that you always state whether the CheckBox should be visible or not.

Related Links

PhoneGap + android: 2 'www' folder in source code: which to modify?
Multiple Package Names - Android App
Change cryptos used in WebRTC browser calls
Input Text into Invisible EditText
DialogFragment behaves differently when invoked via ActionBar
string types not allowed (at mapType with value roadmap)
Nullpointer exception at startDisplayPreview()
Android AnimationSet using XML Resource
Android: Set items inside View from Adapter get NullPointerException
Strange one: Text in website on Android is scrambled, looks hacked, but isn't
How to make android app to fit for all screen sizes
Update information of an contact in android
Traingle shape view in android layout
Details of some facebook friends not coming in facebook sdk
How to Handle Race Condition Due to AsyncTask android
LockScreen widget doesn't work properly

Categories

HOME
google-cloud-platform
localization
turing-machines
prism
dns
ecmascript-6
numbers
hessian
appcelerator
google-cloud-functions
newrelic
spring-data-mongodb
entity-framework-core
missing-data
nfs
mql5
android-securityexception
siddhi
startup
google-form
frequency-distribution
package-managers
capistrano3
delphi-xe5
sencha-touch
selinux
poedit
ember-cli
imageresizer
epsilon
outliers
exchangewebservices
gp
vex
alm
userdefaults
gitlab-api
anki
mailkit
classcastexception
openerp-7
parse-tree
alphabetical
playframework-2.1
modernizr
apns-php
plaintext
file-sharing
snapchat
opennms
term
jquery-ui-draggable
hammer.js
fclose
page-replacement
context-switch
static-cast
compiler-design
quicktime
rxtx
android-instrumentation
cvxpy
vcloud-director-rest-api
xcode-server
lrs
dotnetnuke-7
resourcemanager
expectations
android-dateutils
fink
thread-sleep
grass
exim4
qbxml
design-principles
relativelayout
oracle-spatial
xvim
slash
evolus-pencil
eoferror
viola-jones
dulwich
liquid-layout
pyobjc
nutiteq
ms-access-2000
entity-framework-4.3
j-security-check
ejb-2.x
sql-scripts
cuba
cancellation
artemis
solace-mq
beaker-testing
process-explorer
jquery-1.9
video-codecs
extreme-programming
faye
lambdaj
jdb
ikimagebrowserview
saxparseexception
codebase
objective-j
cruisecontrol.rb
dot-emacs
folding
tui
alsb
blind

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile