循环中存在多个膨胀视图

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;
    }
1个回答

3

根据您提供的信息,我猜测您的LinearLayout设置为水平方向(默认设置),因此所有标题/列表视图实际上都已添加,但您无法看到它们,因为它们在一侧。通过添加以下内容来解决这个问题:

android:orientation="vertical"

将以下代码添加到您的LinearLayout XML标记中。
如果这不是问题,请发布相关的XML布局。

谢谢!太棒了!我的代码一切正常。那就是问题所在!XML 没有方向。我加上它,所有的工作都按照需要进行了。 - Sterling Diaz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接