在下面的代码中,如何获得点击按钮的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) {
}
});
}
}
答案 0 :(得分:1)
如果你需要列表中项目的位置,你可以从传递给onClick方法的View中访问它,参考this link你可以编辑代码:
@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();
}
});
}
希望它有效:)
答案 1 :(得分:1)
以下代码应该有用(final
很重要):
@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);
}
});