我做了一个应用程序,每 5 秒刷新一次 WiFi 的状态。这是主要活动
public class WiFiList extends Activity {
/** Called when the activity is first created. */
private String screen;
private ListView nets;
private WifiManager wifi;
private NetAdapter nAdap;
SharedPreferences getPrefs;
Updater skaner;
List<ScanResult> scan;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String screen = getPrefs.getString("start", "list");
wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if(screen.equals("list")){
setContentView(R.layout.main);
nets = (ListView) findViewById(R.id.lvNets);
wifi.startScan();
scan = new ArrayList<ScanResult>();
nAdap = new NetAdapter(getApplicationContext(), scan);
nets.setAdapter(nAdap);
nAdap.notifyDataSetChanged();
}
skaner = new Updater();
skaner.execute();
}
public void refresh(){
nAdap.notifyDataSetChanged();
}
class Updater extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
// doSomething();
while(wifi.isWifiEnabled()){
int waiter = getPrefs.getInt("intervals", 5) * 1000;
wifi.startScan();
scan = wifi.getScanResults();
publishProgress();
try {
Thread.sleep(waiter);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(isCancelled()){
break;
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
refresh();
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
skaner.cancel(true);
super.onBackPressed();
}
但是 ListView 不会刷新。当我运行调试器时,我看到它refresh()
被调用并且列表每次都有不同的值。是什么阻止了它令人耳目一新?当我让它扫描 中的网络时onCreate()
,它们会出现在列表中,所以我假设适配器是正确的,但无论如何这里是代码:
public class NetAdapter extends BaseAdapter{
private LayoutInflater mInflater;
List<ScanResult> res;
public NetAdapter(Context context, List<ScanResult> l){
mInflater = LayoutInflater.from(context);
res = l;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return res.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return res.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.netitem, null);
holder = new ViewHolder();
holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
holder.tvFreq = (TextView) convertView.findViewById(R.id.tvFreq);
holder.tvRssi = (TextView) convertView.findViewById(R.id.tvRssi);
holder.ivState = (ImageView) convertView.findViewById(R.id.ivSignal);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvName.setText(res.get(position).SSID +" (" + res.get(position).BSSID + ")");
holder.tvFreq.setText(Integer.toString(res.get(position).frequency)+"MHz");
holder.tvRssi.setText(Integer.toString(res.get(position).level)+"dBm");
holder.ivState.setImageResource(R.drawable.ic_launcher);
return convertView;
}
static class ViewHolder{
TextView tvName;
TextView tvFreq;
TextView tvRssi;
ImageView ivState;
}