长按列表视图android后显示一个对话框

时间:2012-06-19 06:08:40

标签: android dialog onlongclicklistener

我的应用程序中的Hello我将从游标中获取数据并将其放在ListActivity中。

我想长按一下对话框。

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;

以下是包含List ...

的XML文件
<?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>

带有光标列的XML文件是......

<?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>

提前谢谢你......

3 个答案:

答案 0 :(得分:1)

使用setOnItemLongClickListener,如下所示..

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

}

}

答案 1 :(得分:0)

试试这段代码:

selectRoom.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
            return onLongListItemClick(v,pos,id);
        }
        protected boolean onLongListItemClick(View v, final int pos, long id) {
            /////Display Dialog Here.......................

                    return true;

    });

答案 2 :(得分:0)

应使用dialg构建器构建

对话框:

AlertDialog alertDialog = new AlertDialog.Builder(view.getContext()).create();
alertDialog.setTitle("Reset...");
alertDialog.setMessage("Are you sure?");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
      // here you can add functions
   }
});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();

希望它有所帮助:)