Listview的setOnItemLongClickListener在Android中不显示AlertDialog

时间:2012-12-31 12:28:54

标签: android listview android-alertdialog

开发应用程序时出现了混乱,我在列表中列出了SQLite数据。我希望让它像它一样工作,因为用户长时间点击它应该显示一个包含4-5项的警告对话框。但它失败了。如果有人有这个想法吗?所以请指导我。

我已经使用过此代码,但无效....

lv.setOnItemClickListener ( new OnItemClickListener() {

    public void onItemClick ( AdapterView<?> parent, View view,int position, long id) {
        final CharSequence[] items = {"India", "US", "UK","Australia"};
        AlertDialog.Builder builder = new    AlertDialog.Builder(CustomerConView.this);

        builder.setTitle("Alert Dialog with ListView");

        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {                                 
                Toast.makeText(getApplicationContext(), "hi", Toast.LENGTH_SHORT).show();
            }
        });

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

});

...谢谢

4 个答案:

答案 0 :(得分:1)

使用方法:registerForContextMenu()

您必须在活动的onCreate上调用它,并且必须实现方法“onCreateContextMenu()”。

答案 1 :(得分:1)

public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
        // TODO Auto-generated method stub
        Log.d("postiton", String.valueOf(position));
        //switch(position)
        //{
        //case 0:
        if(position==0)
        {
            if(Session.getActiveSession()==null)
            {
                Toast.makeText(MainActivity.this, "You must login first", Toast.LENGTH_LONG).show();
            }
            else
            {
                dialog=new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("Update Status");

                LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                dialogView = inflater.inflate(R.layout.update_status, null);
                //dialog.
                dialog.setView(dialogView);
                dialog.setPositiveButton("Update Status", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int ButtonId) {
                        // TODO Auto-generated method stub
                        UpdateStatus();
                    }
                });
                dialog.show();
            }
        }

        //case 1:
        if(position==1) 
        {

            if(Session.getActiveSession()==null)
            {
                Toast.makeText(MainActivity.this, "You must login first", Toast.LENGTH_LONG).show();
            }
            else
            {
                dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("Upload Photo");

                LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                dialogView = inflater.inflate(R.layout.post_photo, null);

                Button browse = (Button)dialogView.findViewById(R.id.browsePhotoButton);
                browse.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent i = new Intent(
                                Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                                startActivityForResult(i, RESULT_LOAD_IMAGE);
                    }
                });

                dialog.setView(dialogView);
                dialog.setPositiveButton("Upload Photo", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int ButtonId) {
                        // TODO Auto-generated method stub
                        publishStory(image);
                    }
                });

                dialog.show();
            }
        }

        if(position==2)
        {
            if(Session.getActiveSession()==null)
            {
                Toast.makeText(MainActivity.this, "You must login first", Toast.LENGTH_LONG).show();
            }
            else
            {
                Intent friendsActivity = new Intent(MainActivity.this,Friends.class);
                startActivity(friendsActivity);
            }
        }


    }

看到这个我也在listview项目点击上显示对话框。

问题是您没有设置警告对话框的视图以显示您应该为其分配一些布局以显示在警告对话框中。试试吧。

答案 2 :(得分:0)

使用此功能,这可能会对您有所帮助。 。 。 。

public boolean onContextItemSelected(MenuItem aItem) 
    {
        AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) aItem.getMenuInfo();
        switch (aItem.getItemId()) 
        {
            case 0:
                try 
                {
                    // your code 
                }
                catch (Exception ewe) 
                {
                    Log.e("Exception",""+ewe)
                }       
                break;

            case 1:
                           ................ for all your cases

            break;  
        }
        return false;
    }

答案 3 :(得分:0)

获取应用程序上下文在其他类

中不起作用

所以写classname.this

  

例如myservice.this

myservice是我的班级名称