WIFI列表

转载地址:http://blog.csdn.net/liuhui_8989/article/details/22945141


部分参考:http://l62s.iteye.com/blog/1675509

一个ListView通常有两个职责。

1)将数据填充到布局。

2)处理用户的选择点击等操作。

第一点很好理解,ListView就是实现这个功能的。第二点也不难做到。

一个ListView的创建需要3个元素。

1ListView中的每一列的View

2)填入View的数据或者图片等。

3)连接数据与ListView的适配器。

首先了解下什么是适配器:

适配器是AdapterView视图(如ListView - 列表视图控件、Gallery - 缩略图浏览器控件、GridView - 网格控件、Spinner - 下拉列表控件、AutoCompleteTextView - 自动提示文本框、ExpandableListView - 支持展开/收缩功能的列表控件等)与数据之间的桥梁,用来处理数据并将数据绑定到AdapterView上。

android提供多种适配器,开发时可以针对数据源的不同采用最方便的适配器,也可以自定义适配器完成复杂功能。

补充:AdapterView对象有两个主要任务
          1. 在布局中显示数据
         2. 处理用户的选择

也就是说,适配器能有效地实现数据与AdapterView的分离设置,使AdapterView与数据的绑定更加简便,修改更加方便

Android中提供了很多的Adapter,表1列出了常用的几个。

常用适配器

Adapter

含义

ArrayAdapter<T>

用来绑定一个数组,支持泛型操作

SimpleAdapter

用来绑定在xml中定义的控件对应的数据

SimpleCursorAdapter

用来绑定游标得到的数据

BaseAdapter

通用的基础适配器

 下面以自己的一个对ListView控件的简单应用,介绍下使用ListView 的操作步骤。

目的:实现对搜索到的WIFI信号的列表显示,至于WIFi的使用后面讲。

1、定义好布局文件。布局文件中包含一个Button和一个ListView。

<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
            
            
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="@color/burlywood"
tools:context=".ControlPCMainActivity" >
<Button
android:id="@+id/wifiSearchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="搜索WIFI信号"/>
<ListView
android:id="@+id/wifiListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:singleLine="false"/>
</LinearLayout>
 来自CODE的代码片
WIFIlayout.XML
2、在Activity中定义一个ArrayAdapter,然后为LIstView绑定一个Adapter。
<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a>
             
             
wifiSearchListView = (ListView)findViewById(R.id.wifiListView);
arrayWifiAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,wifiListString);
wifiSearchListView.setAdapter(arrayWifiAdapter);
 来自CODE的代码片
apdater
重点讲解下new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,wifiListString);
首先对ArrayAdapter的参数进行说明:
第一个参数:为上下文,此处指定的this为Activity
第二个参数:一个包含TextView,用来填充ListView的每一行的布局资源ID,这个可以自定义一个Layout,但是Layout中必须得包含TextView控件。这个自定义布局文件之前有介绍 http://blog.csdn.net/liuhui_8989/article/details/22941581,此处使用的是系统提供的Layout布局。
第三个参数:为ListView的内容,此处为private List<String> wifiListString = new ArrayList<String>();存储的是WIFI信号的信息。
ListView是通过绑定一个适配器来实现自身数据的更新的,即代码中的wifiSearchListView.setAdapter(arrayWifiAdapter);
3、数据更新,此处主要是搜索WIFI信号,将搜索出来的WIFi信号进行列表显示。
<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
             
             
//ScanResult类型转为String
public void scanResultToString(List<ScanResult> listScan,List<String> listStr){
for(int i = 0; i < listScan.size(); i++){
ScanResult strScan = listScan.get(i);
String str = strScan.SSID+"--"+strScan.BSSID;
boolean bool = listStr.add(str);
if(bool){
arrayWifiAdapter.notifyDataSetChanged();//数据更新,只能单个Item更新,不能够整体List更新
}
else{
Log.i("scanResultToSting","Addfail");
}
Log.i("scanResultToString",listStr.get(i));
}
}
 来自CODE的代码片
datauptate
这个函数的功能是实现对搜索到的WIFI信号的处理,listScan存储的是搜索到的WIFI信号信息,包含内容比较多。我在ListView中只显示WIFI的名称SSID和地址BSSID,两项保存在listStr,即实参 wifiListString中。
此处要特别注意之前对数据的更新,是对数据处理完成后对wifiListString整体更新,结果是失败的。最终找到原因是只能够对wifiListString中的单个Item进行更新。
通过绑定的适配器Adapter方法通知数据的更新:arrayWifiAdapter.notifyDataSetChanged();
4、数据更新完成后就是要实现对ListView中Item的点击事件,以及点击后颜色的变化。
首先为ListView设定监听器
<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
             
             
ListOnItemClickListener wifiListListener = new ListOnItemClickListener();
wifiSearchListView.setOnItemClickListener(wifiListListener);
 来自CODE的代码片
setListener
下面为对监听器Click事件的重写
<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank id="L29" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank id="L30" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank id="L31" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank id="L32" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
<a target=_blank id="L33" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a>
<a target=_blank id="L34" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a>
<a target=_blank id="L35" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;"> 35</a>
<a target=_blank id="L36" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;"> 36</a>
<a target=_blank id="L37" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;"> 37</a>
<a target=_blank id="L38" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;"> 38</a>
<a target=_blank id="L39" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L39" rel="#L39" style="color: rgb(102, 102, 102); text-decoration: none;"> 39</a>
<a target=_blank id="L40" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L40" rel="#L40" style="color: rgb(102, 102, 102); text-decoration: none;"> 40</a>
<a target=_blank id="L41" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L41" rel="#L41" style="color: rgb(102, 102, 102); text-decoration: none;"> 41</a>
<a target=_blank id="L42" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L42" rel="#L42" style="color: rgb(102, 102, 102); text-decoration: none;"> 42</a>
<a target=_blank id="L43" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L43" rel="#L43" style="color: rgb(102, 102, 102); text-decoration: none;"> 43</a>
<a target=_blank id="L44" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L44" rel="#L44" style="color: rgb(102, 102, 102); text-decoration: none;"> 44</a>
<a target=_blank id="L45" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L45" rel="#L45" style="color: rgb(102, 102, 102); text-decoration: none;"> 45</a>
<a target=_blank id="L46" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L46" rel="#L46" style="color: rgb(102, 102, 102); text-decoration: none;"> 46</a>
<a target=_blank id="L47" href="http://blog.csdn.net/liuhui_8989/article/details/22945141#L47" rel="#L47" style="color: rgb(102, 102, 102); text-decoration: none;"> 47</a>
             
             
class ListOnItemClickListener implements OnItemClickListener{
String wifiItemSSID = null;
private View selectedItem;
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selectedItem = arg1;
arg1.setBackgroundResource(R.color.gray);//点击的Item项背景设置
String wifiItem = arrayWifiAdapter.getItem(arg2);//获得选中的设备
String []ItemValue = wifiItem.split("--");
wifiItemSSID = ItemValue[0];
int wifiItemId = localWifiUtils.IsConfiguration("\""+wifiItemSSID+"\"");
if(wifiItemId != -1){
if(localWifiUtils.ConnectWifi(wifiItemId)){//连接指定WIFI
arg1.setBackgroundResource(R.color.green);
}
}
else{//没有配置好信息,配置
WifiPswDialog pswDialog = new WifiPswDialog(ControlPCMainActivity.this,new OnCustomDialogListener() {
@Override
public void back(String str) {
// TODO Auto-generated method stub
wifiPassword = str;
if(wifiPassword != null){
int netId = localWifiUtils.AddWifiConfig(wifiResultList,wifiItemSSID, wifiPassword);
Log.i("WifiPswDialog",String.valueOf(netId));
if(netId != -1){
localWifiUtils.getConfiguration();//添加了配置信息,要重新得到配置信息
if(localWifiUtils.ConnectWifi(netId)){
selectedItem.setBackgroundResource(R.color.green);
}
}
else{
Toast.makeText(ControlPCMainActivity.this, "网络连接错误", Toast.LENGTH_SHORT).show();
selectedItem.setBackgroundResource(R.color.burlywood);
}
}
else{
selectedItem.setBackgroundResource(R.color.burlywood);
}
}
});
pswDialog.show();
}
}
}
 来自CODE的代码片
ItemListener
这段代码中设计到WIFI的一些操作,参考下以后的WIFI的操作文章吧。另外dialog的实现也会将实现过程写出来。
通过代码中可以看出来Item的点击事件主要是通过继承OnItemClickListener接口实现对点击事件的处理。   
首先说明下onItemClick里面的四个参数,参考: http://blog.csdn.net/catherine880619/article/details/6796561
arg0参数:指的当前的适配器,可以通过它获得适配器里面的所有内容
arg1参数:当前选中的Item的View。ListView里面是由各个View构成的,一些View就构成了ListView。
arg2参数:Item在适配器中的位置
arg3参数:当前Item在ListView中的相对位置,第几行
设定选中Item颜色:arg1.setBackgroundResource(R.color.gray);
另外还有另外一种方法是:
clientBltList.requestFocusFromTouch();//clientBltList为ListView的实例
clientBltList.getSelectedView().setBackgroundColor(R.color.green);
得到选中Item的内容:arrayWifiAdapter.getItem(arg2);
在颜色变化的实现中遇到的问题:本身要实现效果是是点击后Item变为灰色,WIFI连接成功后变为绿色。但是实现过程中发现当上下滑动WIFI列表时候会出现多个WIFI信号变为绿色。而且绿色的位置也会发生变化。一直未解决。
效果图                                                          滑动前                                              滑动后

原因分析:主要是因为ListView 的显示采用了一种优化方法,当调用LIstView显示里面内容时候是通过重写getView()方法实现的。getView ()方法中的参数convertView作为一个缓存,当有条目不可见时,它缓存了它的数据,当上下滑动显示出来该条目数据时候只需要更新数据就可以,这样就重复利用了绘制的View,大大节省了系统开销。这样的重复利用导致了每个Item的位置ID发生滑动时候发生了变化。所以颜色变化的条目就发生了变化。例如全屏显示6个条目,当Item1显示为绿色,滑动时候Item1不可见,测出的View就被下面显示Item重用更新数据,导致下面数据的ID和Item1的ID一样,所以颜色同样变为了绿色。这只是本人的理解。。。。
                                                        http://mobile.51cto.com/android-265197_all.htm
参考的内容为下面


相关project源码:http://download.csdn.net/detail/liuhui_8989/7154671

有错误多多指出


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值