0

I have an ImageButton in my child xml layout like this:

<ImageButton 
    android:id="@+id/favoriteButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/citrus_orange"
    android:paddingLeft="@dimen/feed_item_padding_left_right"
    android:background="@null"
    andorid:onClick="flipVote"/>

I programmatically make this button non-focusable in my adapter:

ImageButton favButton = (ImageButton) convertView.findViewById(R.id.favoriteButton);

favButton.setFocusable(false);

In the same layout, I have a TextView like:

<TextView
     android:id="@+id/store_id"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:visibility="gone" />

The click will call the flipVote(View view) method:

public void flipVote(View view) {
        // make a network call with the value from store_id
     }

How do I get the value from the TextView associated with the clicked button to include with the network call?

settheline
  • 3,333
  • 8
  • 33
  • 65

2 Answers2

0

In the adapter, you have to intercept the click on the button and in the listener make TextView.getText and then call your method flipVote with the value.

AlexBalo
  • 1,258
  • 1
  • 11
  • 16
  • I don't have a listener, the click calls the `flipVote` method automatically. – settheline Jul 23 '14 at 22:21
  • You don't have a custom adapter? – AlexBalo Jul 23 '14 at 22:26
  • Perhaps the OP wants users to have to click the button specifically, not just anywhere on the list item? – Max Spencer Jul 23 '14 at 22:26
  • Yes exactly @MaxSpencer. That was the reason I included `favButton.setFocusable(false);`, as it allows both the button and listview item and the button to be clicked independently. I do indeed have a custom adapter. – settheline Jul 23 '14 at 22:35
0

I discovered that the proper way to achieve this is by using Tags. I did it like so:

Adapter

// store is data model
favButton.setTag(store.getId());

Activity

// use it in the flipVote(View view) method
public void flipVote(View view) {
        JSONObject storeVoteData = new JSONObject();

try {
    storeVoteData.put("store_id", view.getTag());
    } catch (JSONException e) {
             e.printStackTrace();
         }
     }

  // make Volley call with JSONObject
}
settheline
  • 3,333
  • 8
  • 33
  • 65