android


Getting wrong item image button clicked in listview


I have a listview that contains items. And each item has an image button. When an image button is clicked, i want to change it's image using the following code. The code does change the button clicked, but it also changes other items' images that are not clicked. Why this happens, and how to fix?
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
final ImageButton playButton = ((ImageButton) view.findViewById(R.id.play_pause));
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// In the log I get correct position of clicked item, but the image changes are wrong.
Log.d(TAG, "Clicked row-" + view.getTag().toString());
((ImageButton) view.findViewById(R.id.play_pause)).setImageResource(R.drawable.button_pause);
}
});
playButton.setTag(position);
return view;
}
Add this at the begining of your adapter
HashMap<Integer,Boolean> states = new HashMap<Integer, Boolean>();
And in your onClick add this:
if(states.containsKey(position))
{
states.put(position, !states.get(position));
((ImageButton) view.findViewById(R.id.play_pause)).setImageResource(states.get(position)?R.drawable.button_pause:R.drawable.button_play);
}
else
{
states.put(position, true);
((ImageButton) view.findViewById(R.id.play_pause)).setImageResource(R.drawable.button_pause);
}
And before your return view add this:
if(states.containsKey(position) && states.get(position))
((ImageButton) view.findViewById(R.id.play_pause)).setImageResource(R.drawable.button_pause);
else
((ImageButton) view.findViewById(R.id.play_pause)).setImageResource(R.drawable.button_play);

Related Links

is there any difference between phonegap and cordova commands?
Is there a way to call a method from MainActivity inside BroadCastReceiver onReceive?
Android in app purchase . in which case payment is successful?
Stream video file and camera from Android and display it in wpf app
android: nothing shows up in autocomplete result
how to react to the “keep-alive” package in android?
Android GridView add header and footer like listview
I want Start Main Activity once users are signed in
How not add a same fragment if already has been called before?
Default Ringtone Preference to silent
how to load both images and videos as GridView in android
Android cordova - Error loading page while navigation using $.mobile.changePage(“”,“”);
Create android keystory/ private key command line
Get geocode from autocomplete places
sigemptyset() NDK UnsatisfiedLinkError dlopen()
how to change the color of an image to black and white in android

Categories

HOME
microsoftgraph
c++11
sh
sitecore
apiblueprint
access
yandex
web-hosting
visual-studio-emulator
startup
radgridview
cumulocity
easyphp
git-extensions
google-container-registry
nodemcu
microsoft-ocr
systemtime
dynamically-generated
beta
siri
pic32
8051
vex
calculation
event-flow
mustache
cognos-bi
quickfixj
laravel-eloquent
login-script
alphabetical
watchman
ideascript
demo
httr
facebook-pixel
modelsim
subnet
offline.js
android-instrumentation
cifilter
avspeechsynthesizer
appscale
angular-ui-typeahead
hadoop-partitioning
toast
sapi
boomerang
terracotta
ons-api
jsvc
linked-tables
mirror
ansi
omnifaces
xmltype
pseudo-class
administration
metatrader5
wcm
tinyxml
mutual-authentication
vsx
amslidemenu
hibernate-entitymanager
mbunit
css-reset
octal
asplinkbutton
device-width
intersect
dopostback
adobe-media-server
cuda-gdb
thttpd
database-create
jsr286
jquery-address
node-redis
pylucene
luajava
delimited-text
vs-android
cfwindow
lalr
java1.4
commercial-application
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