循环中的多个充气视图

问题描述:

我想要显示几个ListView视图,每个视图的对应标题为LinearLayout循环中的多个充气视图

我有一个LinearLayout,我膨胀为了填补标题和ListView并添加视图到LinearLayout在一个循环,但只出现一个标题和目录(第一个)。

我希望包含标题和列表视图的视图出现与for循环重复的次数。

让我告诉你的代码:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

//fragment_history: a layout with a LinearLayout inside called "main" 

     View fragmentHistory = inflater.inflate(R.layout.fragment_history, container, false); 

//I want to show a date, and all the transactions accomplished that date. With this two arrays, I pretend to loop by day, show the date as title and show the transactions of that day in the listview. 

     allExpenses = dataExpenses.getAllExpenses(); 
     allDatesExpenses = dataExpenses.getAllDates(); 

// The LinearLayout inside the fragmentHistory layout 

     LinearLayout mainLayout = (LinearLayout) fragmentHistory.findViewById(R.id.main);  
     LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

// I made a for loop, and I have two different dates. Two transactions in the first date, and one in the second day. But only the two transactions are been shown. 

     for(int x = 0, y = allDatesExpenses.size(); x < y; x++){ 

      View view = layoutInflater.inflate(R.layout.layout_title, null);   

      Expense expense = allDatesExpenses.get(x); 

      TextView text = Font.useHandelGotic(this.context, (TextView) view.findViewById(R.id.textViewTitleDate)) ; 
    text.setText(expense.getDateExpense()); 

      ListView listview = (ListView) view.findViewById(android.R.id.list); 

      ArrayList<Expense> expensesByDate = (ArrayList<Expense>) dataExpenses.getExpensesByDate(expense.getDateExpense()); 
      listview.setDivider(null); 
      listview.setAdapter(new AdapterTransaction(this.context, expensesByDate)); 

      mainLayout.addView(view); 

     }  
     return fragmentHistory; 
    } 

从你给的信息,我猜你LinearLayout设置为横向(默认值),所以所有的标题/列表视图,实际上被添加,但你不能看到他们,因为他们离开了一边。通过添加

android:orientation="vertical" 

到您的LinearLayout XML标记。

如果这不是问题,请发布相关的XML布局。

+0

谢谢!真不可思议! Mi代码工作得很好。这是问题!该XML没有定位。我把它和所有工作按需要。 –