display a dialog after long click on list view android

yama picture yama · Jun 19, 2012 · Viewed 11.4k times · Source

in my application I will fetch data from a cursor and put it in a ListActivity.

If a long click on a list item happens it should display a dialog.

Now I'll show you some code:

final ListView selectRoom = (ListView) findViewById(android.R.id.list);
        selectRoom.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> adapter, View view,
                    int position, long id) {

                Cursor unitCursor = (Cursor) getListView().getItemAtPosition(
                        position);
                int rid = unitCursor.getInt(0);
                DBAccessor dba = new DBAccessor(getApplicationContext());
                dba.alterUnitName(rid);
                callAdapter();
                final Dialog d = new Dialog(getApplicationContext());
                d.setContentView(R.layout.settings);
                d.setTitle("My Dialog");
                d.show();

                return true;

Here is the XML file with the List...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/spinnerRooms"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </Spinner>

    <ListView
        android:id="@android:id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_below="@id/spinnerRooms">
    </ListView>

</RelativeLayout>

the XML file with the columns of cursor is...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/unitId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:visibility="invisible"/>

    <TextView
        android:id="@+id/unitName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/unitRoomId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:visibility="invisible"/>

</LinearLayout>

Thank you for some answers and have a nice day.

Answer

Munish Kapoor picture Munish Kapoor · Jun 19, 2012

Use the setOnItemLongClickListener as following..

 selectRoom.setOnItemLongClickListener(new OnItemLongClickListener(){
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            // TODO Auto-generated method stub
            Dialog();
  or 
final MyDialog rDialog=new MyDialog(this,this);
            return false;
        }

    } );

public void Dialog()    {

        AlertDialog.Builder dialogAlert = new AlertDialog.Builder(getApplicationContext());
        dialogAlert.setTitle("Demo ?");

        dialogAlert.show();
}

 //Another way

 public class MyDialog extends Dialog {

Activity act;
public ReminderSettingDialog(Context context,Activity act) {
    super(context);
    this.act=act;
    // TODO Auto-generated constructor stub
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.settings);

}

}