Android中ListView與CheckBox結合----多選與記錄

jopen 10年前發布 | 378K 次閱讀 Android Android開發 移動開發

很多時候我們會用到ListView與CheckBox結合的東西,比如一個清單,可以用來多選,但是很多人似乎也在這個地方遇到很多問題,我剛開始學的時候也是遇到假選問題,當列表中數量多的之后,我勾選一個,滑動頁面會發現條目也勾選上了, 這明顯與我們的要求不符合,后來網上找了找資料,用HashMap來記錄一個CheckBox的勾選記錄就解決了

以下是實現的的一個小Demo

Android中ListView與CheckBox結合----多選與記錄

 

這是XML ListView 每個item文件清單

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <RelativeLayout  
        android:id="@+id/outpatient_check_hospital"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_marginBottom="5.0dip"  
        android:layout_marginLeft="12.599976dip"  
        android:layout_marginRight="12.599976dip"  
        android:layout_marginTop="5.0dip"  
        android:gravity="center_vertical"   
        android:background="#AAAAAA">  

        <LinearLayout  
            android:id="@+id/linear_layout_up"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_margin="10.0dip"  
            android:gravity="center"  
            android:orientation="horizontal" >  

            <ImageView  
                android:layout_width="10dip"  
                android:layout_height="10dip"  
                android:adjustViewBounds="false" />  

            <TextView  
                android:id="@+id/tv_device_name"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:layout_weight="2.0"  
                android:text="名稱"  
                android:textColor="#ff323232"  
                android:textSize="16.0sp"  
                android:typeface="monospace" />  

            <CheckBox  
                android:id="@+id/checkBox1"  
                android:layout_width="wrap_content"  
                android:layout_height="wrap_content"  
                android:text="" />  

        </LinearLayout>  
    </RelativeLayout>  

</LinearLayout>  </pre><a class="CopyToClipboard" title="copy" href="/misc/goto?guid=4959614078538900840"></a></div>

</div> </div>

 

 

 

這是顯示ListView的頁面,簡單初始化幾個數據

</div> </div>

    public class MainActivity extends Activity {

    private ListView listView;  
    private ListViewAdapter adapter;  
    private String[] beans = new String[] { "1", "2", "3", "4", "5", "6", "7",  
            "8", "9", "10", "11", "12", "13","14","15","16","17","18","19" };  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        initView();  
    }  

    private void initView() {  
        // TODO Auto-generated method stub  
        Log.i("htp", "beans.size:" + beans.length);  
        listView = (ListView) findViewById(R.id.listView1);  
        adapter = new ListViewAdapter(MainActivity.this, beans);  
        listView.setAdapter(adapter);  
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);  
    }  </pre><br />



下面就寫一個Adapter類,我們依然繼承BaseAdapter類。這里我們使用一個HashMap<Integer,boolean>的鍵值來記錄checkbox在對應位置的選中狀況

package com.example.listviewcheckboxdemo;

import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {

private Context context;  
private String[] beans;  

// 用來控制CheckBox的選中狀況  
private static HashMap<Integer, Boolean> isSelected;  

class ViewHolder {  

    TextView tvName;  
    CheckBox cb;  
}  

public ListViewAdapter(Context context, String[] beans) {  
    // TODO Auto-generated constructor stub  
    this.beans = beans;  
    this.context = context;  
    isSelected = new HashMap<Integer, Boolean>();  
    // 初始化數據  
    initDate();  
}  

// 初始化isSelected的數據  
private void initDate() {  
    for (int i = 0; i < beans.length; i++) {  
        getIsSelected().put(i, false);  
    }  
}  

@Override  
public int getCount() {  
    // TODO Auto-generated method stub  
    return beans.length;  
}  

@Override  
public Object getItem(int position) {  
    // TODO Auto-generated method stub  
    return beans[position];  
}  

@Override  
public long getItemId(int position) {  
    // TODO Auto-generated method stub  
    return position;  
}  

@Override  
public View getView(final int position, View convertView, ViewGroup parent) {  
    // TODO Auto-generated method stub  
    // 頁面  
    ViewHolder holder;  
    String bean = beans[position];  
    LayoutInflater inflater = LayoutInflater.from(context);  
    if (convertView == null) {  
        convertView = inflater.inflate(  
                R.layout.assist_device_binding_list_item, null);  
        holder = new ViewHolder();  
        holder.cb = (CheckBox) convertView.findViewById(R.id.checkBox1);  
        holder.tvName = (TextView) convertView  
                .findViewById(R.id.tv_device_name);  
        convertView.setTag(holder);  
    } else {  
        // 取出holder  
        holder = (ViewHolder) convertView.getTag();  
    }  

    holder.tvName.setText(bean);  
    // 監聽checkBox并根據原來的狀態來設置新的狀態  
    holder.cb.setOnClickListener(new View.OnClickListener() {  

        public void onClick(View v) {  

            if (isSelected.get(position)) {  
                isSelected.put(position, false);  
                setIsSelected(isSelected);  
            } else {  
                isSelected.put(position, true);  
                setIsSelected(isSelected);  
            }  

        }  
    });  

    // 根據isSelected來設置checkbox的選中狀況  
    holder.cb.setChecked(getIsSelected().get(position));  
    return convertView;  
}  

public static HashMap<Integer, Boolean> getIsSelected() {  
    return isSelected;  
}  

public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {  
    ListViewAdapter.isSelected = isSelected;  
}  

} </pre></div> </div> </div> 來自:http://blog.csdn.net/qq544529563/article/details/38760357

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!