当我点击适配器中的ImageView时,我试图调出一个对话框。我尝试了几件事但没有让它发挥作用。现在,我正在尝试使用showDialog(),就像我在一个Activity中一样但没有运气。它找不到showDialog()方法。
适配器:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
String[] mStringArray = new String[mData.size()];
mStringArray = mData.toArray(mStringArray);
final Integer recordID = Integer.parseInt(mStringArray[position]);
Cursor res = mydb.getData(recordID);
ImageView cancel = (ImageView) convertView.findViewById(R.id.x);
res.close(); // that's important too, otherwise you're gonna leak cursors
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
activityContext.showDialog(DIALOG_ALERT);
mydb.deleteSavedVideo(recordID);
mData.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
适配器:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_ALERT:
AlertDialog.Builder builder = new AlertDialog.Builder(activityContext);
builder.setTitle("Remove Video");
builder.setMessage("hkjh.");
builder.setCancelable(true);
builder.setPositiveButton("Cancel", new CancelOnClickListener());
builder.setNegativeButton("Remove", new deleteRow());
AlertDialog dialog = builder.create();
dialog.show();
}
return super.onCreateDialog(id);
}
更新
适配器构造函数
public SavedVideoAdapter(Context context, ArrayList<Object> data, MySQLiteHelper database) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mData = data;
mydb = database;
activityContext = context;
}
的活动:
public void getSavedVideos() {
mydb = new MySQLiteHelper(getActivity());
listView.setEmptyView(rootview.findViewById(R.id.noSavedVideosTextView));
//Get IDs of all rows in the db
ArrayList savedVideoIDs = mydb.getAllSavedVideo();
mSavedVideoAdapter = new SavedVideoAdapter(getActivity(), savedVideoIDs, mydb);
listView.setAdapter(mSavedVideoAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myIntent = new Intent(getActivity(), UploadVideoActivity.class);
startActivity(myIntent);
}
});
}
答案 0 :(得分:1)
您在onCreateDialog
课程中定义了Adapter
。您需要在Activity
课程中添加此方法才能访问Activity context
。
答案 1 :(得分:0)
AlertDialog dialog = builder.create();
和dialog.show();
只需要builder.show();
即可。
答案 2 :(得分:0)
问题很可能出现以下问题:
activityContext.showDialog(DIALOG_ALERT);
如何通过Context
的{{1}}方法从主Activity
获得getView
?要解决此问题,只需将主Adapter
的{{1}}表单传递到Context
,然后将Activity
用于Adapter
。
修改强>
我认为问题是您在Context
中使用Dialog
,因为onCreateDialog
属于Adapter
类,所以您无法执行此操作。尝试将onCreateDialog
放入主Activity
,然后在onCreateDialog
中显示Activity
。
此外,当您实例化Dialog
时,请尝试使用:
Adapter
而不是......
Adapter