Two issues here. Following a tutorial and I need a little help adapting it to my project.
1)The children are the same for each group. For example the Arraylist dining contain the children of the group "Dining Commons" I need to make two more arraylists containing academics buildings and residential buildings and those need to be the children of their respective parents.
2) Is it possible to make the children item clickable? using clicklistener or something like that.
java source.
package com.bogotobogo.android.smplexpandable;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Demonstrates expandable lists backed by a Simple Map-based adapter
*/
public class SmplExpandable extends ExpandableListActivity {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
ArrayList buildings = BuildingList();
ArrayList diningCommonBuildings = DiningList();
private ExpandableListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < buildings.size(); i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, (String) buildings.get(i));
//curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> dining = new ArrayList<Map<String, String>>();
for (int j = 0; j < diningCommonBuildings.size(); j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
dining.add(curChildMap);
curChildMap.put(NAME, (String) diningCommonBuildings.get(j));
//curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
}
childData.add(dining);
}
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
setListAdapter(mAdapter);
}
private ArrayList BuildingList() {
ArrayList buildings = new ArrayList();
buildings.add("Academic Buildings");
buildings.add("Dining Commons");
buildings.add("Residential Buildings");
return buildings;
}
private ArrayList DiningList() {
ArrayList dining = new ArrayList();
dining.add("Berkshire");
dining.add("Franklin");
dining.add("Hampden");
dining.add("Hampshire");
dining.add("Worcester");
return dining;
}
}
Check out this example,
public class MainExpand extends ExpandableListActivity {
private ExpandableListAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, String>> parent = new ArrayList<Map<String,String>>();
List<List<Map<String, String>>> childGroup = new ArrayList<List<Map<String,String>>>();
for (int i = 0; i < 5; i++) {
Map<String, String> parentMap = new HashMap<String, String>();
parentMap.put("lalit", "Parent "+i);
parent.add(parentMap);
List<Map<String, String>> child = new ArrayList<Map<String,String>>();
for (int j = 0; j < 2; j++) {
Map<String, String> childMap = new HashMap<String, String>();
childMap.put("lalit", "Child "+j);
child.add(childMap);
}
childGroup.add(child);
}
adapter = new SimpleExpandableListAdapter(
this,
parent,android.R.layout.simple_expandable_list_item_1,new String[] {"lalit"},new int[] { android.R.id.text1, android.R.id.text2},
childGroup,android.R.layout.simple_expandable_list_item_2,new String[]{"lalit","even"}, new int[]{android.R.id.text1,android.R.id.text2}
);
setListAdapter(adapter);
}
I used a combination of both of your answers. Thanks. I took advatange of the multidimensional array.
First i created these arrays.
private String[] buildingTypes = {
"Academic Buildings", "Dining Commons", "Residential Halls" };
private String[][] buildingList = {
{ "Agriculutural Engineering", "Army ROTC", "Arnold House", "Studio Arts Bldg","Bartlett" },
{ "Berkshire", "Franklin", "Hampden", "Hampshire", "Worcester" },
{ "Baker Hall", "Brett Hall", "Brooks Hall", "Brown Hall","Butterfield Hall" },
Then I modified the for loops
for (int i = 0; i < buildingTypes.length; i++) {
Map<String, String> parentMap = new HashMap<String, String>();
parentMap.put("lalit", buildingTypes[i]);
parent.add(parentMap);
List<Map<String, String>> child = new ArrayList<Map<String,String>>();
for (int j = 0; j < rows; j++) {
Map<String, String> childMap = new HashMap<String, String>();
childMap.put("lalit", buildingList[i][j]);
child.add(childMap);
}
childGroup.add(child);
}
Here are the answer to both of your questions. If something is not OK for you, just tell me in comments.
Answer of part 1):
Please look here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html
private String[][] children = {
{ "Academic1", "Academic2", "Academic3", "Academic4" },
{ "Dining1", "Dining2", "Dining2", "Dining3" },
{ "Residential1", "Residential2" },
};
Answer of part 2):
http://developer.android.com/reference/android/widget/ExpandableListView.OnChildClickListener.html
onChildClick(ExpandableListView parent, View v, int groupPosition, int
childPosition, long id) Callback method to be invoked when a child in
this expandable list has been clicked.
Related
I have the listview from the exel:
try {
Workbook wb = WorkbookFactory.create(getAssets().open("bos.xls"));
Sheet sheet = wb.getSheetAt(0);
for(int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
Row row = sheet.getRow(i);
Cell form1 = row.getCell(0);
Cell form2 = row.getCell(1);
Cell form3 = row.getCell(2);
IrrList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> hm;
hm = new HashMap<String, Object>();
hm.put(Form1, form1.getStringCellValue());
hm.put(Form2, form2.getStringCellValue());
hm.put(Form3, form3.getStringCellValue());
IrrList.add(hm);
SimpleAdapter adapter = new SimpleAdapter(this, IrrList,
R.layout.list_item, new String[] { Form1, Form2, Form3 },
new int[] { R.id.text1, R.id.text2, R.id.text3 });
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
}
} catch(Exception ex) {
return;
}
When I use it I have the one value in my listview.
How can i correct it?
I tried to add this before "HashMap hm;":
for(int i1 = 0, l = IrrList.size(); i1 < l; i1++){
"I dont know what write here"
}
Try this..
Initilize the ArrayList before the for loop and set the listview adapter after the for loop
IrrList = new ArrayList<HashMap<String, Object>>();
for(int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
Row row = sheet.getRow(i);
Cell form1 = row.getCell(0);
Cell form2 = row.getCell(1);
Cell form3 = row.getCell(2);
HashMap<String, Object> hm;
hm = new HashMap<String, Object>();
hm.put(Form1, form1.getStringCellValue());
hm.put(Form2, form2.getStringCellValue());
hm.put(Form3, form3.getStringCellValue());
IrrList.add(hm);
}
SimpleAdapter adapter = new SimpleAdapter(this, IrrList,
R.layout.list_item, new String[] { Form1, Form2, Form3 },
new int[] { R.id.text1, R.id.text2, R.id.text3 });
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
At the following code i am loading an ArrayList with data one by one.
This ArrayList is used in an ExpandableListView. So it contains groups and childs.
Every group has a Name and a Description.
Every child has a Name.
Some groups do not have childern.
public class CategoriesExpandableList extends Activity implements OnChildClickListener,
OnGroupClickListener, OnGroupExpandListener, OnGroupCollapseListener {
private static Context mContext;
private ExpandListAdapter ExpAdapter=null;
private ArrayList<ExpandListGroup> ExpListItems;
ArrayList<ExpandListGroup> list=null;
private ExpandableListView ExpandList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.categoriesexpandable);
ExpandList = (ExpandableListView) findViewById(R.id.ExpList);
ExpListItems = SetStandardGroups();
ExpAdapter = new ExpandListAdapter(CategoriesExpandableList.this, ExpListItems);
ExpandList.setAdapter(ExpAdapter);
ExpandList.setOnGroupClickListener(this);
ExpandList.setOnChildClickListener(this);
mContext=this;
}
public ArrayList<ExpandListGroup> SetStandardGroups() {
ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>();
ExpandListGroup gru0 = new ExpandListGroup();
gru0.setName("My Favorites");
gru0.setDescription("My favourite points of interest");
gru0.setItems(list2);
list2 = new ArrayList<ExpandListChild>();
ExpandListGroup gru1 = new ExpandListGroup();
gru1.setName("Essentials");
gru1.setDescription("Very essential points of interest");
gru1.setItems(list2);
list2 = new ArrayList<ExpandListChild>();
ExpandListGroup gru2 = new ExpandListGroup();
gru2.setName("Sights");
gru2.setDescription("Beautiful sights you have to visit");
gru2.setItems(list2);
list2 = new ArrayList<ExpandListChild>();
ExpandListGroup gru3 = new ExpandListGroup();
gru3.setName("Beaches");
gru3.setDescription("Beaches near Agios Nikolaos");
ExpandListChild ch3_0 = new ExpandListChild();
ch3_0.setName("In town");
ch3_0.setTag(null);
list2.add(ch3_0);
ExpandListChild ch3_1 = new ExpandListChild();
ch3_1.setName("Outside town");
ch3_1.setTag(null);
list2.add(ch3_1);
gru3.setItems(list2);
list2 = new ArrayList<ExpandListChild>();
list.add(gru0);
list.add(gru1);
list.add(gru2);
list.add(gru3);
return list;
}
}
I want to know if there is a quick way to load these data at the ArrayList. For example by calling a method like this:
addPoint(String groupname, String groupdescription, String child01)
addPoint("Banks", "Banks and ATMs", "Only banks")
addPoint("Banks", "Banks and ATMs", "Only ATMs")
// Here i have only a group and no children...
addPoint("Sights", "Beautiful sights to visit", "")
public ArrayList<ExpandListGroup> SetStandardGroups() {
ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>();
String[][] expandablelist={ {"My Favorites", "My favourite points of interest","","","","","","","","","",""},
{"Essentials", "Very essential points of interest","","","","","","","","","",""},
{"Sights", "Beautiful sights you have to visit","","","","","","","","","",""},
{"Beaches", "Nice beaches","In town","Around town","","","","","","","",""},
{"Banks", "Banks and ATMs","","","","","","","","","",""},
{"Pharmacies", "All pharmacies near you","","","","","","","","","",""},
{"Doctors", "Doctors in case of emergency","Pediatricians","Pathologists","General doctors","Eye doctors","Surgeons","Otorhinolayngologists","Orthopaedists","Gynecologists","Dermatologists","Cardiologists"}
};
for(int i=0; i<=6; i++) {
System.out.println("i= "+i);
ExpandListGroup gru = new ExpandListGroup();
gru.setName(expandablelist[i][0]);
gru.setDescription(expandablelist[i][1]);
for(int j=2; j<=11; j++) {
System.out.println("j= "+j);
System.out.println("length= "+expandablelist[i][j].length());
if(expandablelist[i][j].length()!=0) {
ExpandListChild chld = new ExpandListChild();
chld.setName(expandablelist[i][j]);
chld.setTag(null);
list2.add(chld);
}
}
if(list2!=null) {
gru.setItems(list2);
}
list2 = new ArrayList<ExpandListChild>();
list.add(gru);
}
}
I have founnd a way to load my ArrayList from an Array like you see at the code above.
Thank you for reading about my problem.
I am sure there is a better solution and if i have some time i will try to figure it out.
package com.coltonnicotera.londontransitguide;
//bunch of imports
public class SchedulesActivity extends Activity {
/** Called when the activity is first created. */
List<Map<String, String>> routesList = new ArrayList<Map<String,String>>();
String[] routesArray = new String[] {"01. Kipps Lane/Thompson Road", "02. Dundas", "03. Hamilton Rd", "04. Oxford East", "05. Springbank", "06. Richmond", "07. Wavell", "08. Riverside", "09. Whitehills", "10. Wonderland", "11. Southcrest", "12. Warncliffe South", "13. Wellington Road", "14. Highbury", "15. Westmount", "16. Adelaide", "17. Oxford West", "19. Oakridge", "20. Cherryhill", "21. Huron Heights", "22. Trafalgar", "23. Berkshire", "24. Baseline", "25. Kilally", "26. Jalna Blvd. West", "27. Fanshawe College", "28. Lambeth", "30. Newbold", "31. Orchard Park", "32. Windermere", "33. Proudfoot", "34. Medway", "35. Argyle", "36. Airport/Industrial", "37. Sovereign Road", "38. Stoney Creek", "39. Fanshawe West"};
public void onCreate(Bundle savedInstanceState) {
initList();
ListView lv = (ListView) findViewById(R.id.listView);
SimpleAdapter simpleAdpt = new SimpleAdapter(this, routesList, android.R.layout.simple_list_item_1, new String[] {"route"}, new int[] {android.R.id.text1});
/*LINE 63 */ lv.setAdapter(simpleAdpt);
}
private void initList() {
for (int i = 0; i<=36; i++)
routesList.add(createRoute("route", routesArray[i]));
}
private HashMap<String, String> createRoute(String key, String name) {
HashMap<String, String> route = new HashMap<String, String>();
route.put(key, name);
return route;
}
}
My problem is that on line 63 there is a NullPointerException. I'm not sure what exactly is null here. I've removed all the imports for simplicity. R.id.listView also exists.
You have not set your layout that why listview findViewbyId returns null...so what you need to do is In oncreate() ,set contentView before actually getting your listview ...like this setContentView(R.layout.yourlayout)
Below is Simple declaration in this a child and parent is being build with dynamic list value.With relevant code as createGroupList() and createChildList(). I am able to manipulate with TextView but one of field is my dynamic image from getting to JSON not is being load in WebView.
expListAdapter = new SimpleExpandableListAdapter(
this, createGroupList(), // Creating group List.
R.layout.group_row,
new String[] { "Group Item" },
new int[] { R.id.`enter code here`row_name },
createChildList(), // Creating Child List.
R.layout.child_row,
new String[] { "Sub Item0", "Sub Item1", "Sub Item2", "Sub Item3" },
new int[] { R.id.grpChildName, R.id.grpChildAddress, R.id.grpChildPhoneNo, R.id.childImage }
);
setListAdapter(expListAdapter);
private List createGroupList() {
String header = "";
int headerSize = 0;
ArrayList result = new ArrayList();
for (int i = 0; i < dynamicList.size(); i++) {
HashMap m = new HashMap();
String taxiCompanyData[] = dynamicList.get(i);
if (header != taxiCompanyData[3]) {
m.put("Group Item", taxiCompanyData[3]);
header = taxiCompanyData[3];
headerList[headerSize] = header;
headerSize++;
result.add(m);
counter++;
}
}
return (List) result;
}
Above is Group creating and
private List createChildList() {
ArrayList result = new ArrayList();
Log.i("Info ", "headerList.length" + headerList.length);
for (int i = 0; i < headerList.length; i++) {
final ArrayList secList = new ArrayList();
System.out.println("dynamic list size is in main"
+ dynamicList.size());
webView = (WebView) findViewById(R.id.childImage);
for (int j = 0; j < dynamicList.size(); j++) {
String taxiCompanyData[] = dynamicList.get(j);
if (taxiCompanyData[3].equalsIgnoreCase(headerList[i])) {
final HashMap child = new HashMap();
child.put("Sub Item0", taxiCompanyData[0]);
child.put("Sub Item1", taxiCompanyData[1]);
child.put("Sub Item2", taxiCompanyData[2]);
// child.put("Sub Item3", taxiCompanyData[4]);
Thread th = new Thread(){
#Override
public void run() {
webView.loadUrl("https://lh5.googleusercontent.com/-u8heQyD_4y4/T5VMu-Zc6wI/AAAAAAAi71s/EP32a3D-fc0/s90/Corsino");
secList.add(child);
};
};
th.start();
}
}
result.add(secList);
}
return (List)result;
}
I am unable to load WebUI. Please help anyone.
I have an arraylist with multiple hashmaps that contains information that comes from a sql database
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
This arraylist will eventually fill my listview with items (names of people).
When I click on one item I want to send all of the content of that hashmap to another page.
For example:
John <
Now I want to send the hashmap with all the information of John to another android page.
How do I get that information out of the hashmap?
This is my code:
public class Contactenlijst extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactview);
ListView lv;
lv = (ListView) findViewById(R.id.list);
lv.setTextFilterEnabled(true);
String[] from = new String[] {"naam"};
int[] to = new int[]{R.id.naam};
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
// Get the data (see above)
JSONObject json = Database
.getJSONfromURL("http://fabian.nostradamus.nu/Android/getcontactinfo.php");
try {
JSONArray contactinfo = json.getJSONArray("contactlijst");
// Loop the Array
for (int i = 0; i < contactinfo.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = contactinfo.getJSONObject(i);
map.put("voornaam", e.getString("staff_name"));
map.put("achternaam", e.getString("staff_lastname"));
map.put("geboortedatum", e.getString("staff_dateofbirth"));
map.put("adres", e.getString("staff_address"));
map.put("postcode", e.getString("staff_address_postal"));
map.put("woonplaats", e.getString("staff_address_city"));
map.put("email", e.getString("staff_email"));
map.put("telefoon", e.getString("staff_phone"));
mylist.add(map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
//array for view
ArrayList<HashMap<String, String>> mylist2 = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < mylist.size(); i++){
HashMap<String,String> map2 = new HashMap<String, String>();
map2.put("naam", (mylist.get(i).get("voornaam")+" "+(mylist.get(i).get("achternaam"))));
mylist2.add(map2);
}
try{
lv.setAdapter(new SimpleAdapter(this, mylist2, R.layout.list_item, from, to));
}catch (Exception e){Log.d("test1","test2");}
//onclick stuur array naar contactinfo
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String text = ((TextView) view).getText().toString();
Intent i = new Intent(Contactenlijst.this, Contactinfo.class);
String uittekst[] = text.split(" ");
String voornaam = uittekst[0].toString();
String achternaam = uittekst[1].toString();
startActivity(i);
}
});
}
}
So it all has to happen under "String achternaam = uittekst[1].toString();"
for(Hashmap<String, String> map: mylist) {
for(Entry<String, String> mapEntry: map) {
String key = mapEntry.getKey();
String value = mapEntry.getValue();
}
}