0

i am creating an expandable list view in android for an app I am making. What I am trying to dynamically add and remove items from an array.

this is the source code I used and am modifying.http://androidtrainningcenter.blogspot.in/2012/07/android-expandable-listview-simple.html

what i did is made a button in another class that has an "add" function. this is supposed to add to the list whatever is in a referenced textfield. However, I cannot figure out how to reference the button in the sourcecode class, so I thought i would do it the other way and reference the array in the button class.

How can I do this? I know that to reference a method from another class I would do:

Methodclass test = new Methodclass();

then use:

 test.getGroupData();

however, I am not sure how to do this with an array and call the .add()method on it.

EDIT

here is the code I want to use:

    MainActivity.java
package info.androidhive.expandablelistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

public class MainActivity extends Activity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

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

        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);
    }

    /*
     * Preparing the list data
     */
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add("Top 250");
        listDataHeader.add("Now Showing");
        listDataHeader.add("Coming Soon..");

        // Adding child data
        List<String> top250 = new ArrayList<String>();
        top250.add("The Shawshank Redemption");
        top250.add("The Godfather");
        top250.add("The Godfather: Part II");
        top250.add("Pulp Fiction");
        top250.add("The Good, the Bad and the Ugly");
        top250.add("The Dark Knight");
        top250.add("12 Angry Men");

        List<String> nowShowing = new ArrayList<String>();
        nowShowing.add("The Conjuring");
        nowShowing.add("Despicable Me 2");
        nowShowing.add("Turbo");
        nowShowing.add("Grown Ups 2");
        nowShowing.add("Red 2");
        nowShowing.add("The Wolverine");

        List<String> comingSoon = new ArrayList<String>();
        comingSoon.add("2 Guns");
        comingSoon.add("The Smurfs 2");
        comingSoon.add("The Spectacular Now");
        comingSoon.add("The Canyons");
        comingSoon.add("Europa Report");

        listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
        listDataChild.put(listDataHeader.get(1), nowShowing);
        listDataChild.put(listDataHeader.get(2), comingSoon);
    }
}

how can I change all that hard-coded material and into a dynamic input? as it, what can I use add to the parent list when the user wants, and if the user wants to add to the child list of a specific parent, how do I do that?

Currently, my code has the button for adding and the edittext code in a separate class.  What I want to do is make is so that when the ADD button is pressed, it will add something to the parent list, and if I click on an element in the parent list, i get that index and add a child element to that specific parent's child-list.

how can I do this?  so basically, i want to do something like this:

remove all of this:

array.add() //hardcoded
and add a more dynamic user dependant input.

and make is something similar to this:

     test = edittext to string
     **pressed add putton**
     test is added to the list.
    **click on test in the list**
     **select add item**
     add item to the test childlist

and this should be able to work f

or any number of inputs.

please let me know how to do this as I have tried everything i can think of. Keep in mind that my add button and edit text are in a separate class. My parent and child lists are in one class, but the button and edit-text are in another. am I going to have to move it?

5
  • 1
    With Methodclass test = new Methodclass(); test.getGroupData() you'd get the group data of a newly-created Methodclass, which presumably has no group data yet since it was just created. Commented Jan 13, 2015 at 1:18
  • I'm not sure exactly what you're asking... Where is this array declared? Can you post the code for it? By the looks of it, the array you're wanting to add to is not public. This means you can't modify it directly from out of the class, you need to used getter and setter methods. Get the arrayList (effectively copying it into your current context), add to it, then use the set method to save it. Commented Jan 13, 2015 at 1:19
  • 1
    Call the .add() method on it? There's no add method on an array. Commented Jan 13, 2015 at 3:23
  • I added some code to the question and made it more specific. will you be able to help me with this? Commented Jan 13, 2015 at 6:53
  • Be sure to differentiate between an array, a list, and an arraylist. At times they are similar, but they are certainly not the same. Commented Jan 13, 2015 at 17:57

1 Answer 1

1

First off, you need to style your questions better. It's currently horrible to read. You won't get many answers (and you're lucky not to get heavily downvoted) for such bad layout.

However, I think I can help.

Be sure to always name your variables well. It helps a lot later on when you're reading through your old code.

Also, have 2 buttons: one to add header (addHeader), one to add child (addChild) under a header item. Display the header items in a list view of some sort.

On the click event of the addHeader button

String headerItemToAdd = >>>get from text field
listDataHeader.add(headerItemToAdd);

That should be about all you need to add to the header. The next one's a bit more tricky.

On the click event of the addChild button

String headerItem = >>>get the selected text from the list view, try getSelectedItem() or something.
List<String> childList = listDataChild(headerItem);
String childItemToAdd = >>>get from text field
childList.add(childItemToAdd);//at this point the item is NOT added into listDataChild. You only have a copy of the array with the extra element in it. We need to put this updated copy back into listDataChild
listDataChild.put(headerItem , childList);

And that should be the bulk of the code. So to recap we have:

  • 2 text edit fields: One for headers, one for children
  • 1 list view of the headers (or a similar control)
  • 2 add buttons (addHeader and addChild)

And in the onClick methods of the buttons you have code similar to above.

PS: The code above is incomplete. It is pseudocode to give you an idea of how you can do this. You will need a bit of error checking etc, and fill in the parts that I haven't. If there are parts in this you're unclear about (eg, buttons and onClick methods) you should google a basic tutorial for implementing a button in android.

Good luck! Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for the information. this really helps me get the basics down. I will figure out the rest as I go
I was trying to implement this, and I realized that in the second line of the addChild code you gave, there is a problem I cannot figure out. In the second line, you said: listDataChild(headerItem). However the listDataChild is a HashMap and needs 2 arguments. waas this some oversight or is there something I am missing?
It was a mistake by me. You should be wanting listDataChild.get(headerItem) I think
Think about what's being done in method. The childList is just a copy of the existing list. You then add an item to the copy. Then you put that copy back into listDataChild, overwriting the old value. So you now have that extra element on that list. Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.