Android:没有向TableLayout充气的观点?

时间:2013-07-16 14:51:20

标签: android view inflate

我有一个活动会从数据库中加载数据,这些活动会被放入每组数据的行中。每组数据都有一个类别,即家务,游戏,体育或其他。

有4个TableLayout,我希望使用比较组名的“if”条件将数据集扩展到合适的TableLayout。

问题:

没有任何东西膨胀到任何TableLayout。 然而Toast正确显示每次充气,输出例如XHouseworksX显示没有额外的空间。为什么没有行膨胀???

更多信息:

如果我删除了所有的if子句,并且只是膨胀到任何一个TableLayout,那么视图可以正确地膨胀到那个!

详细编码如下:

MainActivity

//declaration in OnCreate

    table_list = (LinearLayout) findViewById(R.id.table_list);
    ex_housework_List = (TableLayout) findViewById(R.id.ex_housework_List);
    ex_games_List = (TableLayout) findViewById(R.id.ex_games_List);
    ex_sports_List = (TableLayout) findViewById(R.id.ex_sports_List);
    ex_others_List = (TableLayout) findViewById(R.id.ex_others_List);

    exercises = Ex_dbHlp.get_All_Ex_Data();
    int i = exercises.size();       
    for (int j = 0; j < i; j++) 
    {
        Inflate_All_Ex_Data(j); //to inflate rows to the suitable TableLayout
    }



private void Inflate_All_Ex_Data (int index) 
{   
    if(exercises.size() > 0)
    {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View newTagView = inflater.inflate(R.layout.exercises_info, null);

        Button exercise_id = (Button) newTagView.findViewById(R.id.exercise_id);
        Button exercise_group = (Button) newTagView.findViewById(R.id.exercise_group);
        Button exercise_name = (Button) newTagView.findViewById(R.id.exercise_name);
        Button exercise_count = (Button) newTagView.findViewById(R.id.exercise_count);

        exercise_id.setText(exercises.get(index).getId());
        exercise_group.setText(exercises.get(index).get_ex_group());
        exercise_name.setText(exercises.get(index).get_ex_name());
        exercise_count.setText(exercises.get(index).get_ex_count());    

        String group = exercise_group.getText().toString();
        Toast.makeText(getBaseContext(), "X"+group+"X", Toast.LENGTH_SHORT).show();

        if (group == "Houseworks") {ex_housework_List.addView(newTagView, index);}
        if (group == "Games") {ex_games_List.addView(newTagView, index);}
        if (group == "Sports") {ex_sports_List.addView(newTagView, index);}
        if (group == "Others") {ex_others_List.addView(newTagView, index);}

XML

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/Table2"
        android:layout_below="@+id/view1" >

        <LinearLayout
            android:id="@+id/table_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/add"
            android:layout_margin="2dp"
            android:orientation="vertical" >

            <TableLayout
                android:id="@+id/ex_housework_List"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/blue_btn" />

            <TableLayout
                android:id="@+id/ex_games_List"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/green_btn" />

            <TableLayout
                android:id="@+id/ex_sports_List"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/yellow_btn" />

            <TableLayout
                android:id="@+id/ex_others_List"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/pink_btn" />

        </LinearLayout>
    </ScrollView>   

1 个答案:

答案 0 :(得分:2)

if (group == "Houseworks")

导致问题。相反,它应该改为

group.equals("Houseworks");

其他组相同