In the Android application, I have implemented the functionality for dynamically adding a textView to linearLayout , and increasing the height of this Layout :
final View view2 = getLayoutInflater().inflate(R.layout.strings_layout, null); final TextView text = (TextView) view2.findViewById(R.id.txtview); text.setText(iing.getName()); params.height = params.height + layoutpar; linear.setLayoutParams(params); allEds.add(view2); linear.addView(view2); On each textView is a text.setOnClickListener(new View.OnClickListener() event text.setOnClickListener(new View.OnClickListener() , in which one of the operations is the operation of obtaining the index of the pressed textView and saving all textView that have an index less than the given index:
int position=allEds.indexOf(view2); for (int k=0; k<allEds.size();k++) { if (position>k) { allEdsdop.add(allEds.get(k)); } } After clicking on the textView , getting the index of the clicked textView and copying the view to another ListArray , a set of operations takes place, among which all textView from the textView , whose index is less than the position value. Deletion occurs in this way:
linear.removeAllViews(); params.height =0;//обнуляю высоту layout linear.setLayoutParams(params); allEds=new ArrayList<View>(); //очищаю весь список view allEds=allEdsdop; //присваиваю ему значение сохраненных ранее, у которых index меньше postition for (int k=0; k<allEds.size();k++) //в цикле заново создаю и размещаю textview в layout, и устанавливаю высоту { params.height =params.height+layoutpar; linear.setLayoutParams(params); linear.addView(allEds.get(k)); } And besides that, after the last item in the list, another textView and so on.
The problem is that after the first deletion, when the whole process has passed, the textView list with index less than position + new created textview , ALL textView , which have not been deleted (with index less position ), disappears text.setOnClickListener(new View.OnClickListener() event text.setOnClickListener(new View.OnClickListener() . Only the new textView , which is created after deleting and restoring the old textView . That is, nothing happens when you click on this textView.
Is there a way to delete the elements of their ArraList<> without creating copies of this ArrayList<> with the view that you do not need to delete and then re-creating all the elements on the screen so that the setOnClickListener event setOnClickListener not disappear from them? I saw that you can use the Iterator mechanism, but I still do not understand how to use it in my case and how it works with ArrayList .