0

In code below, how get Button position of clicked button?

public class HistoryAdapter extends CursorAdapter {
Button button;
TextView text;

public HistoryAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
}

@Override
public View newView(final Context context, Cursor cursor, ViewGroup parent) {
    View view = LayoutInflater.from(context).inflate(R.layout.historylist, parent, false);
    return view;
}

@Override
public void bindView(View view, final Context context, final Cursor cursor) {
    text = (TextView) view.findViewById(R.id.text);
    button = (Button) view.findViewById(R.id.btn);
    String body = cursor.getString(cursor.getColumnIndex("name"));
    text.setText(body);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

}

}

Error
  • 820
  • 1
  • 11
  • 34

2 Answers2

1

If you need the position of the item in the list, you can access it from the View passed to the onClick method, referring to this link you can edit the code doing:

@Override
public void bindView(View view, final Context context, final Cursor cursor) {
    text = (TextView) view.findViewById(R.id.text);
    button = (Button) view.findViewById(R.id.btn);
    String body = cursor.getString(cursor.getColumnIndex("name"));
    text.setText(body);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position=(Integer)v.getTag();
        }
    });
}

hope it works :)

Community
  • 1
  • 1
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
  • 1
    this CursorAdapter used for listview , i want get position of button in list view if it clicked – Error Aug 25 '15 at 15:46
  • 1
    While links may answer the question, it is not advisable to post them. Post the relevant code from the links that you think will solve the problem and add the links as a reference. In future, the links might get modified or replaced, making the answer totally irrelevant. Also, certain links are not accessible to everyone. – Prerak Sola Aug 25 '15 at 15:52
1

The following code should work (the final is important):

    @Override
public void bindView(View view, final Context context, final Cursor cursor) {
    text = (TextView) view.findViewById(R.id.text);
    button = (Button) view.findViewById(R.id.btn);
    String body = cursor.getString(cursor.getColumnIndex("name"));
    final int position = cursor.getPosition();
    text.setText(body);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            doSomething(position);
        }
    });