سؤال

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