【问题标题】:Show alertDialog with radiobuttons when click a listViewItem单击 listViewItem 时显示带有单选按钮的 alertDialog
【发布时间】:2013-03-31 16:45:52
【问题描述】:

我有一个包含 2 个项目的 listView,这 2 个项目是“秒”和“分钟” 当我按“秒”时,我想要一个 alertDialogBox 打开并显示 5,10,15,... 秒。按分钟时也一样

类似这样的:

但是我在实现它时遇到了麻烦,因为我不太了解它是如何工作的。这是我的代码:

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();
    }
}

【问题讨论】:

    标签: android android-listview android-spinner android-alertdialog onitemclicklistener


    【解决方案1】:

    错误很明显,您缺少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()里面。在您提供的代码中,它漂浮在不知名的地方!

    【讨论】:

    • 我更改了代码,我多么愚蠢...但我仍然在 showDialogBox 上收到错误消息。怎么会?错误是ShowDialog cannot be resolved to a type
    • 最好把logcat贴出来让我们看看你在说什么
    • 不可能,我无法编译,因为我遇到了错误。 Logcat 为空
    • 而不是 ShowDialog.this 它应该是上下文。没有所谓的 ShowDialog 这样的东西。它应该是 SettingsActivity.this。
    猜你喜欢
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 2011-12-14
    相关资源
    最近更新 更多