Question

i am working on list , in this list setOnItemLongClickListener write the code part ,user long press to open dialog but dialog is not open ?please send any suggestion for open dialog?

listshipments.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() 
        {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) 
            {
                view.setSelected(true);
                TextView tv = (TextView) view.findViewById(R.id.txtName);
                String shipmenttxt = tv.getText().toString();
                int b=delete_Message("Delete ", "Do you want delete shipment id", "Delete", "Cancel",shipmenttxt,position);
                if(b==1){
                    this.mList.remove(position);
                    adapter.notifyDataSetChanged(); 
                }
                return true;

            }

    });

@SuppressWarnings("deprecation")
    private int delete_Message(String sTitle,String sMessage,String sButton1_Text,String sButton2_Text,final String msg,final int position)
    {

        try {
            alertDialog = new AlertDialog.Builder(getParent()).create();
            alertDialog.setTitle(sTitle);
            alertDialog.setIcon(R.drawable.info);
            alertDialog.setMessage(sMessage);
            alertDialog.setButton(sButton1_Text, new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int which) 
                {
                    aa=1;
                    delete(msg);

                    //new LoadDatashipment().execute();
                    return ;

                } });

            alertDialog.setButton2(sButton2_Text, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    aa=0;
                    //return;
                }});

            alertDialog.show();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

        return aa;
    }
Était-ce utile?

La solution

If any row item of list contains focusable or clickable view then your click listener might not work properly

you must put this line in your custom listviews row_item.xml file i.e. android:descendantFocusability="blocksDescendants"

For eg:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:descendantFocusability="blocksDescendants"
//other layout info here .....
>
</LinearLayout>

i think what you need to do is before showing your Dialog

alertD = alertDialog.create();

and show

alertD.show();

check here for example http://www.mkyong.com/android/android-alert-dialog-example/

Autres conseils

try this code:

listshipments.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        view.setSelected(true);
        TextView tv = (TextView) view.findViewById(R.id.txtName);
        String shipmenttxt = tv.getText().toString();

        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    //TODO Yes button clicked
                    this.mList.remove(position);
                    adapter.notifyDataSetChanged();
                    break;
                case DialogInterface.BUTTON_NEGATIVE:
                    //TODO No button clicked
                    dialog.dismiss();
                    break;
                }
            }
        };
        AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
        builder.setMessage("Extract " + rarListView.getItemAtPosition(arg2).toString() + " \n to '" + Environment.getExternalStorageDirectory().toString() + "/AndRar/' folder?")
            .setPositiveButton("Yes", dialogClickListener)
            .setNegativeButton("No", dialogClickListener)
            .show();
        return true;
    }
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top