how to implement expandalbelistview with not any child
I am new in android I want to implement and expandalbelistview in which some groups have children and some have none.
Try This
It works for me
mExpandableListView = (ExpandableListView) findViewById(R.id.grouplist);
groupCollection = new LinkedHashMap<>();
List<String> newsCategories = new ArrayList<>();
List<String> nullcats = new ArrayList<>();
nullcats.add(null);
ArrayList<Categories> categories1 = new ArrayList<>();
//dataSource is my DBOpen helper class
categories1 = dataSource.findAllCategories();
if (categories1 != null) {
for (int i = 0; i < categories1.size(); i++) {
newsCategories.add(categories1.get(i).getCatagory_name_en());
}
} else {
newsCategories.add(null);
}
groupCollection.put(groupList.get(0), nullcats);
groupCollection.put(groupList.get(1), newsCategories);
expandableListAdapter = new ExpandableListAdapter(this, groupList, groupCollection);
mExpandableListView.setAdapter(expandableListAdapter);
I have gone through Android: How to correctly use NotifyDataSetChanged with SimpleExpandableListAdapter?, and have a similar program structure but the list (of access points) appears on creation (first scan) and disappears on the next wifi scan.
public class APScanActivity extends ExpandableListActivity {
public static WifiManager mywifiManager;
private TextView PlotTitle;
boolean mDualPane;
int mCurCheckPosition = 0;
List<ScanResult> scanResults;
SimpleExpandableListAdapter expListAdapter;
ExpandableListView APList;
List GroupList = new ArrayList();
List ChildList = new ArrayList();
int scanResultsSize;
ArrayList<String> NetworkList;
Multimap<String,String> Networks;
private long[] expandedIds;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mywifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
setContentView(R.layout.activity_autoscan);
APList = getExpandableListView();
PlotTitle = (TextView) findViewById(R.id.displayMsg);
View detailsFrame = findViewById(R.id.detailFragment);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null) {
// Restore last state for checked position.
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
if (mDualPane) {
// In dual-pane mode, list view highlights selected item.
getExpandableListView().setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);
showDetails(mCurCheckPosition,null);
}
if (mywifiManager.isWifiEnabled())
PlotTitle.setText("Choose Accesspoint from Network list");
registerReceiver(wifiScanReceiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
new WifiScanner().execute();
}
/** Implement a background task for Wi-Fi scans **/
public static class WifiScanner extends AsyncTask<Void, Void, Boolean> {
protected Boolean doInBackground(Void... params) {
return mywifiManager.startScan();
}
}
private BroadcastReceiver wifiScanReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
/** Get the Wi-Fi scan results **/
scanResults = mywifiManager.getScanResults();
new WifiScanner().execute();
scanResultsSize = (scanResults == null) ? 0 : scanResults
.size();
Networks = ArrayListMultimap.create(); // This is our multimap with ssid,bssid pairs
for (int index = 0; index < scanResultsSize; index++) {
ScanResult scanResult = scanResults.get(index);
String ssid = scanResult.SSID;
String AccessPoint = scanResult.BSSID+" ("+scanResult.level+"dBm )";
Networks.put(ssid, AccessPoint);
}
NetworkList = new ArrayList<String>(Networks.keySet());
GroupList.clear();
ChildList.clear();
GroupList = createGroupList();
ChildList = createChildList();
if (expListAdapter == null){
expListAdapter = new SimpleExpandableListAdapter(
context,
GroupList, // Creating group List.
R.layout.group_row, // Group item layout XML.
new String[] { "Network" }, // the key of group item.
new int[] { R.id.row_name }, // ID of each group item.-Data under the key goes into this TextView.
ChildList, // childData describes .
R.layout.child_row, // Layout for sub-level entries(second level).
new String[] {"AccPt"}, // Keys in childData maps to.
new int[] { R.id.grp_child} // Data under the keys above go into these TextViews.
);
//setListAdapter(expListAdapter);
APList.setAdapter(expListAdapter);
}
expListAdapter.notifyDataSetChanged();
}
};
#SuppressWarnings({ "unchecked", "rawtypes" })
public List createGroupList() {
ArrayList result = new ArrayList();
for( int i = 0 ; i < NetworkList.size() ; ++i ) {
HashMap m = new HashMap();
m.put( "Network",NetworkList.get(i) ); // the key and it's value.
result.add( m );
}
return (List)result;
}
#SuppressWarnings({ "unchecked", "rawtypes" })
public List createChildList() {
ArrayList result = new ArrayList();
for( int i = 0 ; i < NetworkList.size() ; ++i ) {
/* each group need one HashMap-Here for each group we have subgroups */
String nn = NetworkList.get(i);
ArrayList<String> APlist = new ArrayList<String>(Networks.get(nn));
ArrayList secList = new ArrayList();
for( int n = 0 ; n < APlist.size() ; n++ ) {
HashMap child = new HashMap();
String AP = APlist.get(n);
child.put( "AccPt", AP);
secList.add( child );
}
result.add( secList );
}
return result;
}
I cant figure out how the data update gets notified to the adapter. Unlike the arraylist adapter there are no direct add/remove etc. methods on the SimpleExpandableListAdapter.
From the previous post on the same topic Android: How to correctly use NotifyDataSetChanged with SimpleExpandableListAdapter?
Removed createGroupList() and createChildList().
Directly add elements of child nad group data using GroupList.add and ChildList.add instead of assigning from the functions return variable.
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 my listview activity that works pretty fine but i have an annoying problem.
Whenever it resumes it adds the same items in the the list and that ist gets bigger. I just want it to keep the values i feeded at first.
How can i do that?
here is my activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
adapter = new SimpleAdapter(
this,
list,
R.layout.custom_row_view,
new String[] {"pen","price","color"},
new int[] {R.id.text1,R.id.text2, R.id.text3}
);
populateList();
setListAdapter(adapter);
}
And here is my arrayList:
static final ArrayList<LinkedHashMap<String,String>> list =
new ArrayList<LinkedHashMap<String,String>>();
private void populateList() {
LinkedHashMap<String,String> temp = new LinkedHashMap<String,String>();
temp.put("pen"," Mission name");
list.add(temp);
LinkedHashMap<String,String> temp1 = new LinkedHashMap<String,String>();
temp1.put("pen"," Map activity");
list.add(temp1);
LinkedHashMap<String,String> temp2 = new LinkedHashMap<String,String>();
temp2.put("pen"," Check sensors");
list.add(temp2);
LinkedHashMap<String,String> temp3 = new LinkedHashMap<String,String>();
temp3.put("pen"," Infrared Image");
list.add(temp3);
LinkedHashMap<String,String> temp4 = new LinkedHashMap<String,String>();
temp4.put("pen"," Radar Image");
list.add(temp4);
LinkedHashMap<String,String> temp5 = new LinkedHashMap<String,String>();
temp5.put("pen"," Visual image");
list.add(temp5);
LinkedHashMap<String,String> temp6 = new LinkedHashMap<String,String>();
temp6.put("pen"," suscribe to Viewer");
list.add(temp6);
}
Don't declare the list as static.
i.e.:
final ArrayList<LinkedHashMap<String,String>> list = new ArrayList<LinkedHashMap<String,String>>();
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.