문제

I need to display a delete button on long press of a list item..

I've got the code for long press.. but don't know how to code for displaying a button inside this long press...

도움이 되었습니까?

해결책

Finally got the answer...

.xml file

<ImageButton
        android:id="@+id/imgdelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/delete" 
        android:visibility="invisible"/>

.java file

      lv.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            arg1.findViewById(R.id.imgdelete).setVisibility(View.VISIBLE);
            return false;
        }
    });
}

다른 팁

First you have to make that delete button invisible using code, or setting it's property in xml file. When the user clicks on longpress you have to make that delete button visible. After the delete action is completed, make that button invisible again.

You can use Alert dialog. Here is an example

listView.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> parent, View view,
                    int position, long id) {
                final CharSequence[] items = { "Delete Item" };


                AlertDialog.Builder builder = new AlertDialog.Builder(
                        [CLASS_NAME].this);
                builder.setTitle("Delete Item");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        Intent i;
                        switch (item) {
                        case 0:
                            AlertDialog.Builder builder = new AlertDialog.Builder(
                                    SelectProfile.this);
                            builder.setMessage(
                                    "Are you sure you want to delete?")
                                    .setCancelable(false)
                                    // Prevents user to use "back button"
                                    .setPositiveButton(
                                            "Delete",
                                            new DialogInterface.OnClickListener() {
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int id) {
                                                    //Todo code here
                                                }
                                            })
                                    .setNegativeButton(
                                            "Cancel",
                                            new DialogInterface.OnClickListener() {
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int id) {
                                                    dialog.cancel();
                                                }
                                            });
                            builder.show();
                            break;
                        }
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
                return false;
            }
        });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top