我已经通过了Android: How to correctly use NotifyDataSetChanged with SimpleExpandableListAdapter?,并且具有类似的程序结构,但是创建(第一次扫描)时列表(接入点)会出现,并在下一次wifi扫描时消失。
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;
}
我无法弄清楚数据更新如何通知适配器。与arraylist适配器不同,SimpleExpandableListAdapter
上没有直接添加/删除等方法。
答案 0 :(得分:0)
来自上一篇文章的同一主题Android: How to correctly use NotifyDataSetChanged with SimpleExpandableListAdapter?
删除了createGroupList()和createChildList()。 使用GroupList.add和ChildList.add直接添加子nad组数据的元素,而不是从函数返回变量中分配。