我想要顯示一個對話框,只要有人點擊自定義列表視圖中的某一行的圖像。我應該如何去做這件事?以下是我在自定義適配器中實現的內容。請參閱「如何在代碼中顯示對話框片段???」的評論。如何在自定義適配器中顯示對話框片段?
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;
}
}
爲什麼不使用this.context而不是view.getContext()? – Codeman
@Pheonixblade小改正:this.mContext – Marek
@Pheonixblade - this.mContext不是此調用的適當範圍。 'This'似乎引用視圖而不是DirectoryAdapter類。我似乎無法從'mContext'獲得對FragmentManager的引用,所以我有點難住。 – user1532208