0

我已经通过了Android:如何正确使用带有 SimpleExpandableListAdapter 的 NotifyDataSetChanged?,并具有类似的程序结构,但(接入点)列表在创建(第一次扫描)时出现,并在下一次 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.

4

1 回答 1

0

来自上一篇关于 Android 同一主题的文章:如何正确使用带有 SimpleExpandableListAdapter 的 NotifyDataSetChanged?

删除了 createGroupList() 和 createChildList()。使用 GroupList.add 和 ChildList.add 直接添加子和组数据的元素,而不是从函数返回变量中分配。

于 2013-07-24T08:00:15.467 回答