-->

Capturing Click on a custom listview in Android

2020-03-26 09:53发布

问题:

I used a custom XML file created bind my db cursor in a ListActivity. Each item in the XML file has 2 buttons. I want to capture the click event of the button and the position in the list.

This is my XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/smListName" android:paddingTop="2dip" android:paddingBottom="3dip" android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:textSize="22dip" />
    <Button android:id="@+id/smListCompleted" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="right" android:textStyle="bold" android:textColor="#0000ff"  />
    <Button android:id="@+id/smListNotCompleted" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="right" android:textColor="#ff0000" 
        android:textStyle="bold" />
</LinearLayout>

And this is how I'm binding

db = openOrCreateDatabase("ITC", MODE_PRIVATE, null);
Cursor outlets = db.rawQuery("Select s.salesmanid as _id, s.name ...", null);
this.setListAdapter(new SimpleCursorAdapter(this, R.layout.salesmanlist, outlets, new String[] { "name", "complete", "incomplete" }, new int[] {
R.id.smListName, R.id.smListCompleted, R.id.smListNotCompleted }));
db.close();

I'm not using a custom adapter. Now I want to capture the click of smListNotCompleted and smListCompleted along with the row position.

Thanks

回答1:

You will have to use a new adapter. Try to understand the concept behind this before implementing this :

class YourNewAdapter extends SimpleCursorAdapter
{

 public View getView(int position, View convertView, ViewGroup parent)
 {

        View v = convertView;
        LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(id, null);

         btn = (Button)v.findViewById(R.id.yourbutton);  
         btn.setOnClickListener(YourActivity.this);
         btn.setId(position);

         btn.setText("sometext");

         v.setLongClickable(true);

          }
            return v;
     }
 }

and in your activity

public void onClick(View v)
{
        if(v.getId() == R.id.yourbutton id)
        {
               // do what you want you can also put this on click listener in the getview fn 
        }
    }


回答2:

I used a click event in the listadapter and put the position id into the buttons tag

Button editButton = view.FindViewById(Resource.Id.editPaymentButton_2) as Button;
editButton.Tag = position;
editButton.Clickable = true;
editButton.Click += editButton_Click;

this is the event I tied to the event in GetView

void editButton_Click(object sender, EventArgs e)
    {
        if (editButtonClicked)
        {
            return;
        }
        editButtonClicked = true;
        var button = sender as Button;
        if (button == null)
            return;
        //OnEdit((Entities.PaymentTemplate)GetItem((int)button.Tag));
    }


回答3:

Since you require click events for both the buttons in the custom list with 2 buttons; you will need to create a custom list adapter in which you can add click events for both the buttons seperatly and you will get the position clicked also.