Display ListView with available networks

3

I'm trying to populate a ListView with the List returned by the getScanResults() method of the WifiManager class. However, I would not want to have to go through this list, after all, all the information I need is already in it. However, by passing this list directly to this method, it displays all properties of the ScanResult class for each item. I would like to be able to display only the SSID.

package tk.joaoeduardo.metropolitano;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Main extends Activity {

    private ListView list;
    private WifiManager wifi;
    private ArrayAdapter<ScanResult> adapter;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    }

    protected void onStart() {

        super.onStart();

        list = (ListView) findViewById(R.id.list);

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

        adapter = new ArrayAdapter<ScanResult>(this, android.R.layout.simple_list_item_1);

    }

    protected void onResume() {

        super.onResume();

        adapter.addAll(wifi.getScanResults());

        list.setAdapter(adapter);

    }

    protected void onPause() {

        super.onPause();

        list = null;

        wifi = null;

        adapter = null;

    }

}
    
asked by anonymous 31.01.2014 / 14:57

2 answers

2

It will not be possible without going through the list returned by the getScanResults method. In the posted code this is done in the addAll method of the adapter. This method traverses the list that is passed to it and adds each item to the array.

What I suggest is to create a List<String> only with the SSID obtained from getScanResults and pass it to adapter on constructor

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ssidList);

Instead of being the addAll method of the adapter to go through the list, you will be doing this when creating the list ssidList

    
31.01.2014 / 16:40
0

You can make an Adapter that inherits ArrayAdapter<ScanResult> and in it you override how to display the information by overwriting the getView method.

You can also create a ArrayAdapter<String> that gets a List<String> in the constructor and you override the getItem method.

    
01.02.2014 / 18:37