質問

I have a customized list item with 2 Buttons ( save , share) in a ListView.

How to get which Button is clicked in list item in onItemClick() method ?

Like:

      ListView
----------------------------
[Text]
[SAVE BUTTON1][SHARE BUTTON2]
-----------------------------
[Text]
[SAVE BUTTON1][SHARE BUTTON2]
-----------------------------
[Text]
[SAVE BUTTON1][SHARE BUTTON2]
-----------------------------
-
-
役に立ちましたか?

解決

You can do the following,

set Tag to your Button in the getView() method of your ListView

btn.setTag(position);

then set OnClickListener to your button,

btn.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "button position is: "+v.getTag(),
   Toast.LENGTH_LONG).show();
    }
});

他のヒント

For custom adapters you should use View.OnClickListener and set it to each individual element Button using setOnClickListener() method.

the onItemClick() is part of OnItemClickListener that only responds to click on the whole view. This is usually used for simpler lists using one of the default adapters.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top