I have programmatically created entries, this is TextView and its Checkbox . I need to change the code in order to remove them together under certain conditions. Here is the code to create them:

 ArrayList<LinearLayout.LayoutParams> lParams_r = new ArrayList<LinearLayout.LayoutParams>(); int size_strok_r = 250; int check_left_margin = 125; fontW = 11; lParams_r.add(0, new LinearLayout.LayoutParams(size_strok_r, wrapContent)); lParams_r.get(0).gravity = Gravity.LEFT; ArrayList<LinearLayout.LayoutParams> lParams_check_r = new ArrayList<LinearLayout.LayoutParams>(); lParams_check_r.add(0, new LinearLayout.LayoutParams(size_strok, matchParent)); lParams_check_r.get(0).gravity = Gravity.RIGHT; lParams_check_r.get(0).leftMargin = check_left_margin; ArrayList<LinearLayout> LNew_r = new ArrayList<LinearLayout>(); LNew_r.add(0, new LinearLayout(this)); LNew_r.get(0).setPadding(0, 7, 0, 0); if (first_schet_id == 0) first_schet_id = idbd; ArrayList<TextView> KatNew_r = new ArrayList<TextView>(); KatNew_r.add(0, new TextView(this)); htmlTaggedString = "<u><b>" + str + "</b></u>"; textSpan = android.text.Html.fromHtml(htmlTaggedString); KatNew_r.get(0).setMaxLines(1); KatNew_r.get(0).setId(idbd + 98989); KatNew_r.get(0).setTag(idbd + 98989); KatNew_r.get(0).setText(textSpan); KatNew_r.get(0).setTextSize(fontW); KatNew_r.get(0).setPadding(20, 10, 0, 0); KatNew_r.get(0).setTextColor(getResources().getColor(R.color.white_cl)); KatNew_r.get(0).setTypeface(typeFace); LNew_r.get(0).addView(KatNew_r.get(0), lParams_r.get(0)); ArrayList<CheckBox> CheckNew_r = new ArrayList<CheckBox>(); CheckNew_r.add(0, new CheckBox(this)); CheckNew_r.get(0).setChecked(true); CheckNew_r.get(0).setButtonDrawable(R.drawable.customcheckboxselector); CheckNew_r.get(0).setId(idbd + 99999); CheckNew_r.get(0).setTag(idbd); LNew_r.get(0).addView(CheckNew_r.get(0), lParams_check_r.get(0)); ArrayList<LinearLayout.LayoutParams> lParamsRM = new ArrayList<LinearLayout.LayoutParams>(); lParamsRM.add(0, new LinearLayout.LayoutParams(wrapContent, wrapContent)); 

And here is how I try to delete:

 removeViewFrom(submenu_right, vibranniSCHET + 98989); removeViewFrom(submenu_right, vibranniSCHET + 99999); public void removeViewFrom(LinearLayout[] views, int id) { for (LinearLayout l : views) { l.removeView(findViewById(id)); } } 

Deletion does not work, and I do not know why. Help is needed.

    1 answer 1

    To remove a View , you must first obtain this View . All elements are contained in two ArrayList for TextView and for CheckBox . As I understand it, vibranniSCHET is the sequence number of the selected item. To match the element number in an ArrayList , add elements to its end, not the beginning:

     KatNew_r.add(new TextView(this)); CheckNew_r.add(new CheckBox(this)); 

    Accordingly, we can simply obtain the element itself by the element number.

    Pass one parameter to the method for deletion: the number of the element to be deleted. Lists with TextView and CheckBox can be stored globally. Here is the method for deleting TextView and CheckBox :

     public void removeViewFrom(int position) { TextView textView = KatNew_r.get(position); textView.setVisibility(View.GONE); KatNew_r.remove(position); CheckBox checkBox = CheckNew_r.get(position); checkBox.setVisibility(View.GONE); CheckNew_r.remove(position); } 
    • hmmm .. need to try - Moonwolf45
    • yes it is true I did redo a little bit - Moonwolf45
    • @ Moonwolf45, in Java it is customary to give names to variables and methods with a small letter. It is easier to distinguish them from class names. - Kirill Malyshev
    • I will consider for the future - Moonwolf45