方法onClick()仅在position == 0时有效?为什么呢?
public class mySCAdapter extends SimpleCursorAdapter implements OnClickListener {
final String LOG_TAG = "myLogs";
LayoutInflater inflater;
public mySCAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
inflater = LayoutInflater.from( context );
// TODO Auto-generated constructor stub
}
@Override
public View getView( int position, View convertView, ViewGroup parent) {
View v = null;
if( convertView != null )
v = convertView;
else
v = inflater.inflate( R.layout.add_program_ex_list_item, parent, false);
ImageButton button = (ImageButton) v
.findViewById(R.id.add_program_exercise_list);
button.setTag(position);
button.setOnClickListener(this);
return super.getView(position, convertView, parent);
}
@Override
public void onClick(View v) {
Log.d(LOG_TAG, "It works, pos=" + v.getTag());
}
}
答案 0 :(得分:1)
您没有正确覆盖getView()
。它必须是这样的:
@Override
public View getView (int position, View convertView, ViewGroup parent) {
View v = null;
if (convertView != null)
v = convertView;
else
v = inflater.inflate( R.layout.add_program_ex_list_item, parent, false);
TextView textView = (TextView) v.findViewById(R.id.your_text_view);
textView.setText(getItem(position));
ImageButton button = (ImageButton) v.findViewById(R.id.add_program_exercise_list);
button.setTag(position);
button.setOnClickListener(this);
return v;
}
答案 1 :(得分:0)
设置光标位置,如
而不是
button.setTag(position)
你做了
button.setTag(cursor.getPosition());
您可以在构造函数中设置游标。
this.cursor = c;