带有自定义ListView的Android对话框

时间:2015-09-29 11:58:22

标签: java android listview dialog

我正在尝试使用自定义列表视图制作一个Dialog。但是我收到了这个错误:“指定的孩子已经有了父母。你必须首先在孩子的父母上调用removeView()”。

ActivityCode:

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(); ”上收到此错误,或者,如果我使用构建器,则会在“ dialog.create()”。想法?

EDIT1: 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$1.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$1.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$1.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();”

3 个答案:

答案 0 :(得分:1)

好的,我们来看看:

  • 您的错误消息是“指定的视图已有父级”

  • 您的布局中有LinearLayout您未使用

  • 您的Java代码明确忽略了LinearLayout

因此,从布局资源中删除ListView。你没有使用它。你不需要它。并且,它是您的错误的来源,因为LinearLayout 不能在对话框的内容视图中的LinearLayout 中。

或者,如果您有一个需要LinearLayout的长期愿景,请将ListView作为对话框的内容视图,而不是LinearLayout内的{{1}} {1}}。

答案 1 :(得分:0)

试试这个

if(view.getParent()!=null)
((ViewGroup)view.getParent()).removeView(v);

答案 2 :(得分:0)

尝试以编程方式创建列表视图。

使用

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)

并在您的活动代码中

LinearLayout base=v.findViewById(R.id.base_view);

并将此视图设置为对话框的内容

dialogOut.setContentView(base);