Domanda

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]
-----------------------------
-
-
È stato utile?

Soluzione

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();
    }
});

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top