0

方法 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());

}
}
4

2 回答 2

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;
}
于 2013-06-24T11:01:24.153 回答
0

设置光标位置

代替

button.setTag(position)

你做

button.setTag(cursor.getPosition());

您可以在构造函数中设置光标。

this.cursor = c;
于 2013-06-24T10:55:58.357 回答