這確實讓我陷入了困境,在我的Android應用程序中點擊Button
時,似乎這些方法被稱爲不同步。當我點擊Button
下面我想打電話:Android撥打電話的方法
loc = new Location(Options.this);
一旦完成,然後我想打電話:
setLocationPref();
在現實中會發生什麼事是,當我的程序進入下面的「displayLocations方法「再次跳回按鈕,並呼籲:
setLocationPref();
我認爲錯誤在於對兩行代碼:
builder.setItems(cs, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int item){
如果任何人有任何想法,請讓我知道,非常感謝:)
/** Location selection */
bLocation = (Button) findViewById(R.id.bLocation);
bLocation.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
loc = new Location(Options.this);
loc.locationSelection();
setLocationPref();
}
});
代碼轉回,我已經寫/ **失敗HERE */
/** Display locations in a list */
public void displayLocations(){
Log.d("displayLocations", "displayLocations ");
LocationSQL getSetLocation = new LocationSQL(context);
final ArrayList<String> locations = locSQL.allLocations();
final CharSequence[] cs = locations.toArray(new CharSequence[locations.size()]);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Pick your location.");
builder.setItems(cs, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int item){
/** FAILS HERE */
Toast.makeText(context, cs[item], Toast.LENGTH_SHORT).show();
String selectedLocation = locations.get(item);
updateLocationInfo(selectedLocation);
}
});
builder.create().show();
}