Cloning Views in Android


Last Updated:

  1. kyungjin

    kyungjin Member This Topic's Starter

    Joined:
    Mar 29, 2010
    Messages:
    5
    Likes Received:
    2
    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 (Text):
    1.  
    2. File carDB;
    3. [COLOR=Green]// Contains the database of cars that have issues already loaded from a file.[/COLOR]
    4.  
    5. [COLOR=Green]// The ScrollView provides scrolling capability for the LinearLayout that
    6. holds all the cars since ScrollView can only directly host one child.[/COLOR]
    7. ScrollView carList_scrollView = [B][COLOR=Purple]new[/COLOR][/B] ScrollView();
    8. LinearLayout carList_linearLayout = [B][COLOR=Purple]new[/COLOR][/B] LinearLayout();
    9.  
    10. [COLOR=Green]// curCar_view references the View created from the carlayout.xml file
    11. that contains just TextViews that reference the data that it should
    12. display.  The idea is that multiple curCar_views (each of which represent
    13. one car) are to be added into the carList_linearLayout which will house all
    14. the cars.  This single carList_linearLayout will then be added to
    15. carList_scrollView.[/COLOR]
    16. View curCar_view;
    17. LayoutInflater curCar_inflater;
    18.  
    19. curCar_inflater = [B][COLOR=Purple]this[/COLOR][/B].getLayoutInflater();
    20. curCar_view = curCar_inflater.inflate(R.layout.carlayout, [COLOR=Purple][B]null[/B][/COLOR]);
    21.  
    22.  
    23. [COLOR=Green]// curCar_view references the View created from the carlayout.xml file
    24. that contains just TextViews that reference the data that it should
    25. display.  The idea is that multiple curCar_views (each of which represent
    26. one car) are to be added into the carList_linearLayout which will house all
    27. the cars.  This single carList_linearLayout will then be added to
    28. carList_scrollView.[/COLOR]
    29. TextView carName_text = (TextView) curCar_view.findViewById(R.id.carName_text);
    30. TextView carType_text = (TextView) curCar_view.findViewById(R.id.carType_text);
    31. TextView carYear_text = (TextView) curCar_view.findViewById(R.id.carYear_text);
    32.  
    33. [B][COLOR=Purple]for[/COLOR][/B]([B][COLOR=Purple]int[/COLOR][/B] carCounter = 0; carCounter < N; carCounter++)
    34. {[INDENT][COLOR=Green]// Store relevant data from the car database into
    35. curCar_view's respective TextView element.[/COLOR]
    36. carName_text.setName(carDB.getNextName());
    37. carType_text.setType(carDB.getNextType());
    38. carYear_text.setYear(carDB.getNextYear());
    39.  
    40. [COLOR=Green]// Add the curCar_view into the carList_linearLayout (to be
    41. repeated for all N number of cars) - BP1[/COLOR]
    42. carList_linearLayout.addView(curCar_view);
    43. [/INDENT]}
    44.  
    45. [COLOR=Green]// Add the carList_linearLayout into the carList_scrollView to give the
    46. layout scroll capabilities[/COLOR].
    47. carList_scrollView.addView(carList_linearLayout);
    48.  
    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.
     

    Advertisement

Share This Page

Loading...