-1

I still can't quite understand the expandable list views, after so much searching and tutorials. The majority of resources on the item cover making a custom adapter, which is not necessary for my needs. I have a set of folders in the assets/characters/ named like a-row, ka-row, a-row-katakana, ka-row-katakana etc. They get added to a TreeMap, which then get added to the list view based on their title.

The titles are formatted like so: Hiragana~A-row and are split by the ~. The first in the split, Hiragana is the category and A-row is the title for the item. However, the Katakana items (for now there are just two) incorrectly appear in the Hiragana category and when I expand the Katakana category I get the exception.

Here is a video demonstrating: https://mediacru.sh/Gb59fW-FpOJY

Finally, here is the error received after attempting to open the Katakana cateogory:

11-28 13:01:01.736  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at java.util.ArrayList.get(ArrayList.java:308)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at android.widget.SimpleExpandableListAdapter.getChildrenCount(SimpleExpandableListAdapter.java:255)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:567)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:698)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:698)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:651)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at android.widget.AbsListView$PerformClick.run(AbsListView.java:3467)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at android.widget.AbsListView$3.run(AbsListView.java:4830)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at android.os.Looper.loop(Looper.java:146)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5602)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
11-28 13:01:01.741  31037-31037/com.lemonlake.japanesehandwriting W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

And my code:

List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

List<Map<String, String>> childGroupCurrent = new ArrayList<Map<String, String>>();

for (Map.Entry<String, String> entry : mDictionaryFolders.entrySet()) {
    final String key = entry.getKey();
    final String value = entry.getValue();

    final String[] split = key.split("~");

    Map<String, String> category = new HashMap<String, String>() {{
        put("ROOT_NAME", split[0]);
    }};

    if (!groupData.contains(category)) {
        groupData.add(category);
        if (!childData.isEmpty())
            childData.add(childGroupCurrent);
        childGroupCurrent = new ArrayList<Map<String, String>>();
    }

    childGroupCurrent.add(new HashMap<String, String>() {{
        put("CHILD_NAME", split[1]);
        put("FILE_NAME", value);
    }});
}

childData.add(childGroupCurrent);

ExpandableListView mKanjiListView = (ExpandableListView) findViewById(R.id.dictionariesListView);
mKanjiListAdapter = new SimpleExpandableListAdapter(
        Main.this,
        groupData,
        R.layout.expandable_list_item_1,
        new String[]{"ROOT_NAME"},
        new int[]{android.R.id.text1},
        childData,
        R.layout.expandable_list_item_2,
        new String[]{"CHILD_NAME"},
        new int[]{android.R.id.text1}
);
mKanjiListView.setAdapter(mKanjiListAdapter);

mKanjiListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
                                int groupPosition, int childPosition, long id) {
        HashMap<String, String> child = (HashMap<String, String>) mKanjiListAdapter.getChild(groupPosition, childPosition);
        Intent intent = new Intent(Main.this, HandwritingActivity.class);
        intent.putExtra("fileToOpen", child.get("FILE_NAME") + "/chars.dat");
        startActivity(intent);
        return true;
    }
});

For reference, this is the method where the dictionaries are located: String fileList[] = getAssets().list("characters");

for (String aFileList : fileList) {
    if (!aFileList.contains(".")) {
        String fileList2[] = getAssets().list("characters/" + aFileList);

        for (String aFileList2 : fileList2) {
            if (aFileList2.toLowerCase().contains("chars.dat")) {
                System.out.println("Dictionary file: " + aFileList + "/" + aFileList2);

                String title = "";

                try {
                    InputStream in = getAssets().open("characters/" + aFileList + "/" + aFileList2);
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));

                    title = reader.readLine();

                    reader.close();
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                mDictionaryFolders.put(title, "characters/" + aFileList);
            }
        }
    }
}
Drew Lemmy
  • 447
  • 1
  • 4
  • 13
  • possible duplicate of [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – Selvin Nov 28 '14 at 13:10
  • can you please highlight code where you get such error ? – Haresh Chhelana Nov 28 '14 at 13:13
  • ... IndexOutOfBoundsException is so obvious like **2+2=4** or `new ArrayList(Arrays.asList(new String[1])).get(1)` ... it also should be obvious why you are getting this exception, even without proper part of code(as you did not provide it) ... you are spliting and assuming that split's results will be string[2] ... but what will happend if there is no `~` in string? – Selvin Nov 28 '14 at 13:14
  • Here's a few things: I know what an IndexOutOfBoundsException is. And right now it's not necessary to add a check as it is always the case that there will be one but that will be added at a later date. I'm asking this because I still can't figure out how to use the ExpandableListView due to its lack of documentation and clear examples. – Drew Lemmy Nov 28 '14 at 13:17

1 Answers1

1

Turns out I was never actually adding the childGroup.

 if (!childData.isEmpty())
            childData.add(childGroupCurrent);

Turned to:

if (!childGroupCurrent.isEmpty())
    childData.add(childGroupCurrent);

Fixed it.

Drew Lemmy
  • 447
  • 1
  • 4
  • 13