当单击ListView项目时显示带有单选按钮的AlertDialog。

7
我有一个listView,其中有两个项目:“秒”和“分钟” 当我按“秒”时,我想打开一个alertDialogBox显示5,10,15,...秒钟。当我按分钟时也是一样。 就像这样: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();
    }
}
1个回答

3
错误很明显,你漏掉了一个分号。你的代码应该像这样:
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()中。在您提供的代码中,它漂浮在无处!


我改了代码,太蠢了... 但是在showDialogBox上仍然出现错误。 怎么回事? 错误是 ShowDialog无法解析为类型 - mXX
最好您能够发布logcat,这样我们才能看到您所说的是什么。 - ElefantPhace
不可能,我无法编译,因为出现了错误。Logcat为空。 - mXX
2
应该使用上下文而不是ShowDialog.this。没有叫做ShowDialog的东西。应该使用SettingsActivity.this代替。 - Antrromet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接