显示alertDialog与单选按钮,当点击一个ListViewItem的(Show alertDi

2019-08-18 09:30发布

我有一个在它2项列表视图中,2项是“秒”和“分”当我按下“秒”我想一个alertDialogBox开EN秀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();
    }
}

Answer 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() 在您提供的代码,它漂浮在中间的地方!



文章来源: Show alertDialog with radiobuttons when click a listViewItem