每当有人从自定义列表视图中的某一行单击图像时,我想显示一个对话框。我该怎么做呢?这是我迄今为止在我的自定义适配器中实现的。请参阅代码中的“如何显示对话框片段??? ”注释,了解我难过的地方。
public class DirectoryAdapter extends BaseAdapter {
private Context mContext;
private final Session mSession;
private final ArrayList<MyObject> mMyObjects;
public DirectoryAdapter(Context context, Session session, ArrayList<MyObject> myObjects) {
mContext = context;
mSession = session;
mMyObjects= myObjects;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = null;
try {
if (convertView == null) {
view = new View(mContext);
// get row item from directory_item.xml
view = inflater.inflate(R.layout.server_row, null);
view.setLongClickable(true);
} else {
view = (View) convertView;
}
//Info button
ImageView info = (ImageView) view.findViewById(R.id.iv_directory_item_options);
info.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
ServerMenuDialog dialog = new ServerMenuDialog();
dialog.setRetainInstance(true);
// How to show dialog fragment ???
dialog.show(view.getContext(), "Server Menu"); //EXAMPLE -- WONT COMPILE there is no such view.getContext() method
}
});
} catch (Exception ex) {
//TODO error handling
} finally {
return view;
}
}