6

我有一個列表視圖,其中有2個項目,這兩個項目是「秒」和「分鐘」 當我按「秒」時,我想要一個alertDialogBox打開en show 5 ,10,15,...秒。同樣的,當我按分鐘當單擊listViewItem時顯示帶單選按鈕的alertDialog

事情是這樣的:

enter image description here

但我無法實現它,因爲我不很瞭解它是如何工作的。以下是我的代碼:

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class SettingsActivity extends Activity { 

    ListView listView = (ListView) findViewById(R.id.settingsList); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.settings); 

     String[] values = new String[] { "Seconds", "Minutes" }; 

     // Define a new Adapter 
     // First parameter - Context 
     // Second parameter - Layout for the row 
     // Third parameter - ID of the TextView to which the data is written 
     // Forth - the Array of data 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, android.R.id.text1, values); 

     // Assign adapter to ListView 
     listView.setAdapter(adapter); 

     listView.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> arg0, View arg1, 
        int position, long arg3) { 
       AlertDialogView(); 
      } 
     }); 

    } 

    private void AlertDialogView() { 
     final CharSequence[] items = { "15 secs", "30 secs", "1 min", "2 mins" }; 

     AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);//ERROR ShowDialog cannot be resolved to a type 
     builder.setTitle("Alert Dialog with ListView and Radio button"); 
     builder.setSingleChoiceItems(items, -1, 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int item) { 
         Toast.makeText(getApplicationContext(), items[item], 
           Toast.LENGTH_SHORT).show(); 
        } 
       }); 

     builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     }); 

     builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     }); 

     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 
} 

回答

2

錯誤很明顯,您錯過了semicolon。你的代碼應該像

listView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
     { 
      AlertDialogView(); 
     }//ERROR I'm GETTING HERE is Syntax error, insert ";" to complete Statement 
    }; 

和上面的代碼應該在onCreate()裏面。在你提供的代碼中,它在無處不在的情況下浮動!

+0

我改變了代碼,我是多麼愚蠢......但我仍然在showDialogBox上出現錯誤。怎麼來的?錯誤是'ShowDialog無法解析爲類型' – mXX

+0

如果您發佈logcat,以便我們可以看到您正在談論的內容會更好 – ElefantPhace

+0

不可能,我無法編譯,因爲我有錯誤。 Logcat是空的 – mXX

相關問題