0

我正在尝试显示范围内所有 wifi AP 的列表,即包括具有相同 SSID 的那些,因为这些未显示在默认 wifi 设置中。

这是我对 Android SDK 的第一次尝试,到目前为止我已经做到了:

package com.iiitd.wifistats;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WiFiStats extends Activity implements View.OnClickListener {

    WifiManager wifi;
    List<ScanResult> scanResults;
    ArrayList<Map<String, Integer>> list;
    SimpleAdapter adapter;
    ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        if (!wifi.isWifiEnabled()) {
            Toast.makeText(getApplicationContext(), "Turning on WiFi...", Toast.LENGTH_LONG).show();
            wifi.setWifiEnabled(true);
        }

        carryON();
    }

    private void carryON(){
        final Button button = (Button) findViewById(R.id.buttonScan);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                wifi = (WifiManager) v.getContext().getSystemService(Context.WIFI_SERVICE);
                scanResults = wifi.getScanResults();
                Toast.makeText(getApplicationContext(), "Scanning...", Toast.LENGTH_SHORT).show();
                list = buildData(scanResults);
                //Toast.makeText(getApplicationContext(), list.toString(), Toast.LENGTH_SHORT).show();
                adapter = new SimpleAdapter(v.getContext(), list, R.layout.listitem, new String[]{"BSSID", "strength"}, new int[] {R.id.BSSID, R.id.strength});
                //Toast.makeText(getApplicationContext(), adapter.toString(), Toast.LENGTH_SHORT).show();
                listview = (ListView) findViewById(R.id.list);
                listview.setAdapter(adapter);
            }
        });
    }

    private ArrayList<Map<String, Integer>> buildData(java.util.List<ScanResult> s) {
        ArrayList<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
        for (ScanResult result : s) {
            list.add(putData(result.BSSID, result.level));
        }
        return list;
    }

    private HashMap<String, Integer> putData(String BSSID, int level) {
        HashMap<String, Integer> item = new HashMap<String, Integer>();
        item.put(BSSID, level);
        return item;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.wi_fi_stats, menu);
        return true;
    }

    @Override
    public void onClick(View view) {

    }
}

这是我的activity_xml:

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Scan"
            android:id="@+id/buttonScan"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"/>

    <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/list"
            android:layout_above="@+id/buttonScan"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="14dp"/>
</RelativeLayout>

这是 listitem.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="?android:attr/listPreferredItemHeight"
                android:padding="6dip">

    <TextView
            android:id="@+id/strength"
            android:layout_width="fill_parent"
            android:layout_height="26dip"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:ellipsize="marquee"
            android:singleLine="true"
            android:text="Description"
            android:textSize="12sp"/>
    <TextView
            android:id="@+id/BSSID"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/strength"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_alignWithParentIfMissing="true"
            android:gravity="center_vertical"
            android:text="Example application"
            android:textSize="16sp"/>

</RelativeLayout>

当我开始扫描时没有显示列表。但是,如果我使用 toast,我可以看到列表正确形成。有人可以在这里指导我吗?谢谢。

4

2 回答 2

1

看看这里的代码

adapter = new SimpleAdapter(v.getContext(), list, R.layout.listitem, new String[]{"BSSID", "strength"}, new int[] {R.id.BSSID, R.id.strength});

String 数组告诉适配器在访问您定义的映射时要使用的键。在您的代码中,您正在分配值

private HashMap<String, Integer> putData(String BSSID, int level) {
    HashMap<String, Integer> item = new HashMap<String, Integer>();
    item.put(BSSID, level);
    return item;
}

这是将 int for level 分配给由 String BSSID 设置的键。您需要更改代码中所有出现的 HashMap 以获取 String 值和 String 键HashMap< String, String >,并将该方法替换为以下内容

private HashMap<String, String> putData(String BSSID, int level) {
    HashMap<String, String> item = new HashMap<String, String>();
    item.put("BSSID", BSSID);
    item.put("strength", Integer.toString(level));
    return item;
}
于 2013-09-04T02:51:51.193 回答
0

您是否尝试过扩展 ListActivity 而不是普通的活动?您可能需要将 XML 中 ListView 的 ID 更改为 android:id="@id/android:list"

然后在您的 caryON() 方法中,您将setListAdapter(adapter);在初始化新的简单适配器后调用

adapter = new SimpleAdapter(v.getContext(), list, R.layout.listitem, new String[]{"BSSID", "strength"}, new int[] {R.id.BSSID, R.id.strength});
setListAdapter(adapter);
于 2013-09-04T02:33:23.217 回答