View Listview with available networks

Asked

Viewed 606 times

4

I’m trying to popular a ListView with List returned by method getScanResults() class WifiManager. However, I would not like to have to go through this list, after all, all the information I need is already on it. However, by directly passing this list to this method, it displays all class properties ScanResult 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;

    }

}
  • 2

    Instead of using Arrayadapter, you will have to create a Basedapter class and manage how Listview displays the information. Look here for an example: http://stackoverflow.com/questions/16333754/how-to-customize-listview-using-basedpter

  • I researched more about the Basedapter class, and actually, it’s more interesting in this case that I demonstrated. But that depends on the case. Initially I had used the solution of ramaral, but I’m still researching to reach a decision - even to post here the code.

2 answers

2


It will not be possible without going through the list returned by the method getScanResults.
In the code posted this is done in the method addAllAdapter.
This method goes through the list that is passed to you 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 on to adapter in the constructor

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

Instead of being the method addAllfrom the Adapter to go through the list becomes you to do it when creating the list ssidList

  • As I said in the reply to @bitbank’s comment, the solution using Basedapter fits better with the situation I presented, but it would be a matter of taste after all. I identified myself better with that answer, since I wanted to avoid another looping. However, your answer is also correct.

0

You can make an Adapter that you inherit ArrayAdapter<ScanResult> and in it you replace the way to display the information by overwriting the method getView.

Also you can create a ArrayAdapter<String> who receives a List<String> in the constructor and you override the method getItem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.