android


CheckedTextView with ListView


I have a listview which items are CheckedTextViews with checkboxes. However, whenever I check one and scroll the listview and they hide, when I return back to them by rescrolling up they are not checked anymore! I read on internet that listviews, when are scrolled, somehow the hidden items are recycled (returned to their default state if I'm correct) after you scroll back at them. I also read that in the custom adapter, I have to use a method called "setOnCheckedChangeListener", but checkedTextViews do not have that particular method!! So I think my problem might be with listviews. How can I solve this? Thank you.
UPDATE: So this is my custom adapter and model class.
public CustomAdapter(Context c, Model[] resource) {
super(c, R.layout.list_item, resource);
this.context = c;
this.modelItems = resource;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.list_item, parent, false);
cb = (CheckedTextView) convertView.findViewById(R.id.checkBox1);
pN = (TextView) convertView.findViewById(R.id.numberId);
pN.setText(modelItems[position].getPhoneNumber());
cb.setText(modelItems[position].getName());
return convertView;
}
This is the model class:
public Model(String name, String phoneNumber, int value) {
this.name = name;
this.value = value;
this.phoneNumber = phoneNumber;
}
public String getName() {
return this.name;
}
public int getValue() {
return this.value;
}
public String getPhoneNumber() {
return this.phoneNumber;
}
You can save the checkBox's state in your adapter using the onCheckedChanged listener. Then when you are creating your viewHolders, set the checked state of the checkbox using the data from your adapter.
Hope it helps!
You can follow the process on this blog. Its base on the Same issue.Blog link
You need to use an object that as the value of the checkBox like
public class FormCheck {
private String name;
private boolean isChecked;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isChecked() {
return isChecked;
}
public void setIsChecked(boolean isChecked) {
this.isChecked = isChecked;
}
}
and in the getView() you do something like this:
checkedTextView.setChecked(itemArray.get(position).getIsChecked);
checkedTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (checkedTextView.isChecked())
itemArray.get(position).setIsChecked = true;
else
itemArray.get(position).setIsChecked = false;
}
});

Related Links

Read QR Code without using other app
Gps,GeoCoding causes app hang in android
Android App Closes on creation?
GridLayout spitting out “inconsistent constraint” debug-level logs
Android DB: Merging Two Databases Using SQL Dumps
Questio about AdMob ad url to application
trouble importing android-remote-stacktrace
ADT rev20 - apps don't install on any AVD
View will get twice at onItemClick
downloading xml from ftp in android
Notification upon ended parsing of web response
How to transfer “data” between two device (android,iphone)?
write spinner selected value to database
Using AsyncTask to download an XML file
Screen resolution / Physical size
android - showDialog(); error

Categories

HOME
go
adal
eslint
phpmyadmin
jax-rs
zend-framework
static
yahoo-finance
jwplayer
package
socket.io
error-handling
replace
newrelic
cublas
big-o
onclick
x264
iis-6
character
mql5
drop-down-menu
ibeacon-android
mel
heap
cmdb
endpoint
package-managers
pyramid
cfml
topology
tosca
google-api-client
oculus
datastax-enterprise-graph
errbot
sqsh
dst
hard-drive
dynamic-jasper
engine.io
latitude-longitude
reason
imagenet
dm-script
connection-pool
mongoose-schema
stringtokenizer
postgresql-8.4
plan-9
nanoc
lampp
dtsearch
google-cse
corruption
page-replacement
modelsim
rmongo
cudd
flex-monkey
adafruit
expectations
android-dateutils
xdoclet
cda
httpruntime.cache
mobiledoc-kit
vhosts
character-replacement
concurrentmodification
django-urls
phonertc
nodelist
terminate
cfcache
sbcl
jammer
away3d
hidapi
abstract-data-type
idn
panda3d
oracle-enterprise-linux
xcelsius
automationelement
mbeans
unattended-processing
photolibrary
visualj#
xslcompiledtransform

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