您好我想将不同群组的观点扩展到各自的群组TableLayout。
每个视图都会从数据库中提取数据提取,其中每行数据都有一个“分组”。如果视图的分组相同,则以下代码可以正常工作,即所有数据都具有相同的分组,因此全部膨胀到相同的TableLayout。
然而IndexOutOfBoundsException
出现的数据属于不同的群体。例如,当有3个数据时,他们的组是家务,而1个数据组是体育。然后挂起以下logcat。
我怀疑在执行addview
时出错了。例如,数据分组序列是“家务” - > “家务” - > “家务” - > “sports”,对于j = 0,1,2,它可以适当地扩展到Housework TableLayout,用于行索引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);}
答案 0 :(得分:2)
假设对于索引0,1,2,您可以向ex_housework_List添加视图,这样可以正常工作。对于索引3,您在索引3处向ex_sports_List添加视图,而没有添加其他视图,因此IndexOutOfBoundsException。
我认为你应该尝试在索引0处添加View,或者如果这不是你想要的,而不是传递索引尝试为每一个都有一个计数器(每次添加一个视图时从0和+1开始并使用计数器作为索引。
答案 1 :(得分: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++;
}