0

开发应用程序时存在混淆,我在列表中列出了 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

4 回答 4

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


    }

看到这个我也在列表视图项目点击上显示对话框。

您没有设置警报对话框视图以显示某些内容的问题,您应该为其分配一些布局以在警报对话框中显示。试试看。

于 2012-12-31T13:08:45.023 回答
1

使用方法:registerForContextMenu()

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

于 2012-12-31T12:45:09.890 回答
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;
    }
于 2012-12-31T12:34:17.260 回答
0

获取应用程序上下文在其他类中不起作用

所以写classname.this

例如 myservice.this

其中 myservice 是我的班级名称

于 2013-04-30T08:19:35.807 回答