简体   繁体   English

没有显示-使用SimpleAdapter的ListView

[英]No display - ListView using SimpleAdapter

I'm trying to display a list of ALL the wifi AP's in range ie including those with same SSID's as these are not shown in the default wifi settings. 我正在尝试显示范围内所有wifi AP的列表,即包括具有相同SSID的那些AP,因为默认wifi设置中未显示这些SSID。

This is my first attempt at Android SDK and I've gotten so far: 这是我第一次尝试使用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) {

    }
}

This is my activity_xml: 这是我的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>

And this is listitem.xml: 这是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>

There is no list displayed when the I start a scan. 当我开始扫描时,没有显示列表。 However, if I use a toast, the I can see that the list is forming correctly. 但是,如果我用烤面包,则可以看到列表正确地形成了。 Can someone guide me here? 有人可以在这里引导我吗? Thanks. 谢谢。

Take a look at the code here 在这里看看代码

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

The String array is telling the adapter the keys to be used when accessing the map that you define. String数组告诉适配器访问您定义的映射时要使用的键。 In your code, you are assigning the values as such 在您的代码中,您将这样分配值

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

This is assigning the int for level to the key set by the String BSSID. 这是将级别的int分配给由字符串BSSID设置的键。 You need to change all occurrences of HashMap in your code to take a String value and a String key HashMap< String, String > and replace the method with the following 您需要更改代码中所有出现的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;
}

have you tried extending ListActivity instead of a plain activity? 您是否尝试过扩展ListActivity而不是普通活动? you could need to change the ID of the ListView in your XML to android:id="@id/android:list" 您可能需要将XML中的ListView的ID更改为android:id =“ @ id / android:list”

Then in your caryON() method you would call setListAdapter(adapter); 然后在您的caryON()方法中,将调用setListAdapter(adapter); ;。 after initialising the new Simple Adapter 初始化新的简单适配器后

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM