Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top