Question

I have a list view and each row of it has 3 buttons(Click able Image Views) making different actions if they are clicked.

I have a trouble with it because I don´t know how to recognize if the user click the button for delete or edit the element. I mean, How can I get the View that the user selected?

I tried with the parameters of onItemClick but I´m get stuck in there.

public void onItemClick(AdapterView<?> av, View v, int pos, long id)

Thanks.enter image description here

Was it helpful?

Solution

Try adding tags to your buttons and in onClickListener call view.getTag().

OTHER TIPS

I did like to work with LinearLayout and do something like this :

private void setElements() {
    for (Element elem : listElements) {
        View view = li.inflate(R.layout.item_3_object, null);//view represents a line from your list
        Button bt1 = (Button) view.findViewById(R.id.bt1);
        Button bt2 = (Button) view.findViewById(R.id.bt2);
        Button bt3 = (Button) view.findViewById(R.id.bt3);
        bt1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // your code
            }
        });
        // set listeners for the other buttons too
        listView.addView(view); //is the LinearLayout element to fill with lines
    }
}
private Button button;

button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                        button1Action(); // do your action here
            }
        });

Since you want element from the ListView Row as Clickable you have define it Into your view Like this: i.e Your Adapter you are using to inflate your view

public View getView(int position, View convertView, ViewGroup parent)
    {
        ContentHolder chld=null;
        View row =  convertView;

        final int p=position;
        ContactModel rowItem = getItem(position);
        if(row==null)
        {
            chld=new ContentHolder();

            LayoutInflater inflate=(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

            row=inflate.inflate(textViewResourceId,null);   

            chld.btn=(Button)row.findViewById(R.id.listViewButton_http);

            row.setTag(chld);

        }else{
            chld=(ContentHolder) row.getTag();
        }

        Button b1=(Button)row.findViewById(R.id.listViewButton_http);

        b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

            }
        });

          //similary call your other Button here and it is DONE
    }

Hope this could help

The view-parameter is the one you want.

v.getId() delivers you the id of the clicked button.

For example, use:

if(v.getId() == R.id.buttonDelete) {
    //your code for delete button here
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top