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

问题描述 投票:1回答:3

您好,在我的应用程序中,我将从游标中获取数据并将其放入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;

这里是带有列表的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>

谢谢你...

android dialog onlongclicklistener
3个回答
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);

}

}


0
投票

尝试此代码:


0
投票

对话框应使用如下的Dialg构建器构建:

© www.soinside.com 2019 - 2024. All rights reserved.