1

I found this multilevel expandable listview which is very accurate with my project, but I also want to add a different toast to every third level (which is the last layer of view) child items. I tried this:

expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

             
                Toast.makeText(thirdLevelq1.put(q1[0] this, "The first child" , Toast.LENGTH_LONG).show();
                return false;

            }
        });  

But it gives error. I even tried to use same toast text to every childItem, app installed but crashed when I opened the adapter. So, how to add them properly?

Java:

package com.bacon.expandablelistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;

    String[] parent = new String[]{"What is View?", "What is  Layout?", "What is Dynamic Views?"};
    String[] q1 = new String[]{"List View", "Grid View"};
    String[] q2 = new String[]{"Linear Layout", "Relative Layout"};
    String[] q3 = new String[]{"Recycle View"};
    String[] des1 = new String[]{"A layout that organizes its children into a single horizontal or vertical row. It creates a scrollbar if the length of the window exceeds the length of the screen."};
    String[] des2 = new String[]{"Enables you to specify the location of child objects relative to each other (child A to the left of child B) or to the parent (aligned to the top of the parent)."};
    String[] des3 = new String[]{"This list contains linear layout information"};
    String[] des4 = new String[]{"This list contains relative layout information,Displays a scrolling grid of columns and rows"};
    String[] des5 = new String[]{"Under the RecyclerView model, several different components work together to display your data. Some of these components can be used in their unmodified form; for example, your app is likely to use the RecyclerView class directly. In other cases, we provide an abstract class, and your app is expected to extend it; for example, every app that uses RecyclerView needs to define its own view holder, which it does by extending the abstract RecyclerView.ViewHolder class."};

    LinkedHashMap<String, String[]> thirdLevelq1 = new LinkedHashMap<>();
    LinkedHashMap<String, String[]> thirdLevelq2 = new LinkedHashMap<>();
    LinkedHashMap<String, String[]> thirdLevelq3 = new LinkedHashMap<>();
    /**
     * Second level array list
     */
    List<String[]> secondLevel = new ArrayList<>();
    /**
     * Inner level data
     */
    List<LinkedHashMap<String, String[]>> data = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//The problem starts here

expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {


                Toast.makeText(getApplicationContext(), "The first child", Toast.LENGTH_LONG).show();
                return false;

            }
        });


        setUpAdapter();
    }

    private void setUpAdapter() {
        secondLevel.add(q1);
        secondLevel.add(q2);
        secondLevel.add(q3);
        thirdLevelq1.put(q1[0], des1);
        thirdLevelq1.put(q1[1], des2);
        thirdLevelq2.put(q2[0], des3);
        thirdLevelq2.put(q2[1], des4);
        thirdLevelq3.put(q3[0], des5);

        data.add(thirdLevelq1);
        data.add(thirdLevelq2);
        data.add(thirdLevelq3);
        expandableListView = (ExpandableListView) findViewById(R.id.expandible_listview);
        //passing three level of information to constructor
        ThreeLevelListAdapter threeLevelListAdapterAdapter = new ThreeLevelListAdapter(this, parent, secondLevel, data);
        expandableListView.setAdapter(threeLevelListAdapterAdapter);
        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            int previousGroup = -1;

            @Override
            public void onGroupExpand(int groupPosition) {
                if (groupPosition != previousGroup)
                    expandableListView.collapseGroup(previousGroup);
                previousGroup = groupPosition;
            }
        });


    }
}

Thanks in advance.

Edit: When I use Toast.makeText(getApplicationContext(), "The first child", Toast.LENGTH_LONG).show(); its crash logs says:

  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setOnChildClickListener(android.widget.ExpandableListView$OnChildClickListener)' on a null object reference
Cingen
  • 59
  • 5
  • any crash logs ? – Dinkar Kumar Apr 28 '21 at 15:21
  • `Toast.makeText(thirdLevelq1.put(q1[0] this, "The first child" , Toast.LENGTH_LONG).show();` this is syntactically wrong – Dinkar Kumar Apr 28 '21 at 15:28
  • I know it is wrong, I mean I couldn't add a proper subject to the title. I also added crash logs for using `Toast.makeText(getApplicationContext(), "The first child", Toast.LENGTH_LONG).show();` instead. – Cingen Apr 28 '21 at 15:33
  • Can you show where exactly the `expandableListView.setOnChildClickListener` part is written in the MainAcitvity? – Dinkar Kumar Apr 28 '21 at 15:46
  • Edited and added the text now, I just pasted the same java text from github, my list is pretty much long for here, same codes but bloated lines with lists. – Cingen Apr 28 '21 at 16:03
  • https://stackoverflow.com/questions/52309469/android-multilevel-expandable-list-view-set-third-level-child-item-clicked/52427456#52427456 – i_A_mok Apr 29 '21 at 07:12

2 Answers2

1

Please set this listener expandableListView.setOnChildClickListener after the expandableListView is initialized, that is after setUpAdapter() in your case as expandableListView is still null when you are trying to set the listener because your initialization of this is afterwards.

That is the reason for NullPointerException. because expandableListView is null while setting the listener.

Cheers

Dinkar Kumar
  • 2,175
  • 2
  • 12
  • 22
  • So how can add multiple toast texts into `expandableListView.setOnChildClickListener` and direct them individual childItems? – Cingen Apr 28 '21 at 21:47
  • 1
    You are getting group position and child position in the callback use that to know which child or group has been called. – Dinkar Kumar Apr 29 '21 at 01:25
  • Could you please modelize it? I cannot do that on my own. – Cingen Apr 29 '21 at 23:09
  • 1
    @Cingen You have to share more of your code, so I can help you out, writing everything on assumption will be too cumbersome for me. – Dinkar Kumar Apr 30 '21 at 08:55
  • 1
    you can try https://stackoverflow.com/questions/52309469/android-multilevel-expandable-list-view-set-third-level-child-item-clicked/52427456#52427456. thanks to @i_A_mok . Cingen if you are still not able to achieve what you want, let me know I will try to help you out. – Dinkar Kumar Apr 30 '21 at 09:02
  • Thanks for your attention, it works but I want to add different toasts, like you click `(group 1, Child Level 1, A)` and toasts says "Shakespear is a good poet", and clik `(group 1, Child Level 1, B)` and it says "Earth isn't flat" or something :) I mean not like `plpos + ", " + slpos + ", " + tlpos,` but an independent text. – Cingen May 02 '21 at 10:41
  • 1
    Yes, you can have different toast as per your need, you just have to leverage the groupPosition and childPosition of the callback to get the correct message and use that in your Toast. – Dinkar Kumar May 02 '21 at 16:38
  • You say well but I wish I could write it as a proper code :( All I can wrote it `@Override public void onFinalChildClick(int plpos, int slpos, int tlpos) { Toast toast = Toast.makeText(this, "This is meant to be the first child item", Toast.LENGTH_SHORT); toast.show(); }` When I add groupPosition and childPosition it gives error. – Cingen May 02 '21 at 18:26
  • 1
    Can you please show the data you will be having in all three List, I need to visualize it to give you the exact answer – Dinkar Kumar May 02 '21 at 18:57
  • My MainActivity and ThreeLevelListAdapter are same as [the link you gave](https://stackoverflow.com/questions/52309469/android-multilevel-expandable-list-view-set-third-level-child-item-clicked/52427456#52427456) now, the other adapters are also same as [this github link](https://github.com/ganny26/MultiExpandableListView/tree/master/app). Copy/pasting here looks bloated I guess. I would really appreciated if you solve my problem, you have already helped much. – Cingen May 03 '21 at 01:36
  • 1
    It would be easier for me and faster for you if you could share your code base link of github, – Dinkar Kumar May 03 '21 at 01:58
  • I see, uploaded here: https://github.com/Cingen/Clickable-Multi-Level-Listview-Trial It didn't allow to upload as unzipped. Thank you. – Cingen May 03 '21 at 12:14
1
@Override
public void onFinalChildClick(int plpos, int slpos, int tlpos) {
    String msg = "";
    switch (tlpos) {
        case 0:
            msg = "Shakespear is a good poet";
            break;
        case 1:
            msg = "Earth isn't flat";
            break;
        default:
            msg = "Unknown";
    }
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

@Override
public void onFinalItemClick(String plItem, String slItem, String tlItem) {
    String inMsg = plItem + ", " + slItem + ", " + tlItem;
    String outMsg = "";
    if (inMsg.equals("group 1, Child Level 1, A")){
        outMsg = "Shakespear is a good poet";
    } else if (inMsg.equals("group 1, Child Level 1, B")){
        outMsg = "Earth isn't flat";
    } else {
        outMsg = "Unknown";
    }
    Toast.makeText(this, outMsg, Toast.LENGTH_SHORT).show();
}
i_A_mok
  • 2,744
  • 2
  • 11
  • 15