如何在ListView中添加CheckBox

如何在ListView中添加CheckBox,第1张

package com.billy.demo  

  

import java.util.ArrayList  

import java.util.List  

  

import android.app.ListActivity  

import android.content.Context  

import android.os.Bundle  

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 TestListViewAndCheckbox extends ListActivity {  

    /** Called when the activity is first created. */  

    Context context = null  

    @Override  

    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState)  

        setContentView(R.layout.main)  

        context = getApplicationContext()  

        setListAdapter(new MyListAdapter())  

    }  

      

    class MyListAdapter extends BaseAdapter{  

        String data[] = new String[]{"apple", "pear", "banana", "orange","apple", "pear", "banana", "orange","apple", "pear", "banana", "orange"}  

        List<Integer> checkPosition = new ArrayList<Integer>(data.length)  

        @Override  

        public int getCount() {  

            // TODO Auto-generated method stub  

            return data.length  

        }  

  

        @Override  

        public Object getItem(int position) {  

            // TODO Auto-generated method stub  

            return data[position]  

        }  

  

        @Override  

        public long getItemId(int position) {  

            // TODO Auto-generated method stub  

            return position  

        }  

  

        @Override  

        public View getView(int position, View convertView, ViewGroup parent) {  

            // TODO Auto-generated method stub  

            if (null == convertView){  

                LayoutInflater inflater      = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)  

                convertView = inflater.inflate(R.layout.list_item, parent, false)  

            }  

            TextView text       =   (TextView)convertView.findViewById(R.id.info)  

            final CheckBox checkbox   =   (CheckBox)convertView.findViewById(R.id.checkstatus)  

            checkbox.setTag(new Integer(position))  

            text.setText(data[position])  

            if (checkPosition != null){  

                checkbox.setChecked((checkPosition.contains(new Integer(position)) ? true : false))   

            }else{  

                checkbox.setChecked(false)   

            }  

              

            checkbox  

            .setOnCheckedChangeListener(new OnCheckedChangeListener() {  

  

                @Override  

                public void onCheckedChanged(CompoundButton buttonView,  

                        boolean isChecked) {  

                    // TODO Auto-generated method stub  

                    if (isChecked){  

                        if (!checkPosition.contains(checkbox.getTag())){  

                            checkPosition.add((Integer)checkbox.getTag())  

                        }  

                    }else{  

                        if (checkPosition.contains(checkbox.getTag())){  

                            checkPosition.remove(checkbox.getTag())  

                        }  

                    }  

                }  

            })  

  

            return convertView  

        }  

          

    }  

}

string[] list = new string[] { "张三", "李四", "王五" }

int x = 0, y = 0

foreach (string item in list)

{

      CheckBox cb = new CheckBox()

      cb.Text = item

      cb.Location = new Point(x, y)

      clbInvisibleColumn.Controls.Add(cb)

      y += 22

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11407313.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存