2012-11-17 62 views
3

我想要顯示幾個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; 
    } 

回答

3

從你給的信息,我猜你LinearLayout設置爲橫向(默認值),所以所有的標題/列表視圖,實際上被添加,但你不能看到他們,因爲他們離開了一邊。通過添加

android:orientation="vertical" 

到您的LinearLayout XML標記。

如果這不是問題,請發佈相關的XML佈局。

+0

謝謝!真不可思議! Mi代碼工作得很好。這是問題!該XML沒有定位。我把它和所有工作按需要。 –