我有两个类,一个是适配器,一个是活动。我在适配器中显示了一个对话框。当我尝试更改屏幕方向时,出现错误。我尝试在我的活动中覆盖下面的代码,但似乎没有任何作用。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d(TAG, "configuration changed : " +newConfig);
}
以下是我的适配器代码。
public AddressPopUpAdapter(Activity activity, Activity parent,
int resourceId, ArrayList<PopUpMenu> items, int renderer) {
super(activity, resourceId, items);
this.items = items;
this.renderer = renderer;
this.activity = activity;
this.parentActivity = parent;
popupMenuUtils = new PopupMenuUtils();
dialog = new Dialog(parentActivity);
}
public View getView(int position, View convertView, ViewGroup parent) {
popUpMenu = items.get(position);
Log.d(TAG, "location" + popUpMenu);
if (popUpMenu == null) {
return null;
}
Log.d(TAG, "location" + popUpMenu);
View view = convertView;
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) (getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
view = layoutInflater.inflate(renderer, null);
}
final LinearLayout popupmain = (LinearLayout) view
.findViewById(R.id.popupmain);
popupmain.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setEnabled(false);
performAction(v, activity);
}
});
if (position % 2 == 0) {
view.setBackgroundResource(R.drawable.listshape);
} else {
view.setBackgroundResource(R.drawable.favoritebody);
}
return view;
}
public void performAction(View v, Activity activity) {
Context myContext = v.getContext();
PopUpMenu popUpMenu = (PopUpMenu) v.getTag();
String result = popUpMenu.getMenuName();
if (result != null
&& result.equalsIgnoreCase(myContext.getResources().getString(
R.string.savecurrentlocation))) {
getCurrentLocation();
}
else if (result != null
&& result.equalsIgnoreCase(myContext.getResources().getString(
R.string.distancebetween))) {
AttractionService service = AttractionService
.getInstance(parentActivity.getApplicationContext());
ArrayList<AttractionData> allMenuSearchList = service
.getAllAttractions(true, Constants.field, Constants.order);
if (allMenuSearchList != null && !allMenuSearchList.isEmpty()) {
dialog.setContentView(R.layout.pickdestination);
ListView listPlace = (ListView) dialog
.findViewById(R.id.listPlace);
PlaceAdapter placeAdapter = new PlaceAdapter(parentActivity,
allMenuSearchList, R.layout.pickcity, dialog,
R.string.pickstartingpoint, null);
listPlace.setAdapter(placeAdapter);
giving error here-----------> dialog.show();
} else {
Toast.makeText(parentActivity, R.string.nonavigationtolink,
Toast.LENGTH_SHORT).show();
}
this.activity.finish();
}
非常感谢您的帮助。
onDestroy
回调中使用dismiss()
关闭我的对话框,当设备方向改变时会调用该回调。然后,在新的方向中,在onCreate
中重新显示dialog
,如果需要的话。 - Joshua Pinter