Capture Click on user list in Android

I used my own XML file created to bind my db cursor to ListActivity. Each element in the XML file has two buttons. I want to capture the button click event 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 get attached

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 do not use a custom adapter. Now I want to capture the click of smListNotCompleted and smListCompleted along with the line position.

thank

+3
source share
3 answers

You will need to use a new adapter. Try to understand the concept before 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 work

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 
        }
    }
+7
source

I used the click event in the list and put the position identifier in the button tag

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

, 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));
    }
+4

Because you need click events for buttons in a user list with 2 buttons; you will need to create a custom list adapter in which you can add click events for both buttons separately and you will also get the pressed position.

+2
source

Source: https://habr.com/ru/post/1793518/


All Articles