javaandroidlistviewsubitem

ListView with one SubItem


I know it's been asked a few times. But somehow every answer is too abstract and just doesn't fit my problem.

Okay, as the question says, I need a list where each item has a subitem. I already have a list of items, but without the subitems. As you can see below, I already have two ArrayLists, the first with the listItems, and the second with the subItems. So it would be optimal to solve this problem with these two.

What I have:

listView = view.findViewById(R.id.explore_list);
        listView.setOnItemClickListener(this);

        try {
            ArrayList<String> arrayList = new ArrayList<>();
            ArrayList<String> secondArrayList = new ArrayList<>();

            for (Integer i = 0; i < DataManager.data.getJSONObject(DataManager.dataTableDefinition[0]).length(); i++) {
                arrayList.add(DataManager.data.getJSONObject(DataManager.dataTableDefinition[0]).getJSONObject(i.toString()).getString("Name"));
                secondArrayList.add(DataManager.data.getJSONObject(DataManager.dataTableDefinition[0]).getJSONObject(i.toString()).getString("Beschreibung"));
            }

            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(Objects.requireNonNull(getActivity()), android.R.layout.simple_list_item_1, arrayList);
            listView.setAdapter(arrayAdapter);
        } catch (Exception ignored){}

Note: DataManager.data.getJSONObject(DataManager.dataTableDefinition[0]).getJSONObject(i.toString()).getString("Name") and DataManager.data.getJSONObject(DataManager.dataTableDefinition[0]).getJSONObject(i.toString()).getString("Beschreibung") output a string and are not relevant here. They can therefore be ignored.


Solution

  • Do you want to create something like this?

    It's so simple, and this is my demo project for you: https://github.com/hiepxuan2008/basic-recyclerview-android . Hope it will help your issue a little bit, thanks!

    enter image description here