0

I'm trying to make a simple app that's looks for WiFi networks, and connects to them. I'm currently having a problem with updating the UI.

A few pointers would be great. Thank you for your time.

class UiUpdater extends AsyncTask<Void, Void, List<ScanResult>> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        TextView searching = (TextView) findViewById(R.id.searching);
        searching.setText("Currently searching...");
    }

    @Override
    protected List<ScanResult> doInBackground(Void... params) {
        WifiManager manager = (WifiManager) Client.this.getSystemService(Context.WIFI_SERVICE);

        if (!manager.isWifiEnabled())
            manager.setWifiEnabled(true);

        return manager.getScanResults();
    }

    @Override
    protected void onPostExecute(List<ScanResult> items) {
        super.onPostExecute(items);

        ArrayList<Items> wifi = new ArrayList<>();

        for (ScanResult s : items)
            wifi.add(new Items(s.SSID, s.capabilities));

        ///TextView searching = (TextView) findViewById(R.id.searching);
        ///searching.setText("");

        ListView list = (ListView) findViewById(R.id.list);
        Explorer adapter = new Explorer(Client.this, R.layout.listview_item_row, wifi);

        list.setAdapter(adapter);

        clickListener(list, wifi);
    }

}
Edward Alexander
  • 111
  • 1
  • 2
  • 12

2 Answers2

1

please use Toast message or logcat or breakpoint to check onPost execute is being called or not

MinFu
  • 353
  • 1
  • 13
  • I just checked onPostExecute is called and it can't be my adapter because if I enter some dummy text, it will show it with ease. – Edward Alexander Jan 06 '16 at 01:16
  • your view should update i suspect your arraylist of wifi size is zero mean your getScanResult return nothing that why you can not see your view being update – MinFu Jan 06 '16 at 01:26
0

I managed to get it working, but by taking a different approach.

I used the BroadcastReceiver and registerReceiver to get the onReceive event to update the list.

Here's the code:

    mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);

    if(!mWifiManager.isWifiEnabled())
        mWifiManager.setWifiEnabled(true);
    mWifiManager.startScan();

    wifiReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context c, Intent intent)
        {
            if(mWifiManager != null) {
                List<ScanResult> networks = mWifiManager.getScanResults();
                showWifi(networks);
            }
        }
    };

    registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

Although I haven't managed to understand how AsyncTask works, I did get some nice results with loading a simple progress bar.

I think the problem I encountered was as such:

  1. I started an AsyncTask on another thread.
  2. In the doInBackground, getScanResults started another background thread, leading doInBackground to think the job's done.
  3. onPostExecute was called because doInBackground finished its job.

Bottom line, it wasn't the AsyncTask's fault, it was mine for not knowing that getScanResults starts another background thread.

Edward Alexander
  • 111
  • 1
  • 2
  • 12