嗨,我想将不同组的视图膨胀到各自的组 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);}