Updating ExpandableListView from Database

318 views Asked by At

I want to update my expandable list view to query my database and add items after i add a user from another fragment. Basically, my user has many events and I want that user to be added to each group in the expandable list view depending on the event they have

For example: Event 1,2,3 are in explist group 1, Event 4,5,6 are in explist group 2, and Event 7,8,9 are in explist group 3. If user has events 1,2,5,9 the user should be added to all 3 groups but not repeated in group 1 of the explistview.

I have a database back end that will get all users as well as get the most recently added user. How can I insert the users into my expandable list view and basically dynamically refresh my explistview when I add or edit a user? I as of now send a confirmation message to my listview fragment from my add user fragment but my program crashes when I send the confirmation over the activity.

Code:

Add User Fragment: Adds user to database and then sends list fragment confirmation to update explistview.

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    btnAddAthlete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final Athlete athlete = new Athlete();

            athlete.setFirstName(editFirstName.getText().toString());
            athlete.setLastName(editLastName.getText().toString());
            athlete.setAge(Integer.parseInt(editAge.getText().toString()));
            athlete.setEvents(athleteEvents);
            athlete.setGrade(editGrade.getText().toString());

            db = new dbHelper(getActivity());
            db.insertAthlete(athlete);
            db.close();

            Toast.makeText(getActivity(), athlete.getFirstName() + " " + athlete.getLastName() + " added!", Toast.LENGTH_SHORT).show();

            AthletesList athletesList = new AthletesList();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.frameLayout, athletesList, "athletesList").commit();
            communicator.send("Athlete Added!");
        }
    });
}

Main activity: notifies explistview fragment with users to update. I don't know how setup fragment tags btw.

@Override
public void send(String confirmation) {
    AthletesList athletesList = (AthletesList) getSupportFragmentManager().findFragmentByTag("athleteList");
    athletesList.updateListView();
}

ExpListView Users Fragment: Contains User list into groups depending on events

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_athletes_list, container, false);
    initializeVar();
    setupToolbar();

    listViewItems = addExpListViewItems();
    adapterWhite = new ExpListViewAdapterWhite(getActivity(), listViewItems);

    expListView.setAdapter(adapterWhite);
    expListView.setOnGroupClickListener(this);
    expListView.setOnChildClickListener(this);

    return view;
}

// Adds items to explistview as of now, which is hardcoded
// Categories will be hardcoded but not children
// Prolly shouldn't even need this for children once I can just query the database
public ArrayList<listViewParent> addExpListViewItems(){
    ArrayList<listViewParent> listViewItems = new ArrayList<listViewParent>();

    for(int i = 0; i < 3; i++) {
        // Parents that hold category and holds children which are subcategories
        listViewParent category = new listViewParent();
        if(i == 0) {
            category.setTitle("4 x 400");
            category.setImage(R.drawable.icon_relay);
            category.setChildren(new ArrayList<listViewChild>());
            listViewChild child = new listViewChild();
            child.setTitle("Mike");
            child.setImage(R.drawable.icon_blank);
            category.getChildren().add(child);
        }
        listViewItems.add(i, category);
    }
    return listViewItems;
}

public void updateListView(){
     // add child to explistview and update view so screen shows new user
}
0

There are 0 answers