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

How to change the duplicate values with empty in arraylisthashmap in android?
android intent vs observer
Whats the best way to do a loop for listview selections
IabResult: Billing service unavailable on device. (response: 3:Billing Unavailable)
Location show on map according to list?
Android app for radio streaming mms:// howto
Error from constructor when using ORMLite
Web app: Hide a div except android browsers
resize image on android
How To Center Text In Android Action Bar?
Can't run andEngine game on my phone
FILE_UNRECOGNIZED_PATH for an URI in firefox android extension
How to take screenshot in android emulator
Add a number and a Text Input value with adobe flex
How to keep maps following my-location (moving the camera) and don't have OnCameraChange triggered
AlertDialog.Builder is undefined

Categories

HOME
service-worker
django
fuelux
dns
pagination
redux-form
formal-languages
pda
twig
jmx
rsa
themes
pascal
aws-cli
regular-language
banner
modal-dialog
teechart
accessibility
fosrestbundle
x-cart
enterprise-architect
powershell-v5.0
google-container-registry
jmp
database-connection
parcelable
scriptella
xcode-ui-testing
has-and-belongs-to-many
failover
calculation
xbox
android-gridview
lubridate
hivemq
xcode7.3
dm-script
connection-pool
projects-and-solutions
sim-card
odroid
spring-mvc-test
claims
xmldocument
file-sharing
procfile
builder
wcftestclient
manova
convex-optimization
posixct
appdomain
headless
sundials
css-transforms
template-toolkit
rmongo
stripe-connect
redistributable
xcode-server
data-protection
highland.js
decimalformat
fiware-monitoring
jdi
forwarding
django-urls
sapscript
rrule
at-job
beatsmusic
viewer
crowd
deform
post-commit-hook
universal-image-loader
with-statement
dot42
buffering
mutual-authentication
getopt
kendo-dataviz
ie8-compatibility-mode
cmmi
octal
xcode3.2
pyunit
adobe-media-server
correlated-subquery
failing-tests
isa-swizzling
automationelement
google-ajax-api
tournament
updatesourcetrigger
nstokenfield
xpsdocument
linfu-dynamicproxy

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