Pregunta

I have an activity that would load out data from database, which are put into rows for each set of data. Each set of data has one category, i.e. housework, games, sports or others.

There are 4 TableLayout, and I would like the set of data to be inflated to suitable TableLayout, using the "if" condition that compares the group name.

Question:

Nothing is inflated to any TableLayout. Yet the Toast shows correctly for each inflating, outputting e.g. XHouseworksX showing that there are no extra space. Why there is no rows being inflated???

Further information:

If I remove all the if clause, and solely inflate to any one of the TableLayout, the views can be inflated properly to that successfully!

Details coding as below:

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>   
¿Fue útil?

Solución

if (group == "Houseworks")

that causes the problem. Rather it should be changed to

group.equals("Houseworks");

Same for other groups

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top