• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Cloning Views in Android

kyungjin

Lurker
I'm currently working on an Android application where I need to dynamically generate views and therefore need to be doing some of my UI coding in Java. The issue that I'm currently facing is that I need to be able to generate deep copies of many individual View objects and add them to a large LinearLayout view which in turn goes into a ScrollView. Below is a hypothetical example of an application that demonstrates similar problems to the real application I want to create.
Let's say that a car company has N number of cars affected by a recent recall and all the data about those cars (name, year, type, etc.) are all compiled in some sort of database. The developer of this application wants to pull all the data regarding a specific car and format it according to a XML layout file (carlayout.xml) arranged in a layout relative to how it is setup in the file. That XML file should contain only TextView elements that should be set to the relevant data from the database of its respective car. After all the data for the specified car is processed, that view should then be added to a LinearLayout containing all the car views. This process should then repeat until all cars are processed. After all the individual car views are added to the car list LinearLayout, the LinearLayout view is to be added to a ScrollView since ScrollView can only host one direct child. Here's the pseudocode for what I am trying to accomplish:​
Code:
File carDB;
[COLOR=Green]// Contains the database of cars that have issues already loaded from a file.[/COLOR]

[COLOR=Green]// The ScrollView provides scrolling capability for the LinearLayout that 
holds all the cars since ScrollView can only directly host one child.[/COLOR]
ScrollView carList_scrollView = [B][COLOR=Purple]new[/COLOR][/B] ScrollView();
LinearLayout carList_linearLayout = [B][COLOR=Purple]new[/COLOR][/B] LinearLayout();

[COLOR=Green]// curCar_view references the View created from the carlayout.xml file 
that contains just TextViews that reference the data that it should 
display.  The idea is that multiple curCar_views (each of which represent 
one car) are to be added into the carList_linearLayout which will house all 
the cars.  This single carList_linearLayout will then be added to 
carList_scrollView.[/COLOR]
View curCar_view;
LayoutInflater curCar_inflater;

curCar_inflater = [B][COLOR=Purple]this[/COLOR][/B].getLayoutInflater();
curCar_view = curCar_inflater.inflate(R.layout.carlayout, [COLOR=Purple][B]null[/B][/COLOR]);


[COLOR=Green]// curCar_view references the View created from the carlayout.xml file 
that contains just TextViews that reference the data that it should 
display.  The idea is that multiple curCar_views (each of which represent 
one car) are to be added into the carList_linearLayout which will house all 
the cars.  This single carList_linearLayout will then be added to 
carList_scrollView.[/COLOR]
TextView carName_text = (TextView) curCar_view.findViewById(R.id.carName_text);
TextView carType_text = (TextView) curCar_view.findViewById(R.id.carType_text);
TextView carYear_text = (TextView) curCar_view.findViewById(R.id.carYear_text);

[B][COLOR=Purple]for[/COLOR][/B]([B][COLOR=Purple]int[/COLOR][/B] carCounter = 0; carCounter < N; carCounter++)
{[INDENT][COLOR=Green]// Store relevant data from the car database into 
curCar_view's respective TextView element.[/COLOR]
carName_text.setName(carDB.getNextName());
carType_text.setType(carDB.getNextType());
carYear_text.setYear(carDB.getNextYear());

[COLOR=Green]// Add the curCar_view into the carList_linearLayout (to be 
repeated for all N number of cars) - BP1[/COLOR]
carList_linearLayout.addView(curCar_view);
[/INDENT]}

[COLOR=Green]// Add the carList_linearLayout into the carList_scrollView to give the 
layout scroll capabilities[/COLOR].
carList_scrollView.addView(carList_linearLayout);
The problem at hand is that this code works fine for one car, however if I try to use this code on a set of two or more cars, an error appears saying that the child view is already present in the parent view at the line located by BP1. I suspect that this issue is due to the fact that the reference value of curCar_view is getting passed and that even though the values of the TextViews it contains is changing, the object itself is the same.

Coming from a C++ background where all objects are strictly pass-by-copy I naturally thought this would not be much of an issue. However seeing as Java passes all things more-or-less by reference (I know it's pass-by-value of the reference address but for simplicity sake I'll just refer to it as pass-by-reference), I don't see how I can make a deep copy of the View object at hand.

I appreciate any ideas from the community.
 
Back
Top Bottom