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

Android : The application may be doing too much work on its main thread
Android imageView zooming out when image is at 100% size
Android: supporting multiple screens failed
Is there any setting in android for seeing ANR for an app if app UI becomes unresponsive for say 10sec
No resource found that matches the given name:attr 'colorAccent'
Create transparent toolbar over map fragment
Gridview setChoiceMode(GridView.CHOICE_MODE_SINGLE) does not work
ViewPager with Picasso not loading inside RecyclerView Row
Fatal Exception: java.lang.IllegalArgumentException width and height must be > 0
SVG/VectorDrawable issue in android
How to show all photos folder when user select gallery option
Cobertura throws ClassNotFoundException in Android-project
define manifest place holders in build.gradle
I use ToolBar in my app and I want to custom the width of menu but I do not how to do?
Android: One item on the list view is not showing text properly
using geofencing in android or ios

Categories

HOME
uml
search
prism
eslint
jsf-2.2
socket.io
entity-framework-core
tail
ado.net
zip
echo
teechart
jxl
supervisor
package-managers
capistrano3
dimensional-modeling
easyphp
visual-studio-extensions
tpm
vex
realm-mobile-platform
recurring-events
deltaspike
angularjs-material
sammy.js
imagenet
git-rewrite-history
reactivekit
claims
angular-http
file-sharing
wc
clang++
procfile
linter
netbeans6.8
bluez
page-replacement
pair-programming
custom-font
video-conferencing
headless
swingworker
rxtx
filenet
choice
rocks
burrows-wheeler-transform
jodd
tracker-enabled-dbcontext
inf
android-launcher
git-ftp
bcache
psr-4
cbind
jama
create.js
maven-scm
websphere-esb
css-reset
alice
joomla3.1
cuda-gdb
boost-signals
objectquery
usertype
kqueue
user-tracking
facebook-iframe
jqueryform
binomial-heap
filesystemobject
purepdf
dsl-tools
todos
online-storage
image-capture

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