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?
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..add()method on it? There's no add method on an array.