I have a LinearLayout with vertical orientation as parent, I want to add some view programmatically multiple times to this parent. Right now I am inflating the child every time getting new references to every UI element before adding to parent. This doesn't seem to be very efficient, is there any better way of doing this.
Current code I am using is below, If I inflate only once before for loop I get runtime error "he specified child already has a parent. You must call removeView() on the child's parent first."
LayoutInflater inflator = LayoutInflater.from(getBaseContext());
LinearLayout parentPanel = findViewById(R.id.parent_pannel);
ArrayList<String> myList = getData();
for(String data : myList) {
// inflate child
View item = inflator.inflate(R.layout.list_item, null);
// initialize review UI
TextView dataText = (TextView) item.findViewById(R.id.data);
// set data
dataText.setText(data);
// add child
parentPanel.addView(item);
}
ListView
. – BehindListView
recycles Views for this same reason. Since you only see N views at a time it only inflates those that are necessary, and then just reuses them. Something to consider if you see yourself adding quite a bit of views. – Behind