DBMNG数据库管理与应用

书籍是全世界的营养品。生活里没有书籍,就好像没有阳光;智慧里没有书籍,就好像鸟儿没有翅膀。
当前位置:首页 > 移动应用 > Android

通过重写Adapter实现多选的ListView

下面这个是需要现实在ListView中的布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout android:id="@+id/row"
  3.         xmlns:android="http://schemas.android.com/apk/res/android"
  4.         android:orientation="horizontal"
  5.         android:layout_width="fill_parent"
  6.         android:layout_height="wrap_content"
  7.         >
  8.        
  9.         <RelativeLayout
  10.                 android:layout_width="fill_parent"
  11.                 android:layout_height="wrap_content">
  12.        
  13.                 <ImageView android:id="@+id/tag"
  14.                     android:layout_width="wrap_content"
  15.                         android:layout_height="wrap_content"
  16.                         android:background="@drawable/icon"/>
  17.                
  18.                 <LinearLayout android:layout_width="wrap_content"
  19.                         android:layout_height="wrap_content"
  20.                         android:layout_marginLeft="5dip"
  21.                         android:orientation="vertical"
  22.                         android:layout_marginTop="7dip"
  23.                         android:layout_toRightOf="@id/tag"
  24.                         >
  25.                
  26.                         <TextView android:id="@+id/multiple_title"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29.                             android:gravity="center_vertical"
  30.                                 android:textSize="20dip"
  31.                                 android:layout_marginLeft="5dip"/>
  32.                                
  33.                                
  34. <TextView android:id="@+id/multiple_summary"
  35.                             android:layout_width="fill_parent"
  36.                                 android:layout_height="wrap_content"
  37.                                 android:layout_marginLeft="5dip"
  38.                                 android:gravity="center_vertical"/>
  39.                
  40.                 </LinearLayout>
  41.                
  42.                 <!--
  43.                 这三个很重要
  44.                 android:focusable="false"
  45.                 android:focusableInTouchMode="false"
  46.                 android:clickable="false"
  47.                 -->
  48.                 <CheckBox 
  49.                         android:id="@+id/multiple_checkbox"
  50.                         android:layout_width="wrap_content"
  51.                                 android:layout_height="wrap_content"
  52.                                 android:layout_gravity="center_vertical"
  53.                                 android:layout_marginTop="6dip"
  54.                                 android:focusable="false"
  55.                                 android:focusableInTouchMode="false"
  56.                                 android:clickable="false"
  57.                                 android:layout_alignParentRight="true"/>
  58.        
  59.         </RelativeLayout>
  60.            
  61. </LinearLayout>
  62. 这个文件是Activity的布局文件:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.         android:layout_width="fill_parent"
    4.         android:layout_height="wrap_content"
    5.         >
    6.        
    7.         <ListView android:id="@+id/listview"
    8.                 android:layout_width="fill_parent"
    9.                 android:layout_height="wrap_content"
    10.                 android:cacheColorHint="#00000000"
    11.                 />
    12.                
    13. </LinearLayout>
    复制代码
    有了上面的两个文件,就可以写Activity了~~~~
    下面的是源码:
    1. package com.heji.demo.effect;
    2. import java.util.ArrayList;
    3. import java.util.HashMap;
    4. import java.util.List;
    5. import java.util.Map;
    6. import android.app.Activity;
    7. import android.content.Context;
    8. import android.os.Bundle;
    9. import android.view.LayoutInflater;
    10. import android.view.View;
    11. import android.view.ViewGroup;
    12. import android.widget.AdapterView;
    13. import android.widget.CheckBox;
    14. import android.widget.ListView;
    15. import android.widget.SimpleAdapter;
    16. import android.widget.TextView;
    17. import android.widget.AdapterView.OnItemClickListener;
    18. import com.heji.demo.R;
    19. public class MultipleChoiceActivity extends Activity implements OnItemClickListener{
    20.        
    21.         private MyAdapter mSimpleAdapter;
    22.        
    23.         public final static String NAME = "name"; 
    24.         public final static String PHONE_NUMBER = "phone";
    25.         @Override
    26.         protected void onCreate(Bundle savedInstanceState) {
    27.                 super.onCreate(savedInstanceState);
    28.                
    29.                 setTitle("带有多选的Listview");
    30.                
    31.                 setContentView(R.layout.multiple_checkbox_main);
    32.                
    33.                 ListView listView = (ListView) findViewById(R.id.listview);
    34.                
    35.                 ArrayList<Map<String, String>> al = new ArrayList<Map<String,String>>();
    36.                 Map<String, String> map1 = new HashMap<String, String>();
    37.                
    38.                 map1.put(NAME, "A");
    39.                 map1.put(PHONE_NUMBER, "132456789");
    40.                 al.add(map1);
    41.                
    42.                 Map<String, String> map2 = new HashMap<String, String>();
    43.                 map2.put(NAME, "B");
    44.                 map2.put(PHONE_NUMBER, "132134");
    45.                 al.add(map2);
    46.                
    47.                 Map<String, String> map3 = new HashMap<String, String>();
    48.                 map3.put(NAME, "C");
    49.                 map3.put(PHONE_NUMBER, "132134");
    50.                 al.add(map3);
    51.                
    52.                 Map<String, String> map4 = new HashMap<String, String>();
    53.                 map4.put(NAME, "D");
    54.                 map4.put(PHONE_NUMBER, "132134");
    55.                 al.add(map4);
    56.                
    57.                 Map<String, String> map5 = new HashMap<String, String>();
    58.                 map5.put(NAME, "E");
    59.                 map5.put(PHONE_NUMBER, "132134");
    60.                 al.add(map5);
    61.                
    62.                 Map<String, String> map6 = new HashMap<String, String>();
    63.                 map6.put(NAME, "F");
    64.                 map6.put(PHONE_NUMBER, "132134");
    65.                 al.add(map6);
    66.                
    67.                 Map<String, String> map7 = new HashMap<String, String>();
    68.                 map7.put(NAME, "G");
    69.                 map7.put(PHONE_NUMBER, "132134");
    70.                 al.add(map7);
    71.                
    72.                
    73.                 Map<String, String> map8 = new HashMap<String, String>();
    74.                 map8.put(NAME, "H");
    75.                 map8.put(PHONE_NUMBER, "132134");
    76.                 al.add(map8);
    77.                
    78.                 Map<String, String> map9 = new HashMap<String, String>();
    79.                 map9.put(NAME, "I");
    80.                 map9.put(PHONE_NUMBER, "132134");
    81.                 al.add(map9);
    82.                
    83.                 Map<String, String> map10 = new HashMap<String, String>();
    84.                 map10.put(NAME, "J");
    85.                 map10.put(PHONE_NUMBER, "132134");
    86.                 al.add(map10);
    87.                
    88.                 Map<String, String> map11 = new HashMap<String, String>();
    89.                 map11.put(NAME, "K");
    90.                 map11.put(PHONE_NUMBER, "132134");
    91.                 al.add(map11);
    92.                
    93.                
    94.                 String[] from = { NAME, PHONE_NUMBER };
    95.                 int[] to = { R.id.multiple_title, R.id.multiple_summary };
    96.                 mSimpleAdapter = new MyAdapter(this, al, R.layout.multiple_checkbox_main_row, from, to);
    97.                
    98.                 listView.setAdapter(mSimpleAdapter);
    99.                 listView.setOnItemClickListener(this);
    100.         }
    101.         @Override
    102.         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    103.                 CheckBox checkBox = (CheckBox) view.findViewById(R.id.multiple_checkbox);
    104.                
    105.                 checkBox.toggle();
    106.                
    107.                 mSimpleAdapter.map.put(position, checkBox.isChecked());
    108.                
    109.         }
    110.        
    111.         public class MyAdapter extends SimpleAdapter {
    112.                
    113.                 Map<Integer, Boolean> map;
    114.                
    115.                 LayoutInflater mInflater;
    116.                
    117.                 private List<? extends Map<String, ?>> mList;
    118.                
    119.                 public MyAdapter(Context context, List<Map<String, String>> data,
    120.                                 int resource, String[] from, int[] to) {
    121.                         super(context, data, resource, from, to);
    122.                         map = new HashMap<Integer, Boolean>();
    123.                         mInflater = LayoutInflater.from(context);
    124.                         mList = data;
    125.                         for(int i = 0; i < data.size(); i++) {
    126.                                 map.put(i, false);
    127.                         }
    128.                 }
    129.                
    130.                 @Override
    131.                 public int getCount() {
    132.                         return mList.size();
    133.                 }
    134.                 @Override
    135.                 public Object getItem(int position) {
    136.                         return position;
    137.                 }
    138.                 @Override
    139.                 public long getItemId(int position) {
    140.                         return position;
    141.                 }
    142.                
    143.                 @Override
    144.                 public View getView(int position, View convertView, ViewGroup parent) {
    145.                         if(convertView == null) {
    146.                                 convertView = mInflater.inflate(R.layout.multiple_checkbox_main_row, null);
    147.                         }
    148.                         TextView tN = (TextView) convertView.findViewById(R.id.multiple_title);
    149.                         tN.setText((String)mList.get(position).get(NAME));
    150.                        
    151.                         TextView tP = (TextView) convertView.findViewById(R.id.multiple_summary);
    152.                         tP.setText((String)mList.get(position).get(PHONE_NUMBER));
    153.                        
    154.                         CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.multiple_checkbox);
    155.                        
    156.                         checkBox.setChecked(map.get(position));
    157.                        
    158.                         return convertView;
    159.                 }
    160.                
    161.         }
    162. }
    复制代码
    下面我来分析为什么要用map来保存checkbox的状态。

    这个与ListView的刷新机制有关,当你的listview对象很多的时候,每次你拖动listview上下滚动,listview都会刷新一次。怎么刷新呢?比如一个屏幕它最多只显示七条listview,如果你有十条数据,当你想看第八条时,第一条数据理所当然的要被隐藏掉,而第八条数据会被显示,这时listview就刷新了。如果你不保存你所选的checkbox的状态,这时如果你选的是第一条的checkbox的状态为true,当你把余下的第八、第九、第十条数据显示出来时,第十条的checkbox的状态会显示为true,但是它的状态没有被保存,只是你看到它是被选中了而已,其实你选的还是第一条数据。这个问题很操蛋。还有一个更离奇的状态,你让checkbox的状态为true,数据一定要大于十条,你不停的上下拖动屏幕,你会看见checkbox的显示状态会乱跳,但是你实际上选择的还是第一条数据,只是会让你的用户感觉很不爽罢了。
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号