gpt4 book ai didi

android - 长按 ListView android后显示对话框

转载 作者:行者123 更新时间:2023-11-30 04:09:12 26 4
gpt4 key购买 nike

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

感谢您的一些回答,祝您有愉快的一天。

最佳答案

按如下方式使用 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);

}

关于android - 长按 ListView android后显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11095298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com