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;
}
}
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
– BitBank
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.
– João Eduardo