Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> ListView的CheckBox實現全部選中/不選中,listviewcheckbox

ListView的CheckBox實現全部選中/不選中,listviewcheckbox

編輯:關於android開發

ListView的CheckBox實現全部選中/不選中,listviewcheckbox


在Adapter類中定義一個HashMap列表,保存每一行是否被選中:

private static HashMap<Integer, Boolean> isSelected;

可見定義了名為isSelected的HashMap列表,再定義它的getter和setter方法:

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

在Adapter類的構造方法中,將isSelected的所有初始值設置為false:

    public MyAdapter(Context context, List<String> list){
        this.context = context;
        this.list = list;        
        isSelected = new HashMap<Integer, Boolean>();
        initData();
    }
    
    private void initData() {  
        for (int i = 0; i < list.size(); i++) {  
            getIsSelected().put(i, false);  
        }  
    }

在getView方法中,定義CheckBox的點擊方法,當CheckBox選中/不選中時設置isSelected的值為true/false;同時根據isSelected的值設置CheckBox是否選中,並顯示相應的圖標:

        mCheckbox = (CheckBox)convertView.findViewById(R.id.chkBox);
        mCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked){
                    buttonView.setButtonDrawable(R.drawable.r02);
                    isSelected.put(position, true);
                }else{
                    buttonView.setButtonDrawable(R.drawable.r01);
                    isSelected.put(position, false);
                }
            }            
        });
        mCheckbox.setChecked(getIsSelected().get(position));
        if (getIsSelected().get(position)){
            mCheckbox.setButtonDrawable(R.drawable.r02);
        }else{
            mCheckbox.setButtonDrawable(R.drawable.r01);
        }

在主窗體文件中,當點擊列表下面的CheckBox時,調用selectAll方法將isSelected的所有值設置為true/false,同時顯示相應的圖標:

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()){
        case R.id.chkSelect:
            selectAll(chkSelect.isChecked());
            break;
        case R.id.btnDelete:
            break;
        default:
            break;
        }
        
    }
    private void selectAll(boolean x){
        for (int i=0; i<mList.size(); i++){
            mAdapter.getIsSelected().put(i, x);
        }
        if (chkSelect.isChecked()){
            chkSelect.setButtonDrawable(R.drawable.r02);
        }else{
            chkSelect.setButtonDrawable(R.drawable.r01);
        }
        mAdapter.notifyDataSetChanged();
    }

完整的文件如下。

MainActivity.java:

package com.hzhi.mylistview;

import android.support.v7.app.ActionBarActivity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity implements OnClickListener{
    
    private CheckBox chkSelect;
    private Button btnDelete;
    private List<String> mList;
    private MyAdapter mAdapter;
    private ListView mListview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        initView();
        initData();
        mAdapter = new MyAdapter(MainActivity.this, mList);
        mListview.setAdapter(mAdapter);
    }
    
    private void initView(){
        chkSelect = (CheckBox) findViewById(R.id.chkSelect);
        btnDelete = (Button) findViewById(R.id.btnDelete);
        mListview = (ListView) findViewById(R.id.lstView);
        
        chkSelect.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        
        chkSelect.setButtonDrawable(R.drawable.r01);
    }
    
    private void initData(){
        mList = new ArrayList<String>();
        for (int i=0; i<10; i++){
            mList.add("第" + String.valueOf(i) + "條數據");
        }
    }
    
    private void selectAll(boolean x){
        for (int i=0; i<mList.size(); i++){
            mAdapter.getIsSelected().put(i, x);
        }
        if (chkSelect.isChecked()){
            chkSelect.setButtonDrawable(R.drawable.r02);
        }else{
            chkSelect.setButtonDrawable(R.drawable.r01);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()){
        case R.id.chkSelect:
            selectAll(chkSelect.isChecked());
            break;
        case R.id.btnDelete:
            break;
        default:
            break;
        }
        
    }

}

MyAdapter.java:

package com.hzhi.mylistview;

import java.util.HashMap;
import java.util.List;

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.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MyAdapter extends BaseAdapter{
    
    List<String> list;
    Context context;
    private static HashMap<Integer, Boolean> isSelected;
    
    public MyAdapter(Context context, List<String> list){
        this.context = context;
        this.list = list;        
        isSelected = new HashMap<Integer, Boolean>();
        initData();
    }
    
    private void initData() {  
        for (int i = 0; i < list.size(); i++) {  
            getIsSelected().put(i, false);  
        }  
    }

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

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(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
        CheckBox mCheckbox;
        TextView mText;
        
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.listitem, null);
        }
        mCheckbox = (CheckBox)convertView.findViewById(R.id.chkBox);
        mCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked){
                    buttonView.setButtonDrawable(R.drawable.r02);
                    isSelected.put(position, true);
                }else{
                    buttonView.setButtonDrawable(R.drawable.r01);
                    isSelected.put(position, false);
                }
            }            
        });
        mCheckbox.setChecked(getIsSelected().get(position));
        if (getIsSelected().get(position)){
            mCheckbox.setButtonDrawable(R.drawable.r02);
        }else{
            mCheckbox.setButtonDrawable(R.drawable.r01);
        }
        
        mText = (TextView)convertView.findViewById(R.id.txtText);
        mText.setText(list.get(position).toString());
        
        return convertView;
    }
    
    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {  
        MyAdapter.isSelected = isSelected;  
    }
    
    public static HashMap<Integer, Boolean> getIsSelected() {  
        return isSelected;  
    }

}

 運行效果如下:

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved