android


Checkbox getting deselected in arrayadapter in listview


CheckBox is automatically getting deselected while I scroll down
Here is my following code:
package com.serial.teach;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class TeachAdapter extends ArrayAdapter<String> {
Context context;
ArrayList<String> roll =new ArrayList<String>();
ArrayList<String> checkbox=new ArrayList<String>();
private int lastPosition = -1;
private int counter = 0;
ArrayList<Boolean> status = new ArrayList<Boolean>();
public static List<Integer> SelectedBox = new ArrayList<Integer>();
Animation animation;
View row;
public TeachAdapter(Context c,ArrayList<String> r, ArrayList<String> ch) {
super(c, R.layout.single_rows, R.id.name, ch);
// TODO Auto-generated constructor stub
this.context = c;
this.roll = r;
this.checkbox = ch;
for (int i = 0; i < roll.size(); i++) {
status.add(false);
}
}
class MyViewHolder {
TextView tv;
CheckBox cb;
public MyViewHolder(View v) {
// TODO Auto-generated constructor stub
tv = (TextView) v.findViewById(R.id.roll);
cb = (CheckBox) v.findViewById(R.id.name);
}
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
row = convertView;
final MyViewHolder holder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_rows, parent, false);
holder = new MyViewHolder(row);
row.setTag(holder);
} else {
holder = (MyViewHolder) row.getTag();
}
animation = AnimationUtils.loadAnimation(getContext(),
(position > lastPosition) ? R.anim.up_from_bottom
: R.anim.abc_fade_in);
//down_from_top
row.startAnimation(animation);
lastPosition = position;
holder.tv.setText(roll.get(position));
holder.cb.setText(checkbox.get(position));
holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (holder.cb.isChecked()) {
status.set(position, true);
SelectedBox.add(Integer.parseInt((roll.get(position))));
Toast.makeText(getContext(), "You selected " + roll.get(position)+" "+ checkbox.get(position),
Toast.LENGTH_SHORT).show();
holder.cb.setTextColor(Color.rgb(0, 255, 0));
} else {
status.set(position, false);
Toast.makeText(getContext(), "You unchecked " + position,
Toast.LENGTH_SHORT).show();
holder.cb.setTextColor(Color.rgb(255, 0, 0));
}
}
});
holder.cb.setChecked(status.get(position));
holder.cb.setTag(position);
return row;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return super.getCount();
}
#Override
public String getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
}
I have a textView and a checkbox like this:
TV |_|Checkbox
TV |_|Checkbox
TV |_|Checkbox
TV |_|Checkbox
TV |_|Checkbox
But for many items when i select its deselecting automatically.
My layout consists of a textView and a CheckBox as mentioned
The list elements are viewed by your getView() function in your adapter and they are re-generated when they appear in the visible part of the screen. Your checkBox may be checked, but when it goes out of screen, this information is recycled. When it appears in screen again, the getView() method works again and it appears as unchecked. However, this is just appearance, it means, any programmatic algorithms you make in the background related to checkBox are still valid. If you want to keep the appearances of checkBoxes too, you should keep a list of boolean values that hold status.
For example, create a boolean list checkedBefore. Fill this list accordingly at each checkBox operation. The position parameter in your getView() method is the index in the list. Then you can use the following code to display the checkBoxes properly each time;
if( checkedBefore(position) ){
checkBox.setChecked(true);
}
this is because the getview is called again and again.So to prevent it you must preserve the state of checkboxes.THIS link helped me and it will also help you.This is the best solution
i found.I cannot paste the code because it's too big.It's a complete blog on the this issue

Related Links

display notification in specific time of day in android
change back arrow image in Actionbar with appcompat-v7
How display image into simple listview if this image is a bitmap
Android Firebase - cant get userid using getUid() - Error: on null object reference
android.sources in build.gradle
How to make Android SDK skip download of already fetched update files during “tools/android update sdk”?
Very long startup time
How can I send an email in android app? [duplicate]
Android emulator with Marshmallow,Lollipop doesn't have GApps
InAppBrowser does not play Youtube videos on full screen mode in ionic
How can I use the same fragment\instance more than one place
How to dim the area around a view in android?
Android app open gpx files
Appium I am seeing error message after running the following Appium code
Visual Studio Emulator for Android doesn't connect to VM
Use of xyz.class in the initialization of Intent [duplicate]

Categories

HOME
phantom-dsl
twitter-bootstrap-3
forms
webdriver
formal-languages
powerpoint
pascal
jelastic
owl-carousel
ms-access-2016
edge
entity-framework-core
bookmarks
code-coverage
text-mining
mautic
rtf
shared-libraries
heat
scsi
dynamic-sql
null-pointer
template-engine
google-picker
symfony-console
replication
selinux
jquery-ias
inno-download-plugin
interpreter
dynamically-generated
ckfinder
sinch
user-defined-functions
uitapgesturerecognizer
magento-1.8
scrollview
paragraph
laravel-eloquent
jett
lubridate
xajax
alphabetical
openalpr
projects-and-solutions
demo
apache-stanbol
movilizer
logback-groovy
contract
apache-directory
flask-login
stylecop
jboss-4.2.x
libgphoto2
gitlist
wcftestclient
rserve
insight
carrot2
truezip
baidu
swingworker
qtplugin
jodd
mapinfo
dup
refinerycms
tracker-enabled-dbcontext
ess
morton-number
sql-server-data-tools
apache-commons-daemon
uvc
cakephp-2.7
phpldapadmin
uncaught-typeerror
post-commit-hook
wcm
ignite-ui
winginx
draw2d-js
openargs
high-resolution
beaker-testing
process-explorer
css-reset
actiondispatch
mantle
windows-98
stacky
apache-abdera
qi4j
mapping-by-code
agility.js
node-redis
django-paypal
twitterizer
cloudfiles
commerceserver2007
objective-j
binomial-heap
openfaces
mathematical-typesetting
image-capture
idatareader

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App