当前位置:操作系统 > 安卓/Android >>

listview与checkbox联合使用

效果图如下:\
 

 源码如下:

MainActivity.java:

 

[java] 
package com.google.activity; 
 
import com.google.R; 
import com.google.adapter.MainAdapter; 
 
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; 
 
public class MainActivity extends Activity implements OnClickListener{   
     
    private ListView listView; 
    private Button btnSelectAll, btnUnSelectAll; 
     
    private MainAdapter adapter; 
     
    @Override 
    public void onCreate(Bundle b){ 
        super.onCreate(b); 
        setContentView(R.layout.main); 
         
        init(); 
        initAdapter(); 
        addAdapter(); 
        addListener(); 
         
    } 
     
    private void init(){ 
        listView = (ListView)this.findViewById(R.id.listview_main); 
        btnSelectAll = (Button)this.findViewById(R.id.button_main_selectall); 
        btnUnSelectAll = (Button)this.findViewById(R.id.button_main_unselectall); 
    } 
     
    private void initAdapter(){ 
        adapter = new MainAdapter(this); 
    } 
     
    private void addAdapter(){ 
        listView.setAdapter(adapter); 
    } 
     
    private void addListener(){ 
        btnSelectAll.setOnClickListener(this); 
        btnUnSelectAll.setOnClickListener(this); 
    } 
 
    @Override 
    public void onClick(View v) { 
        switch(v.getId()){ 
        case R.id.button_main_selectall: 
            adapter.setListCheckStatus(true); 
            adapter.notifyDataSetChanged(); 
            break; 
        case R.id.button_main_unselectall: 
            adapter.setListCheckStatus(false); 
            adapter.notifyDataSetChanged(); 
            break; 
         
        } 
             
         
    } 
 

package com.google.activity;

import com.google.R;
import com.google.adapter.MainAdapter;

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;

public class MainActivity extends Activity implements OnClickListener{ 
 
 private ListView listView;
 private Button btnSelectAll, btnUnSelectAll;
 
 private MainAdapter adapter;
 
 @Override
 public void onCreate(Bundle b){
  super.onCreate(b);
  setContentView(R.layout.main);
  
  init();
  initAdapter();
  addAdapter();
  addListener();
  
 }
 
 private void init(){
  listView = (ListView)this.findViewById(R.id.listview_main);
  btnSelectAll = (Button)this.findViewById(R.id.button_main_selectall);
  btnUnSelectAll = (Button)this.findViewById(R.id.button_main_unselectall);
 }
 
 private void initAdapter(){
  adapter = new MainAdapter(this);
 }
 
 private void addAdapter(){
  listView.setAdapter(adapter);
 }
 
 private void addListener(){
  btnSelectAll.setOnClickListener(this);
  btnUnSelectAll.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  switch(v.getId()){
  case R.id.button_main_selectall:
   adapter.setListCheckStatus(true);
   adapter.notifyDataSetChanged();
   break;
  case R.id.button_main_unselectall:
   adapter.setListCheckStatus(false);
   adapter.notifyDataSetChanged();
   break;
  
  }
   
  
 }

}

 


MainAdapter.java:

 

[java] 
package com.google.adapter; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import com.google.R; 
 
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.CompoundButton.OnCheckedChangeListener; 
import android.widget.ImageView; 
import android.widget.TextView; 
 
public class MainAdapter extends BaseAdapter { 
     
    public Context context; 
    private LayoutInflater layoutInflater; 
     
    private int listSize = 21; 
    private List<Boolean> listCheckStatus; 
     
     
    public MainAdapter(Context context){ 
        this.context = context; 
        layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,