listview条目全选、取消、单选

activity_main.xml布局文件





    

item.xml布局文件


class Bean

public class Bean {

public String title;
public boolean sf;
}

Clean回调接口

public interface Clean {
public void cleanText();

}

listview适配器

package com.example.moreselect;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter{
private Clean clean;
private final Context context;
private final ArrayList list;

public MyAdapter(Context context, ArrayList list) {
    this.context = context;
    this.list = list;
}

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

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

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder=null;
    if(convertView==null){
        holder=new ViewHolder();
        convertView=View.inflate(context, R.layout.item, null);
        holder.title=(TextView) convertView.findViewById(R.id.item_text);
        holder.checkBox=(CheckBox) convertView.findViewById(R.id.item_checkbox);
        convertView.setTag(holder);

    }else{
        holder=(ViewHolder) convertView.getTag();
    }
    final CheckBox cb=holder.checkBox;
    cb.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(cb.isChecked()){
                cb.setChecked(true);
                list.get(position).sf=true;
            }else{
                list.get(position).sf=false;
                cb.setChecked(false);
            }
            clean.cleanText();//回调方法
        }
    });
    holder.title.setText(list.get(position).title);
    cb.setChecked(list.get(position).sf);
    return convertView;
}
class ViewHolder{
    TextView title;
    CheckBox checkBox;
}
public void setClean(Clean clean){
    this.clean=clean;
}

}

MainActivity

package com.example.moreselect;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

private Button main_button;
public  TextView main_textview;
private ListView main_listview;
private ArrayList list;
private Button main_allOrCancle;
private boolean tag=true;
private MyAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initData();//模拟数据
    initView();//初始化视图
    initEvent();//初始化事件
}

private void initEvent() {
    adapter = new MyAdapter(MainActivity.this,list);
    main_listview.setAdapter(adapter);
    main_button.setOnClickListener(this);//下载的点击事件
    main_allOrCancle.setOnClickListener(this);//全选或者取消
}

private void initData() {
    list = new ArrayList();
    for (int i = 0; i < 50; i++) {
        Bean b=new Bean();
        b.title="条目"+i+"-------------->";
        b.sf=false;
        list.add(b);
    }
}

private void initView() {
    main_button = (Button) findViewById(R.id.main_button);
    main_textview = (TextView) findViewById(R.id.main_textview);
    main_listview = (ListView) findViewById(R.id.main_listview);
    main_allOrCancle = (Button) findViewById(R.id.main_allOrCancle);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.main_button://下载
        donwload();
        break;
    case R.id.main_allOrCancle://全选or取消
        selectOrCancle();
        break;
    default:
        break;
    }
}








/**
 * 全选或者取消的放方法
 */
private void selectOrCancle() {
    if(tag){
        main_allOrCancle.setText("取消");
        tag=false;
        all();
        adapter.notifyDataSetChanged();
        main_textview.setText("");
    }else{
        main_allOrCancle.setText("全选");
        tag=true;
        cancle();
        adapter.notifyDataSetChanged();
    }

}





private void cancle() {//取消
    for (int i = 0; i < list.size(); i++) {
        list.get(i).sf=false;
    }
}

private void all() {//全选
    for (int i = 0; i < list.size(); i++) {
        list.get(i).sf=true;
    }
}

/**
 * 下载的方法
 */
private void donwload() {
    StringBuilder sb=new StringBuilder();
    sb.append("选择了:");
    int count=0;
    boolean yn=false;//是否有选中的框
    for (int j = 0; j < list.size(); j++) {
        if(list.get(j).sf){
            yn=true;
            sb.append(list.get(j).title+"->");
            count++;
        }
    }
    if(yn){//如果有选中的
        sb.append("共计:"+count+"个");
        String str = sb.toString();
        main_textview.setText(str);
        main_allOrCancle.setText("全选");
        tag=true;
        cancle();
        adapter.notifyDataSetChanged();
        adapter.setClean(new Clean() {//回调  点击框用来展示下载信息的  main_textview消息清空

            @Override
            public void cleanText() {
                // TODO Auto-generated method stub
                main_textview.setText("");
            }
        });
    }else{
        Toast.makeText(MainActivity.this, "请选择", 0).show();
    }
}

}

你可能感兴趣的:(listview条目全选、取消、单选)