先补充:Android ListView之动态修改
MyAdapter 类:
package com.android.test; import java.util.ArrayList; import java.util.Map; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class MyAdapter extends BaseAdapter { private ListMain main; private ArrayList<Map<String, Object>> list; public MyAdapter(ListMain main, ArrayList<Map<String, Object>> list) { this.main = main; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //如果未选择或list列表为空时直接跳出,避免NullPointException if (position < 0 || list.size() <= 0) { return null; } //如果为空重新导入ListView的布局文件,布局文件自定义 if (convertView == null) { convertView = LayoutInflater.from(main).inflate(R.layout.list1, null); } //实例化 ImageView img = (ImageView)convertView.findViewById(R.id.list1_img_icon); TextView tv1 = (TextView)convertView.findViewById(R.id.list1_tv1); TextView tv2 = (TextView)convertView.findViewById(R.id.list1_tv2); //获取参数 Map<String, Object> map = list.get(position); tv1.setText((String)map.get("TITLE")); tv2.setText((String)map.get("CONTENT")); //修改样式 if (main.getStatus(position) == ListMain.LIST_SELECT) { convertView.setBackgroundColor(Color.parseColor("#003D79")); img.setBackgroundResource(R.drawable.ic_settings_personalize); tv1.setTextColor(Color.BLUE); tv1.setShadowLayer(1, 1, 0.75f, Color.WHITE); } else { convertView.setBackgroundColor(Color.BLACK); img.setBackgroundResource(Integer.valueOf(map.get("IMG").toString())); tv1.setTextColor(Color.WHITE); tv1.setShadowLayer(1, 1, 0.75f, Color.CYAN); } return convertView; } }
在ListMain中,插入一个ArrayList<Map<String, Object>>
ArrayList<Map<String, Object>> list; list = new ArrayList<Map<String,Object>>(); for (int i = 0; i < TITLE.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("TITLE", TITLE[i]); map.put("CONTENT", CONTENT[i]); map.put("IMG", IMG[i]); map.put("STATUS", LIST_NOT); list.add(map);}
将ListView与Adapter绑定:
adapter = new MyAdapter(this, list);
lv.setAdapter(adapter);
在onItemClick中处理选中事件:
for (int i = 0; i < TITLE.length; i++) { list.get(i).put("STATUS", LIST_NOT); } list.get(arg2).put("STATUS", LIST_SELECT); adapter.getView(arg2, null, lv); adapter.notifyDataSetChanged();
效果图如下(图片随便拉来的:-))
初始时:
选中“后来”,改变图标,改变字体颜色,改变文字阴影。
ok,这样就完成了。
言归正传。
单字段排序:
private ArrayList<Map<String, Object>> mArrayList; private void getData() { //add Data..... //对ListView中数据list排序 if (!mArrayList.isEmpty()){ Collections.sort(mArrayList, new Comparator<Map<String, Object>>() { @Override public int compare(Map<String, Object> object1, Map<String, Object> object2) { //根据字段"LEVEL"排序 return object1.get("LEVEL").toString() .compareTo(object2.get("LEVEL").toString()); } }); } }
效果图如下(LEVEL字段为图片样式):
如上图如果这样排你肯定不满意。因为文本没有排序。
多字段排序:
if (!mArrayList.isEmpty()) { Collections.sort(mArrayList, new Comparator<Map<String, Object>>() { @Override public int compare(Map<String, Object> object1, Map<String, Object> object2) { //我把obejct1 和obejct2写倒了,这样写的用处就是反向排序。 int i = object2.get("LEVEL").toString()</pre> .compareTo(object1.get("LEVEL").toString()); if (i == 0) { //如果"LEVEL"字段相同,再根据下一字段排序 object1.get("TITLE").toString() .compareTo(object2.get("TITLE").toString()); } return i; } }); }
前后效果图如下:
值得一提的是右边图片的样式:
<!-- wifi_singal_open.xml --> <level-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:maxLevel="0" android:drawable="@drawable/ic_wifi_signal_1" /> <item android:maxLevel="1" android:drawable="@drawable/ic_wifi_signal_2" /> <item android:maxLevel="2" android:drawable="@drawable/ic_wifi_signal_3" /> <item android:maxLevel="3" android:drawable="@drawable/ic_wifi_signal_4" /> </level-list>
上面drawable是4种不带锁的图片。
在wifi_singal.xml中引用wifi_signal_open和wifi_signal_lock文件: <!-- wifi_singal.xml --> <selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:settings="http://schemas.android.com/apk/res/com.zkx.demo"> <item settings:state_encrypted="true" android:drawable="@drawable/wifi_signal_lock" /> <item settings:state_encrypted="false" android:drawable="@drawable/wifi_signal_open" /> </selector> settings:state_encrypted="true"为自定义属性,在attr.xml中有配置: <resources> <declare-styleable name="WifiEncryptionState"> <attr name="state_encrypted" format="boolean" /> </declare-styleable> </resources>
最后在Adapter getView中配置图片文件:
public static final int[] STATE_SECURED = {R.attr.state_encrypted}; public static final int[] STATE_NONE = {}; ImageView img = (ImageView)v.findViewById(R.id.img); img.setImageResource(R.drawable.wifi_signal); img.setImageLevel(Integer.valueOf(map.get("LEVEL").toString())); //可见或不可见 int[] state = Boolean.valueOf(map.get("STATE").toString()) ? STATE_SECURED : STATE_NONE; img.setImageState(state, true);
时间: 2024-11-02 09:16:53