Custom Layout with a lot of TextViews: only showing the first set of TextViews

Joseph.JazdzewskiJoseph.Jazdzewski USMember ✭✭
edited May 2014 in Xamarin.Android

I am trying to make a sample Stocks app which uses a database that tracks the last sale price of a stock once a week or each day that the stock market is open since 2003. I am currently trying to get show all the recorded prices for a stock and currently it will only gets the latest stock price.

` protected override void OnCreate (Bundle bundle)
{ // this section works
base.OnCreate (bundle);
LinearLayout MyLayout = new LinearLayout (this);
MyLayout.Orientation = Orientation.Vertical;
cHistory myhistory = new cHistory(); //custom class
string sym = Intent.GetStringExtra ("symbol") ?? "";
myhistory.setSym (sym); // this sets the symbol that will be searched
myhistory.getStockInformation (); // fills the price and date attributes

        if (myhistory.prices.Count == { // the Count is about 2500 items
            for (int i = 0; i < myhistory.prices.Count; i++) {
                TextView myPrice = new TextView (this);
                myPrice.SetText (myhistory.prices.ToArray () [i].ToString(), TextView.BufferType.Normal );
                myPrice.LayoutParameters = new ViewGroup.LayoutParams (-1, -1);
                TextView myDate = new TextView (this);
                myDate.SetText ( () [i].ToString(), TextView.BufferType.Normal);
                myDate.LayoutParameters = new ViewGroup.LayoutParams (-1, -2);
                MyLayout.AddView (myDate);
                MyLayout.AddView (myPrice);
        SetContentView (MyLayout);

sorry the code is a little sloby but I am going to revise it later. this code is based on

P.S. part of my code got split from the rest but the part with the problem is in the code section


  • CheesebaronCheesebaron DKInsider, University mod

    I would probably use a ListView instead of a bunch of TextViews. A ListView uses an Adapter which helps you manage memory and in the end is probably a lot more easier to manage than a whole bunch of TextView instances nested in a LinearLayout.

    Now you may ask, why you would want to manage the memory? Well, spawning a new TextView will mean it will allocate memory both on the Java side and on the managed C# side, which will mean as soon as you spawn enough instances you will run out of memory.

    What a ViewGroup such as a ListView does, is that it uses an Adapter which has a built in way of recycling Views and only ever instantiates more than shown on the screen. This way you will not run into memory issues, and you will also gain the added benefit of not having to worry much about adjusting the size of the LinearLayout etc.

Sign In or Register to comment.