单击关联的ImageButton时,在ListView中获取项目

时间:2014-07-23 22:14:11

标签: android listview android-imagebutton

我的子xml布局中有ImageButton,如下所示:

<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"/>

我以编程方式使此按钮在我的适配器中不可聚焦:

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

favButton.setFocusable(false);

在相同的布局中,我有TextView喜欢:

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

点击将调用flipVote(View view)方法:

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

如何从与点击按钮关联的TextView中获取值以包含在网络电话中?

2 个答案:

答案 0 :(得分:0)

在适配器中,您必须截取按钮上的单击并在侦听器中创建TextView.getText,然后使用值调用方法flipVote。

答案 1 :(得分:0)

我发现实现这一目标的正确方法是使用标签。我是这样做的:

适配器

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

活动

// 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
}
相关问题