1
我有幾個活動可以使用同一個陣列適配器。我想提供一個默認的onLongClick如果活動沒有實現它像:如果父母不提供接口,Android適配器會提供默認的onLongClick
public class BookAdapter extends ArrayAdapter<Book> {
...
private View.OnLongClickListener onLongClickListener;
public BookAdapter(Context context, ArrayList<Book> aBooks) {
super(context, 0, aBooks);
Log.i(TAG, "caller:" + context.getClass().getSimpleName());
if (context instanceof View.OnLongClickListener) {
Log.i(TAG, "yes");
onLongClickListener = (View.OnLongClickListener)
context.onLongClickListener();
}
else {
onLongClickListener = new View.OnLongClickListener() {
public boolean onLongClick(View v)
String title =
((TextView)(v.findViewById(R.id.tvTitle)))
.getText().toString();
Log.v(TAG, "view onLongClick pressed for title \""
+ title + "\"");
return true;
}
};
}
}
@Override
public View getView(
int position, View convertView, ViewGroup parent) {
...
convertView.setOnLongClickListener(onLongClickListener);
...
}
...
}
但我似乎無法給調用者onClickListener分配給本地onLongClickListener。
我該怎麼做?