0

我的活动中有多个 ListView(s),它们使用相同的 CustomAdapter。在我的 CustomAdapter 中,在每个 ListView 上,我将图像添加到 ListView 的单个项目中。

我只想管理图像单击侦听器(在我的 CustomAdapter 中)并显示自定义 AlertDialog。

但我只想在这个 AlertDialog 上绘制一个 ArrayList 的内容

我找到了第一个解决方案:以编程方式制作一个 LinearLayout 并用我的字典 ArrayList 填充它

编辑:这是 MyAdapter (为了让您理解,我让我的适配器更简单):

class MyAdapter extends ArrayAdapter<Drink> 
    {
            LayoutInflater inflat;
            private ArrayList<Drink> items;
            private String sousCategory;

            public MyAdapter(Context context, ArrayList<Drink> objects) 
            {
                super(context, R.layout.activity_sousboissons_list_item_elementsouscategorie, objects);
                this.items = objects;
                this.inflat = LayoutInflater.from(context);
                this.sousCategory = objects.get(0).getType();
            }
           private class ViewHolder{
                public TextView title;
                public TextView prix;
                public TextView desc;
                public TextView size;
                public ImageView img;
                public LinearLayout ll;
            }

           @Override
           public View getView(int position, View convertView, ViewGroup parent) {
               String currentKey = "";
               ViewHolder holder = null;
               Drink element = items.get(position);

               if (convertView == null) {
                       holder = new ViewHolder();
                       convertView = inflat.inflate(R.layout.activity_sousboissons_list_item_elementsouscategorie, null);
                       holder.title = (TextView) convertView.findViewById(R.id.sousboissons_element_title);
                       holder.desc =  (TextView) convertView.findViewById(R.id.sousboissons_element_desc);
                       holder.prix =  (TextView) convertView.findViewById(R.id.sousboissons_element_prix);
                       holder.size = (TextView) convertView.findViewById(R.id.sousboissons_element_size);

                       if (element.getPrice().size() > 1){
                                  EditText editT = new EditText(SousBoisson.this);
                           final AlertDialog.Builder alert = new AlertDialog.Builder(SousBoissonsActivity.this);
                           alert.setTitle(element.getName());
                   alert.setView(e);
                           holder.img.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                alert.show();
                            }
                        });
                           holder.ll.addView(holder.img);
                       }else{
                       }
                       convertView.setTag(holder);
               } else {
                       holder = (ViewHolder) convertView.getTag();
               }
               if (element != null) {
                   // Here I'm setting all the Text Value of my Labels
                   }
               }
               return convertView;
           }
    }

它运行良好,但是当我第二次单击它时出现错误:

03-27 19:50:11.007: E/AndroidRuntime(5416): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
03-27 19:50:11.007: E/AndroidRuntime(5416):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3618)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at android.view.ViewGroup.addView(ViewGroup.java:3489)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at android.view.ViewGroup.addView(ViewGroup.java:3465)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at com.android.internal.app.AlertController.setupView(AlertController.java:402)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at com.android.internal.app.AlertController.installContent(AlertController.java:242)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at android.app.AlertDialog.onCreate(AlertDialog.java:336)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at android.app.Dialog.dispatchOnCreate(Dialog.java:351)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at android.app.Dialog.show(Dialog.java:256)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at azur.mobile.incomrestau.SousBoissonsActivity$MyAdapter$1.onClick(SousBoissonsActivity.java:189)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at android.view.View.performClick(View.java:4211)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at android.view.View$PerformClick.run(View.java:17267)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at android.os.Handler.handleCallback(Handler.java:615)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at android.os.Looper.loop(Looper.java:137)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at android.app.ActivityThread.main(ActivityThread.java:4898)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at java.lang.reflect.Method.invokeNative(Native Method)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at java.lang.reflect.Method.invoke(Method.java:511)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-27 19:50:11.007: E/AndroidRuntime(5416):     at dalvik.system.NativeStart.main(Native Method)

我读过这很正常,因为我还是个孩子,所以我不能代替他。

我想用alert.setAdapter而不是用,alert.setView但我不知道..

编辑:问题不在于我的holder.ll..

4

2 回答 2

0

哦,它可以在我的 img 的 OnClickListener 中使用删除视图!

我刚刚补充说:

holder.img.setOnClickListener(new OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if(e.getParent() != null)
                                        ((ViewGroup) e.getParent()).removeView(e);
                                    alert.show();
                                }
                            });

:) 谢谢,希望它将使用自定义自定义布局而不是 editText。

于 2013-03-27T23:41:12.983 回答
0

它运行良好,但是当我第二次单击它时出现错误:

IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

这似乎是问题所在:

holder.ll.addView(holder.img);

一个快速的解决方法是检查是否img已经有一个父级,如果它确实删除了它:

if(holder.img.getParent() != null)
    ((ViewGroup) holder.img.getParent()).removeView(holder.img);
holder.ll.addView(holder.img);
于 2013-03-27T19:01:34.617 回答