java - No display - ListView using SimpleAdapter -
i'm trying display list of wifi ap's in range i.e. including same ssid's these not shown in default wifi settings.
this first attempt @ android sdk , i've gotten far:
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>>(); (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 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 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 no list displayed when start scan. however, if use toast, can see list forming correctly. can guide me here? thanks.
take @ 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 telling adapter keys used when accessing map define. in code, assigning values 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 assigning int level key set string bssid. need change occurrences of hashmap in code take string value , string key hashmap< string, string >
and replace method following
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; }
Comments
Post a Comment