对话框消息不起作用

时间:2015-02-07 12:20:46

标签: android listview android-alertdialog

我有这个数组

String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows8", "Max OS X", "Linux", "OS/2" };

我需要在ListView和适配器中显示元素,当我单击对话框消息中的某个元素时,我需要显示单击的元素。

public class zadaca1 extends ListActivity {


 String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows8", "Max OS X", "Linux", "OS/2" };
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.layout_zadaca1);


        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values));

        ListView listView=getListView();
        listView.setTextFilterEnabled(true);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog.Builder builder= new AlertDialog.Builder(getApplicationContext());
                        builder.setTitle("Delete entry");
                        builder.setMessage("Are you sure you want to delete this entry?");
                        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // continue with delete
                                dialog.cancel();
                            }
                        });
             //   builder.setMessage(((TextView)view).getText());
                builder.show();

            }

        });

    }

}

但是当我在手机上运行并点击某个项目时,不幸的是zadaca1已经停止了。

我也试试

AlertDialog.Builder builder= new AlertDialog.Builder(this);

AlertDialog.Builder builder= new AlertDialog.Builder(AlertDialog.Builder.this);

但此代码显示错误

2 个答案:

答案 0 :(得分:1)

您可以通过以下方式获取列表视图中的项目:

TextView nameyouWant = (TextView) view.findViewById(R.id.idofyourItem);

在ClickListener中显示警告对话框:

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

            //fetch data inside the listview item
            TextView Title = (TextView) view.findViewById(R.id.idofyourItem);
            String someTitle = Title.getText().toString();

                AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);

                //delete button
                builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                       //do some stuffs here!
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
                builder.setTitle("Delete ?");
                builder.setCancelable(false);

                AlertDialog dialog = builder.create();
                dialog.show();


            }
        });

答案 1 :(得分:1)

我试试这个并且工作正常

 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    AlertDialog.Builder builder= new AlertDialog.Builder(zadaca1.this);
                            builder.setTitle("You clicked on");
                             builder.setMessage(((TextView)view).getText());
                            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    // continue with delete
                                    dialog.cancel();
                                }
                            });
                 //
                    AlertDialog dialog = builder.create();
                    dialog.show();