Android 带有自定义 ListView 的对话框
Android Dialog With Custom ListView
我正在尝试制作一个内部带有自定义列表视图的对话框。但我收到此错误:“指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView() ".
活动代码:
public class ConfigActivity extends ActionBarActivity {
//... onCreate and blablabla
public void onClickUsoDados(View view) {
final ArrayList<CustomItem> data = init();//init() just get some data
CustomListAdapter adapter = new CustomListAdapter(this, data);
View v = ConfigActivity.this.getLayoutInflater().inflate(R.layout.layout_config_userdata_listview, null, true);
list = (ListView) v.findViewById(R.id.listView1);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String Slecteditem = data.get(position).nome;
Toast.makeText(ConfigActivity.this, Slecteditem, Toast.LENGTH_SHORT).show();
}
});
final Dialog dialogOut = new Dialog(ConfigActivity.this);
dialogOut.setContentView(list);
dialogOut.setCancelable(true);
dialogOut.setTitle("Dados");
dialogOut.buNeutralButton("Voltar",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialogOut.dismiss();
}
});
dialogOut.show();
}
}
CustomItem.java
public class CustomItem {
public String usuario;
public String nome;
public String curso;
}
CustomListAdapter.java
public class CustomListAdapter extends ArrayAdapter<CustomItem> {
private Activity context;
private ArrayList<CustomItem> data;
public CustomListAdapter(Activity context, ArrayList<CustomItem> item) {
super(context, R.layout.layout_config_userdata_row, item);
this.context = context;
this.data = item;
}
public View getView(int position, View convertView, ViewGroup parent) {
CustomItem user = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.layout_config_userdata_row, parent, true);
}
TextView uUsername = (TextView) convertView.findViewById(R.id.textViewListViewUsuario);
TextView uName = (TextView) convertView.findViewById(R.id.textViewListViewNome);
TextView uCurso = (TextView) convertView.findViewById(R.id.textViewListViewCurso);
uUsername.setText(user.usuario);
uName.setText(user.nome);
uCurso.setText(user.curso);
return convertView;
}
}
我在“dialogOut.show();”上收到此错误,或者,如果我使用生成器,我在“[=38=”上收到此错误]dialog.create()”。想法?
编辑 1:
xml 个文件...
layout_config_userdata_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout //stuff>
<LinearLayout <!--stuff-->>
<LinearLayout <!--stuff-->>
<LinearLayout <!--stuff-->>
<TextView <!--stuff-->/>
<TextView
android:id="@+id/textViewListViewUsuario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="userToLogin"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
<LinearLayout <!--stuff-->>
<LinearLayout <!--stuff-->>
<TextView <!--stuff-->/>
<TextView
android:id="@+id/textViewListViewNome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nomeUser"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
<LinearLayout <!--stuff-->>
<LinearLayout <!--stuff-->>
<TextView <!--stuff-->/>
<TextView
android:id="@+id/textViewListViewCurso"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cursoNome"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<FrameLayout <!--stuff-->>
<ImageView <!--stuff-->/>
</FrameLayout>
</LinearLayout>
而 layout_config_userdata_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
EDIT2:堆栈跟踪。
09-29 12:50:24.073 19813-19813/com.ztesteconfig E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View.onClick(View.java:3680)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17229)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View.onClick(View.java:3675)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17229)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: 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:3439)
at android.view.ViewGroup.addView(ViewGroup.java:3310)
at android.view.ViewGroup.addView(ViewGroup.java:3286)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:337)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
at android.app.Dialog.setContentView(Dialog.java:480)
at com.ztesteconfig.ConfigActivity.onClickUsoDados(ConfigActivity.java:334)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View.onClick(View.java:3675)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17229)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
在 com.ztesteconfig.ConfigActivity.onClickUsoDados(ConfigActivity.java:334) 正是 "dialogOut.show();"
试试这个
if(view.getParent()!=null)
((ViewGroup)view.getParent()).removeView(v);
好的,那么,让我们回顾一下:
您的错误信息是"the specified view already has a parent"
您的布局中有一个您没有使用的 LinearLayout
您的 Java 代码专门忽略了 LinearLayout
因此,从布局资源中删除 LinearLayout
。你没有使用它。你不需要它。而且,这是错误的根源,因为 ListView
不能 both 在 LinearLayout
and 中对话框的内容视图。
或者,如果您有一个长期愿景,您将需要 LinearLayout
,请将 LinearLayout
作为对话框的内容视图,而不是 ListView
LinearLayout
.
尝试以编程方式创建列表视图..
使用
list = new ListView(getContext());
而不是
list = (ListView) v.findViewById(R.id.listView1);
或
将您的 xml 编辑为
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/base_view">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
(向 LinearLayout 添加了一个 id)
并在您的 activity 代码中
LinearLayout base=v.findViewById(R.id.base_view);
并将此视图设置为对话框的内容
dialogOut.setContentView(base);
我正在尝试制作一个内部带有自定义列表视图的对话框。但我收到此错误:“指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView() ".
活动代码:
public class ConfigActivity extends ActionBarActivity {
//... onCreate and blablabla
public void onClickUsoDados(View view) {
final ArrayList<CustomItem> data = init();//init() just get some data
CustomListAdapter adapter = new CustomListAdapter(this, data);
View v = ConfigActivity.this.getLayoutInflater().inflate(R.layout.layout_config_userdata_listview, null, true);
list = (ListView) v.findViewById(R.id.listView1);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String Slecteditem = data.get(position).nome;
Toast.makeText(ConfigActivity.this, Slecteditem, Toast.LENGTH_SHORT).show();
}
});
final Dialog dialogOut = new Dialog(ConfigActivity.this);
dialogOut.setContentView(list);
dialogOut.setCancelable(true);
dialogOut.setTitle("Dados");
dialogOut.buNeutralButton("Voltar",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialogOut.dismiss();
}
});
dialogOut.show();
}
}
CustomItem.java
public class CustomItem {
public String usuario;
public String nome;
public String curso;
}
CustomListAdapter.java
public class CustomListAdapter extends ArrayAdapter<CustomItem> {
private Activity context;
private ArrayList<CustomItem> data;
public CustomListAdapter(Activity context, ArrayList<CustomItem> item) {
super(context, R.layout.layout_config_userdata_row, item);
this.context = context;
this.data = item;
}
public View getView(int position, View convertView, ViewGroup parent) {
CustomItem user = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.layout_config_userdata_row, parent, true);
}
TextView uUsername = (TextView) convertView.findViewById(R.id.textViewListViewUsuario);
TextView uName = (TextView) convertView.findViewById(R.id.textViewListViewNome);
TextView uCurso = (TextView) convertView.findViewById(R.id.textViewListViewCurso);
uUsername.setText(user.usuario);
uName.setText(user.nome);
uCurso.setText(user.curso);
return convertView;
}
}
我在“dialogOut.show();”上收到此错误,或者,如果我使用生成器,我在“[=38=”上收到此错误]dialog.create()”。想法?
编辑 1: xml 个文件...
layout_config_userdata_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout //stuff>
<LinearLayout <!--stuff-->>
<LinearLayout <!--stuff-->>
<LinearLayout <!--stuff-->>
<TextView <!--stuff-->/>
<TextView
android:id="@+id/textViewListViewUsuario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="userToLogin"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
<LinearLayout <!--stuff-->>
<LinearLayout <!--stuff-->>
<TextView <!--stuff-->/>
<TextView
android:id="@+id/textViewListViewNome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nomeUser"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
<LinearLayout <!--stuff-->>
<LinearLayout <!--stuff-->>
<TextView <!--stuff-->/>
<TextView
android:id="@+id/textViewListViewCurso"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cursoNome"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<FrameLayout <!--stuff-->>
<ImageView <!--stuff-->/>
</FrameLayout>
</LinearLayout>
而 layout_config_userdata_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
EDIT2:堆栈跟踪。
09-29 12:50:24.073 19813-19813/com.ztesteconfig E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View.onClick(View.java:3680)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17229)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View.onClick(View.java:3675)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17229)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: 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:3439)
at android.view.ViewGroup.addView(ViewGroup.java:3310)
at android.view.ViewGroup.addView(ViewGroup.java:3286)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:337)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
at android.app.Dialog.setContentView(Dialog.java:480)
at com.ztesteconfig.ConfigActivity.onClickUsoDados(ConfigActivity.java:334)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View.onClick(View.java:3675)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17229)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
在 com.ztesteconfig.ConfigActivity.onClickUsoDados(ConfigActivity.java:334) 正是 "dialogOut.show();"
试试这个
if(view.getParent()!=null)
((ViewGroup)view.getParent()).removeView(v);
好的,那么,让我们回顾一下:
您的错误信息是"the specified view already has a parent"
您的布局中有一个您没有使用的
LinearLayout
您的 Java 代码专门忽略了
LinearLayout
因此,从布局资源中删除 LinearLayout
。你没有使用它。你不需要它。而且,这是错误的根源,因为 ListView
不能 both 在 LinearLayout
and 中对话框的内容视图。
或者,如果您有一个长期愿景,您将需要 LinearLayout
,请将 LinearLayout
作为对话框的内容视图,而不是 ListView
LinearLayout
.
尝试以编程方式创建列表视图..
使用
list = new ListView(getContext());
而不是
list = (ListView) v.findViewById(R.id.listView1);
或
将您的 xml 编辑为
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/base_view">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
(向 LinearLayout 添加了一个 id)
并在您的 activity 代码中
LinearLayout base=v.findViewById(R.id.base_view);
并将此视图设置为对话框的内容
dialogOut.setContentView(base);