0

我从 Android/JAVA 开发开始,我坚持使用 ListView 的 add 方法。我正在尝试创建一个包含所有可用 WiFi 网络的列表。当我在实习生类 WifiReceiver 中调用 add 方法时,什么也没有发生。我不明白。你能给我解释一下吗?

public class MainActivity extends Activity
{
    TextView mainText;
    WifiManager mainWifi;
    WifiReceiver receiverWifi;
    List<ScanResult> wifiList;
    StringBuilder sb = new StringBuilder();
    ListView list = null;
    List<String> wifiData = new ArrayList<String>();

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainText = (TextView) findViewById(R.id.mainText);

        list = (ListView) findViewById(R.id.listView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, wifiData);
        list.setAdapter(adapter);
        mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        receiverWifi = new WifiReceiver();
        registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        mainWifi.startScan();
        // mainText.setText("Starting Scan...");
    }

    public boolean onCreateOptionsMenu(Menu menu)
    {
        menu.add(0, 0, 0, "Refresh");
        return super.onCreateOptionsMenu(menu);
    }

    public boolean onMenuItemSelected(int featureId, MenuItem item)
    {
        mainWifi.startScan();
        mainText.setText("Starting Scan");
        return super.onMenuItemSelected(featureId, item);
    }

    protected void onPause()
    {
        unregisterReceiver(receiverWifi);
        super.onPause();
    }

    protected void onResume()
    {
        registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        super.onResume();
    }

    class WifiReceiver extends BroadcastReceiver
    {
        public void onReceive(Context c, Intent intent)
        {
            sb = new StringBuilder();
            wifiList = mainWifi.getScanResults();
            for(int i = 0; i < wifiList.size(); i++)
                wifiData.add(Integer.toString(i + 1) + "." + (wifiList.get(i)).toString());
            wifiData.add("Bite");
        }
    }
}

谢谢你。

4

2 回答 2

0

更改列表数据后,您需要使用notifyDataSetChanged通知适配器其内容实际上已更改。最简单的选择是将适配器声明为实例变量,以便在内部访问它WifiReceiver。-

adapter.notifyDataSetChanged();
于 2013-11-02T23:28:31.463 回答
0

您需要告诉列表发生了一些变化。adapter.notifyDataSetChanged()添加项目后调用。

class WifiReceiver extends BroadcastReceiver
{
    public void onReceive(Context c, Intent intent)
    {
        sb = new StringBuilder();
        wifiList = mainWifi.getScanResults();
        for(int i = 0; i < wifiList.size(); i++)
            wifiData.add(Integer.toString(i + 1) + "." + (wifiList.get(i)).toString());
        wifiData.add("Bite");
        adapter.notifyDataSetChanged();
    }
}

这还需要您创建adapter一个成员变量,以便内部类知道它。只需声明它与其他变量的关系(在方法之外)

TextView mainText;
WifiManager mainWifi;
WifiReceiver receiverWifi;
List<ScanResult> wifiList;
StringBuilder sb = new StringBuilder();
ListView list = null;
List<String> wifiData = new ArrayList<String>();

只是按照onCreate()你的方式初始化它,但不声明它

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, wifiData);

文档

于 2013-11-02T23:28:55.313 回答