سؤال

I want to create programatically my relativeLayout using LayouInflater but it throws an error on AddView method, heres is my code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  android:id="@+id/relativeAgedSummary"     
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
</RelativeLayout>

ViewGroup relativeAgedSummary=FindViewById<ViewGroup>(Resource.Id.relativeAgedSummary);
View view = this.LayoutInflater.Inflate(
                                Resource.Layout.aged_summary_list_item, 
                                relativeAgedSummary, false);    

for (int i = 0; i < agedValues.Count; i++) {
    var row = agedValues.ElementAt(i);
    if(row.Range  == 0 && row.IsTotal == false)
    {
        RelativeLayout rl = view.FindViewById<RelativeLayout>(
                                           Resource.Id.relativeLayoutAgedSummaryItem);
        rl.SetBackgroundResource(Resource.Color.lightBlue); 
        if(contA==0)
        {
           TextView tv = (TextView)view.FindViewById(Resource.Id.txtAgedSummary); 
           tv.SetText(labelsAgedSummary[row.Range], TextView.BufferType.Normal);
           contA++;
        }           
        relativeAgedSummary.AddView(view);
    }
هل كانت مفيدة؟

المحلول

What do you mean "it only appears one". If you are trying to add multiple instances to relativeAgedSummary then you will have to re-inflate the aged_summary_list_item each time:

for (int i = 0; i < agedValues.Count; i++)
{
    View view = this.LayoutInflater.Inflate(
                    Resource.Layout.aged_summary_list_item, 
                    relativeAgedSummary, false);

    var row = agedValues.ElementAt(i);

    // the logic here...

    relativeAgedSummary.AddView(view);
}

This is because you are trying to add the same object more than once. It will ignore the add. And then in the loop you modify the same item repeatedly, thus resulting in it looking like it added only the "last" item.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top