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

How to show SearchView Outside/Below toolbar
When i click a list item i want to store it name in an edittext
Prevent WebView fragment from reloading page on screen rotation
Layout dynamic grid in middle
Stop Android Service when user presses the home button
Android PreferenceFragment Get Result From Intent
Using camera with plyer on kivy: app reseting
IllegalArgumentException: width and height must be > 0 with bitmap.createBitmap method
Django 1.9 is not setting the sessionid cookie on login response only on Android with Cordova 6.0.0
AHBottomNavigation Not Changing Color Android
Android get data from Listview and show in a new activity
how to disable already selected item in bottombar
Caused by: java.lang.IllegalArgumentException: Binary XML file line #28: Duplicate id
how to implement backend server authentication with google play games services for android
What makes QBluetoothDeviceDiscoveryAgent::finished() be called?
Remove an item in RecyclerView- Android

Categories

HOME
google-cloud-platform
pda
battery
3d-reconstruction
jtextpane
ibeacon-android
gkturnbasedmatch
shared-libraries
magnetic-cards
android-intent
chargify
video-editing
asp.net-mvc-viewmodel
smartclient
xmonad
identity-management
replication
react-toolbox
ios10.2
qliksense
game-theory
expressionengine3
opentk
angular-translate
rpgle
quantlib
testcomplete
nsurl
corenlp-server
zend-debugger
autolisp
lubridate
servicestack-text
nohup
demo
c9.io
attributeerror
movilizer
chronicle-map
apache-spark-dataset
getchar
facebook-pixel
dbcontext
rserve
mongotemplate
java-websocket
trigger.io
javafx-css
donations
vcloud-director-rest-api
mapxtreme
reporting-services-2012
firebase-security
cassia
sklearn-pandas
pushbots
design-principles
jbase
custom-errors
phonertc
rpart
lasso
viola-jones
cwrsync
omnifaces
snoop
delayed-execution
android-vibration
structuremap3
deform
post-commit-hook
unix-socket
template-deduction
draw2d-js
mutual-authentication
multiautocompletetextview
windows-embedded-standard
high-resolution
bin
gd-graph
breakout
poker
fb.ui
resource-management
onmousemove
fragmenttransaction
jdb
google-maps-mobile
entitydatasource
jqote
delimited-text
openfaces
zen
pos-for-.net
cfwindow
echo3
lobo-cobra

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