我想将listview
与ArrayAdapter
扫描WiFi后,广播接收器的代码如下
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);
}
但这显示scanResult.setAdapter(scanResultDevices)
我知道如何使用new ArrayAdapter<T>(context, textViewResourceId, objects)
。但在这种特殊情况下,我想尝试new ArrayAdapter<T>(context, textViewResourceId)
。如果我正在尝试的是不可能的,那么我该怎么做呢?
更新了代码
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();
}
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())
}
答案 0 :(得分:0)
可以简单地说:
ArrayAdapter<YOUR_CLASS> name = new ArrayAdapter<YOUR_CLASS>(context, textViewResourceId);
然后在api 11级以上
name.addAll(YOUR_OBJECTS_COLLECTION);
或以下
for(Class item_name: collection){
name.add(item_name);
}
BroadcastReceiver对象仅在调用onReceive(Context,Intent)期间有效。一旦您的代码从此函数返回,系统会认为该对象已完成且不再有效。
你应该在onReceive方法中初始化scanResult