我想在用户执行Listview的onItemLongClick
时显示一个对话框,以确认用户是否想要这样做但是在adb.show()
的行中给出了这个错误:
指定的孩子已经有父母。您必须首先在孩子的父母上调用removeView()
使用以下代码:
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder adb = new AlertDialog.Builder(getBaseContext());
adb.setView(view);
adb.setTitle("Title of alert dialog");
adb.setIcon(android.R.drawable.ic_dialog_alert);
adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Menu.this, "OK", Toast.LENGTH_LONG).show();
} });
adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Menu.this, "cancel", Toast.LENGTH_LONG).show();
//finish();
} });
adb.show();
return true;
}
});
我搜索了答案,但我看到了listview或longclick,所以我需要帮助:(
添加了 logcat,它出错的行显示在adb.show();
:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3936)
at android.view.ViewGroup.addView(ViewGroup.java:3786)
at android.view.ViewGroup.addView(ViewGroup.java:3758)
at com.android.internal.app.AlertController.setupView(AlertController.java:492)
at com.android.internal.app.AlertController.installContent(AlertController.java:236)
at android.app.AlertDialog.onCreate(AlertDialog.java:356)
at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
at android.app.Dialog.show(Dialog.java:274)
at android.app.AlertDialog$Builder.show(AlertDialog.java:993)
at pt.isec.jogodememoria.MenuEscolheNivel$2.onItemLongClick(MenuEscolheNivel.java:119)
at android.widget.AbsListView.performLongPress(AbsListView.java:3121)
at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:3070)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:1)
一个非常简单的错误,这是listView.setOnItemLongClickListener(new AdapterView
.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
createDialog(view);
return true;
}
});
,并且不需要setView。您仍然可以创建一个单独的方法,并为每个长项目单击实例化它。
public void createDialog(View view){
AlertDialog.Builder adb = new AlertDialog.Builder(this);
//adb.setView(Main.this);
adb.setTitle("Title of alert dialog");
adb.setIcon(android.R.drawable.ic_dialog_alert);
adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
} });
adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_LONG).show();
//finish();
} });
AlertDialog alertDialog = adb.create();
alertDialog.show();
}
然后为对话框创建一个单独的方法:
res_count[key]+=each.instance_count
# change to since who knows what is in each.instance_count
res_count[key]+=1
#bottom line, keep the 2 count added the same way, we have no way knowing which to use
答案 1 :(得分:0)
我认为该行
adb.setView(view);
错了。
因为您正在尝试将项目视图(视图)作为主视图为您进行对话。 如果您对这些行进行注释,则代码应该可以正常工作。
你应该改变这一行
adb.show();
由此
AlertDialog dialog = adb.create();
dialog.show();
并通过YourActivity.this或getActivity()更改您的上下文(getBaseContext())。
问候。
答案 2 :(得分:0)
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
showDialog(view);
return true;
}
});
private void showDialog(View view) {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setView(view);//try it first with this line if it doesnt work delete it
adb.setTitle("Title of alert dialog");
adb.setIcon(android.R.drawable.ic_dialog_alert);
adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Menu.this, "OK", Toast.LENGTH_LONG).show();
} });
adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Menu.this, "cancel", Toast.LENGTH_LONG).show();
//finish();
} });
adb.create().show();
}
如果它没有工作,那么创建一个自定义布局,其中只包含对话框的主体,然后尝试这个:
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
showDialog(view);
return true;
}
});
private void showDialog() {
// TODO Auto-generated method stub
LayoutInflater inflater = this.getLayoutInflater();
final View customview = inflater.inflate(
R.layout.your_dialog_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customview);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
Dialog dialog = builder.create();
dialog.show();
}
我希望这会对你有所帮助:)。
答案 3 :(得分:0)
是因为您正在尝试使用包含父级的视图,因为您看不到。最好的方法是使用相同的布局来扩充另一个视图。
尝试使用另一种布局,使用相同的东西:
然后:
final AlertDialog.Builder adb = new AlertDialog.Builder(getBaseContext());
View newView = View.inflate(getBaseContext(),R.layout.my_layout,null);
adb.setView(newView);
adb.setTitle("Title of alert dialog");
adb.setIcon(android.R.drawable.ic_dialog_alert);
adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Menu.this, "OK", Toast.LENGTH_LONG).show();
}
});
adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Menu.this, "cancel", Toast.LENGTH_LONG).show();
//finish();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView < ? > parent, View view, int position, long id) {
adb.show();
return true;
}
});