i want to use listview
with ArrayAdapter
After scanning for WiFi, the code for broadcast receiver is as below
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
scanResultDevices.notifyDataSetInvalidated();
wifiResults = wm.getScanResults();
scanResultDevices = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1);
scanResultAdapter = new ArrayAdapter<ScanResult>(getApplicationContext(),android.R.layout.simple_list_item_1);
Log.e("inside","BD REceiver");
if(wifiResults != null){
for(ScanResult s : wifiResults){
scanResultAdapter.add(s);
scanResultDevices.add(s.SSID);
}
scanResult.setAdapter(scanResultDevices);
scanResultDevices.notifyDataSetChanged();
}
unregisterReceiver(scanComp);
}
But this shows null pointer exception at scanResult.setAdapter(scanResultDevices)
I know how to use new ArrayAdapter<T>(context, textViewResourceId, objects)
. But in this specific case i want to try new ArrayAdapter<T>(context, textViewResourceId)
. If what i am trying is not possible, then how can i do it correctly?
Updated code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
View v = findViewById(R.id.dialogtitle);
scanResult = (ListView) findViewById(R.id.scanResult);
//scanResult.setAdapter(scanResultDevices);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Scan For Device").setCustomTitle(v).setPositiveButton("SCan",this).create();
builder.show();
}
The code for onClick:
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
wm = (WifiManager) getSystemService(WIFI_SERVICE);
if(!wm.isWifiEnabled()){
wm.setWifiEnabled(true);
Toast.makeText(getApplicationContext(), "Turning on Wifi", Toast.LENGTH_SHORT);
Log.e("Wifi","Not-on!Enabling");
}
MyDialogFragment alert = new MyDialogFragment();
alert.show(getFragmentManager(), "SCAN_DIALOG");
wm.startScan();
registerReceiver(scanComp, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
// if(wifiResults.isEmpty())
}
It is possible simply:
ArrayAdapter<YOUR_CLASS> name = new ArrayAdapter<YOUR_CLASS>(context, textViewResourceId);
And then in api above level 11
name.addAll(YOUR_OBJECTS_COLLECTION);
or below
for(Class item_name: collection){
name.add(item_name);
}
A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.
You should init scanResult in onReceive method
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments