我解決了定製的警告對話框問題。這是它有一個自定義的視圖,其中包含兩個按鈕一個用於數據庫操作和其他用於加載另一個activity.I想dismiss對話框執行數據庫操作後。但現在我不能宣佈任何命令,如dialog.dismiss()
或cancel()
或finish()
請有人幫我解決這個問題。無法關閉android
我的班級
@Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
final String title= item.getTitle();
final String snippet= item.getSnippet();
AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
if(title.equalsIgnoreCase("Your Location")){
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
}
else if(title.equalsIgnoreCase("")){
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
final Button park_button = new Button(context);
park_button.setHint("Park here");
// park_button.setBackgroundResource();
layout.addView(park_button);
park_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("parkbutton:Clicked");
// Delete DB Values
int count = db.getDatasCount();
for(int i = 0;i<count;i++){
db.deleteValues(i);
}
//latitude & longtitude (string)
String latitude = String.valueOf(lat);
String longtitude = String.valueOf(lon);
Log.d("Insert: ", "Inserting ..");
db.addData(new Datas(latitude, longtitude));
// Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
// Reading DB Data
Log.d("Reading: ", "Reading all Values...");
List<Datas> datas = db.getAllDatas();
for (Datas dat : datas) {
String log = "Id: "+dat.getID()+" ,Latitude: " + dat.getlat() + " ,Longtitude: " + dat.getlon();
// Writing DB data to log
Log.d("DB Data: ", log);
}
Intent f = new Intent(context,MainTabActivity.class);
context.startActivity(f);
}
});
final Button know_more_button = new Button(context);
know_more_button.setHint("Know more");
// park_button.setBackgroundResource();
layout.addView(know_more_button);
know_more_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences prefs = context.getSharedPreferences("myprefs", 0);
SharedPreferences.Editor editor =prefs.edit();
editor.putString("KEY_REFERENCE", snippet);
editor.commit();
Intent intent = new Intent(context, SinglePlaceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
dialog.setView(layout);
dialog.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
}
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
}
public void populateNow(){
this.populate();
}
}
嘗試顯示一個對話框,無需設置內容視圖'dialog.setView(佈局);' – vokilam 2013-03-04 11:39:23
但我需要另外設置視圖內的兩個按鈕,這正和否定按鈕 – 2013-03-04 11:44:04
所以問題是你不能從你的自定義佈局的按鈕的點擊監聽器隱藏對話框。這是對的嗎? – vokilam 2013-03-04 11:49:38