Pregunta

Hi I would like to inflate views of different groups to respective's group TableLayout.

Question:

Each view would inflate extracts of data from database, of which each row of data gets a "grouping". The following codes work fine if the grouping of the views are the same, i.e. all data are of the same grouping and hence all inflated to the same TableLayout.

Yet IndexOutOfBoundsException arises the data are of different groups. For example, when there are 3 data of which their group are Housework, while 1 data group is sports. It then hangs with the following logcat.

I suspect it got error when performing addview. For example, data grouping sequence is "housework" --> "housework" --> "housework" --> "sports", for j=0,1,2 it can properly inflate to Housework TableLayout for row index 0,1,2. Yet when j=3, the data index is 3 but the index(?) for the Sports TableLayout is 0, and therefore causing error? If then how could it revised? Or is it because of another reason? Thanks!!!

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)

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

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

Solución

Let's say for index 0,1,2 you add views to ex_housework_List which works fine. For index 3 you're adding view to ex_sports_List at index 3 while no other views have been added to it yet hence the IndexOutOfBoundsException.

I think you should either try to addView at index 0 or if that's not what you want, instead of passed index try to have a counter for each one of them (starting from 0 and +1 each time a view added to them) and use the counters for index.

Otros consejos

For those who are interested:

declare at first:

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

then

        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++;
        }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top