简体   繁体   English

如何获取多个膨胀视图的ID?

[英]How to get ids of multiple inflated views?

Using inflated views i am getting add and remove buttons . 使用膨胀视图,我可以添加和删除按钮。 And i am getting multiple views.And onclick button only getting last view id .I need to get all view ids in a loop ,But i am unable to get the ids of multiple added views.How to get ids of multiple inflated views. 我正在获取多个视图。onclick按钮仅获取最后一个视图ID。我需要循环获取所有视图ID,但是我无法获取多个添加视图的ID。如何获取多个膨胀视图的ID。

addBuildingSuper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View addView = inflater.inflate(R.layout.add_edittext, null);
                final FrameLayout frame = addView.findViewById(R.id.frame);
                  btnCustomSuper = addView.findViewById(R.id.btnCustomSuper);
                ImageView remove = addView.findViewById(R.id.remove);

                remove.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((LinearLayout) addView.getParent()).removeView(addView);
                    }
                });
                customSupersLayout = new LinearLayout(getApplicationContext());
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                customSupersLayout.setLayoutParams(layoutParams);
                framer.addView(customSupersLayout);
                customSupersLayout.setVisibility(View.GONE);
                customSupersLayout.setClickable(true);
                View child = getLayoutInflater().inflate(R.layout.supers_dropdown, null);
                customSupersLayout.addView(child);
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                child.setLayoutParams(layoutParams2);
                listSupers = child.findViewById(R.id.listSupers);

                LinearLayout supers_dropdown=child.findViewById(R.id.spin_super);
                supers_dropdown.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        customSupersLayout.setVisibility(View.GONE);
                    }
                });
                btnCustomSuper.setOnClickListener(new View.OnClickListener() {
                    @Override

                    public void onClick(View v) {
                        if (customSupersLayout.getVisibility()==View.GONE){
                            btnCustomSuper = (Button) v;


                            try {
                                for (int i=0;i<jsonArray1.length();i++){
                                    JSONObject jsonObject = jsonArray1.getJSONObject(i);
                                    Log.i("supersadd",jsonObject.getString("name"));
                                    msupers.add(jsonObject.getString("name"));
                                    sArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.text_dropdown, msupers);
                                    synchronized (sArrayAdapter){
                                        sArrayAdapter.notifyDataSetChanged();
                                        //sArrayAdapter.notifyAll();
                                        listSupers.invalidate();
                                    }

                                    listSupers.setAdapter(sArrayAdapter);
                                }
                            }catch (Exception e){
                                e.printStackTrace();
                            }



                            customSupersLayout.setVisibility(View.VISIBLE);
                        }else {
                            customSupersLayout.setVisibility(View.GONE);
                        }


                    }
                });

                listSupers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {



                          listSupers = (ListView)parent;

                        synchronized (sArrayAdapter){
                            sArrayAdapter.notifyDataSetChanged();
                              sArrayAdapter.notifyAll();
                            listSupers.invalidate();
                        }


                        btnCustomSuper.setText(msupers.get(position));
                        listSupers.setTag(position);
                        JSONObject json = null;
                        try {
                            json = jsonArray1.getJSONObject(position);
                            getSuperId=json.getString("id");
                            superId.add(getSuperId);
                            Log.i("msg","btnCustomSupersID"+superId);
                             jsonObject = jsonArray1.getJSONObject(position);

                               Log.i("msg","listID"+listID);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        Log.i("msg","btnCustomSupers"+btnCustomSuper.getText().toString());
                            customSupersLayout.setVisibility(View.GONE);




                    }
                });


                layout.addView(addView);
            }
        });

It's the code for inflated views 这是膨胀视图的代码

Try using setTag to your View while adding in rootView 在添加rootView时尝试在View使用setTag

addView.setTag(counter);
rootView.addView(addView);

And get View by using same tag. 并通过使用相同的标签获取View

View addView = rootView.findViewWithTag(counter)

Where counter can be anything but different for all Views. 对于所有视图,counter可以是任何其他值。

EDIT : 编辑

Just after fetching remove ImageView: 刚获取后删除ImageView:

counter++;
ImageView remove = addView.findViewById(R.id.remove);
remove.setTag("r"+counter);
remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String tag = v.getTag();
                    View viewToRemove = layout.findViewWithTag(tag.replace("r",""));
                    layout.removeView(viewToRemove);
                }
            });

Just before you are adding addView to layout, do this: 在将addView添加到布局之前,请执行以下操作:

addView.setTag(counter);
layout.addView(addView);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从以编程方式夸大的布局访问视图? - How to access views from the programatically inflated layouts? 如何以线性布局访问膨胀视图 - How to access inflated views in a linear layout 用ButterKnife绑定所有视图后如何绑定活动中膨胀的视图 - How to bind view inflated in activity after bind all the views with ButterKnife 如何删除添加到膨胀的 LinearLayout 的视图之间不需要的空间? - How to remove unwanted spaces between views added to a inflated LinearLayout? 如何从布局中获取膨胀的片段引用? - How to get the inflated Fragment Reference from Layout? 动态地将onClickListener分配给膨胀的视图 - Dynamically assign onClickListener to inflated Views 如果片段在另一个布局中膨胀,如何获取对该片段的视图引用? - How to get a reference of a view to a fragment if the fragment is inflated inside another layout? 如何根据MongoDB中的唯一ID获取多个属性? - How can I get the multiple attributes according to unique ids in MongoDB? 我将视图膨胀了 5 次,膨胀的视图有一个 EditText,现在我在屏幕上有 5 个 EditText,如何从这些 EditTexts 中获取文本 - I inflated a view 5 times, the view which was inflated had a EditText, now i have 5 EditText on the screen, how to get the text from those EditTexts 如何关闭充气布局? - How to close inflated layout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM