android - 循环中的多个膨胀 View

标签 android listview view inflate

我想显示多个 ListView View ,每个 View 在 LinearLayout 中都有相应的标题。

我有一个 LinearLayout,我膨胀一个 View 以填充标题和 ListView,然后将该 View 循环添加到 LinearLayout,但只出现一个标题和列表(第一个)。

我希望包含标题和 ListView 的 View 出现的次数与 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 设置为水平(默认),因此您的所有标题/ ListView 实际上都已添加,但您看不到他们因为他们不在身边。通过添加

来解决这个问题
android:orientation="vertical"

到您的 LinearLayout XML 标记。

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

关于android - 循环中的多个膨胀 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433451/

相关文章:

java - OnActivityResult() Intent 数据无缘无故总是 null

.net - 更改 ImageList 的 ImageSize 属性显示空白图像

android - 在 ArrayAdapter 中覆盖 getDropDownView 时出现样式问题

java - 使 LinearLayout 与 ListView 可滚动

java - 来自 Parse 数据的带有 ListView 的 fragment 中的 RatingBar

swift : OverlayView that appears at the bottom of the screen

ios - Xcode 内容从与导航栏相同的位置开始

android - 向服务器发送数据时出现NullPointerException和数据重复

android - Android Instrumented Test有时会起作用,有时会无法生成覆盖率

Android Activity 状态丢失