0

我做了一个应用程序,每 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;
}
4

3 回答 3

0

Keep in mind that startScan() returns immediately and the availability of results is made known asynchronously when the results are available. so when you read the scan results straight after the call to startscan() the results are most likely not there hence you are sending blank information to the adapter.

So this covers why your view is not refreshing. So how do you make sure that you only read results when they are available. See recommendation below.

it will be better to use the BroadcastReceiver to receive the scan results. Register for SCAN_RESULTS_AVAILABLE_ACTION

so you run the scan in your async but don't do any refreshes from there just use it keep your loop running. you can use just a thread for that as well if you wanted to.

To update simply make a call back from your receiver back to your activity where you read the scan results.

I also noticed that your are calling startscan() in onCreate and then straight after you create an Async task to do the same again. what is the point of doing that.

于 2012-06-08T10:14:54.267 回答
0

如果我正确理解您的问题。

假设:1.您正在更新适配器中的值

解决方案:

要刷新列表视图,您需要做的就是:

your_list_adapter.notifyDataSetChanged();

我希望这有帮助..

于 2012-06-08T12:33:19.737 回答
0

您应该使用下面的 AsyncTask 方法来更新列表视图。

protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
于 2012-06-08T09:56:25.270 回答