0

嗨,我想将不同组的视图膨胀到各自的组 TableLayout。

问题:

每个视图都会膨胀从数据库中提取的数据,其中每一行数据都得到一个“分组”。如果视图的分组相同,则以下代码可以正常工作,即所有数据都属于相同的分组,因此都膨胀到相同的 TableLayout。

然而,IndexOutOfBoundsException出现的数据属于不同的组。例如,当有 3 个数据组是家务,而 1 个数据组是运动时。然后它与以下 logcat 一起挂起。

我怀疑它在执行时出错addview。例如,数据分组顺序是“家务”-->“家务”-->“家务”-->“运动”,对于j=0,1,2它可以正确地膨胀到行索引0,1的家务表布局,2。然而,当 j=3 时,数据索引为 3,但 Sports TableLayout 的索引(?)为 0,因此导致错误?如果那样的话,它怎么能修改呢?还是因为其他原因?谢谢!!!

07-16 23:52:20.308: E/AndroidRuntime(879): Caused by: java.lang.IndexOutOfBoundsException: index=3 count=0
07-16 23:52:20.308: E/AndroidRuntime(879):  at android.view.ViewGroup.addInArray(ViewGroup.java:3703)
07-16 23:52:20.308: E/AndroidRuntime(879):  at android.view.ViewGroup.addViewInner(ViewGroup.java:3642)
07-16 23:52:20.308: E/AndroidRuntime(879):  at android.view.ViewGroup.addView(ViewGroup.java:3491)
07-16 23:52:20.308: E/AndroidRuntime(879):  at android.widget.TableLayout.addView(TableLayout.java:425)
07-16 23:52:20.308: E/AndroidRuntime(879):  at android.view.ViewGroup.addView(ViewGroup.java:3436)
07-16 23:52:20.308: E/AndroidRuntime(879):  at android.widget.TableLayout.addView(TableLayout.java:407)
07-16 23:52:20.308: E/AndroidRuntime(879):  at com.abc. abc.Exercises_MainActivity.Inflate_All_Ex_Data(Exercises_MainActivity.java:185)
07-16 23:52:20.308: E/AndroidRuntime(879):  at com. abc. abc.Exercises_MainActivity.onCreate(Exercises_MainActivity.java:127)
07-16 23:52:20.308: E/AndroidRuntime(879):  at android.app.Activity.performCreate(Activity.java:5206)
07-16 23:52:20.308: E/AndroidRuntime(879):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-16 23:52:20.308: E/AndroidRuntime(879):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)

主要活动

//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();

        if (group.contains("Housework")) {ex_housework_List.addView(newTagView, index);}
        if (group.contains("Game")) {ex_games_List.addView(newTagView, index);}
        if (group.contains("Sport")) {ex_sports_List.addView(newTagView, index);} //LINE185
        if (group.contains("Other")) {ex_others_List.addView(newTagView, index);}
4

2 回答 2

3

假设对于索引 0,1,2,您将视图添加到 ex_housework_List 工作正常。对于索引 3,您在索引 3 处将视图添加到 ex_sports_List,而尚未向其中添加其他视图,因此出现 IndexOutOfBoundsException。

我认为您应该尝试在索引 0 处添加视图,或者如果这不是您想要的,而不是通过索引尝试为它们中的每一个设置一个计数器(每次添加视图时从 0 和 +1 开始)并使用索引的计数器。

于 2013-07-16T17:15:20.030 回答
0

对于那些有兴趣的人:

首先声明:

    int HW =0; 
    int SP =0; 
    int GM =0; 
    int OT =0;

然后

        if (group.contains("Housework")) 
        {                               
            ex_housework_List.addView(newTagView, HW);
            HW++;
        }
        if (group.contains("Game")) 
        {
            ex_games_List.addView(newTagView, GM);
            GM++;
        }
        if (group.contains("Sport")) 
        {
            ex_sports_List.addView(newTagView, SP);
            SP++;
        }
        if (group.contains("Other")) 
        {
            ex_others_List.addView(newTagView, OT);
            OT++;
        }
于 2013-07-16T17:38:27.423 回答