简体   繁体   中英

Android get POJO after button is clicked within listview item

I have an ImageButton inside of a listviews children like so:

 <ImageButton 
      android:id="@+id/pick_up_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingLeft="@dimen/feed_item_padding_left_right"
      android:background="@null"
      android:onClick="pickUpDropOff"/>

That calls the pickUpOrDropOff method like this:

public void pickUpDropOff(View view) {

        ImageButton buttonToChange = (ImageButton) view.findViewById(R.id.pick_up_button);

        if (buttonToChange.getTag(R.bool.PICKED_UP_TAG).toString() == "false") {
             buttonToChange.setImageResource(R.drawable.triangle_full);
             buttonToChange.setTag(R.bool.PICKED_UP_TAG, "true");
             ListItem itemToChange = parent
         }

         else if (buttonToChange.getTag(R.bool.PICKED_UP_TAG).toString() == "true") {
             buttonToChange.setImageResource(R.drawable.triangle_empty);
             buttonToChange.setTag(R.bool.PICKED_UP_TAG, "false");
         }

// do some other stuff
}

This does indeed change the image resource, but when the views get recycled the change in image resource doesn't persist, as my POJO atttribute for pickedUp has not been changed. How do I get the POJO in this case in order to change the attribute?

像这样更改pojo数据源后,只需在listview适配器上调用notifydatasetchange()

adapter.notifyDataSetChanged();

I sorted it out.

To get the pojo for the button clicked, set a tag in the adapter with the objects position like so:

pickUpButton.setTag(R.integer.LIST_ITEM_POSITION, position);

Then when the button is clicked, I change the underlying pojo like this (my Model is ShoppingListItem ):

ImageButton buttonToChange = (ImageButton) view.findViewById(R.id.pick_up_button);

if (buttonToChange.getTag(R.bool.PICKED_UP_TAG).toString() == "false") {
     buttonToChange.setImageResource(R.drawable.triangle_full);
     int myPosition = (Integer) buttonToChange.getTag(R.integer.LIST_ITEM_POSITION);
     ShoppingListItem itemToChange = shoppingListItems.get(myPosition);
     itemToChange.setPickedUp(true);
 }

 else if (buttonToChange.getTag(R.bool.PICKED_UP_TAG).toString() == "true") {
     buttonToChange.setImageResource(R.drawable.triangle_empty);
     int myPosition = (Integer) buttonToChange.getTag(R.integer.LIST_ITEM_POSITION);
     ShoppingListItem itemToChange = shoppingListItems.get(myPosition);
     itemToChange.setPickedUp(false);
 }

listAdapter.notifyDataSetChanged();

This will stop the issue of non persisting data with recycling listviews. For me, there is no local storage of this data (all comes and goes via JSON api). Be sure to persist changes to the database if your app calls for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM