如何在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

}

}

}

如果你是手动加载listView 的ListItem,就可以在加载时设置你要加载的ListItem,如: ListViewItem week1 = new ListViewItem()

week1.Text = "星期一"

week1.Checked = true

listView1.Items.Add(week1)

而如果你是在Form中直接通过编辑项添加,就可以设置你添加的ListItem的Checked属性为True即可!

我很少写具体的对象应用心得,这次尝试一下。

WinForm中Listview的ItemCheck事件,例子如下:

private

void lvwTables_ItemCheck(object sender, ItemCheckEventArgs

e)

{

Check()

}

private void Check()

{

for (int i = 0

i <lvwTables.CheckedItems.Counti++)

{

//Some biz here

//这里检测不到选中的项目的正确状态

}

}

因为ItemCheck事件是在Check的一刻触发的,lvwTables.CheckItems中还没有添加。

我这样处理:

private

void lvwTables_ItemCheck(object sender, ItemCheckEventArgs

e)

{

Check(e.Index, e.NewValue)

}

private void

Check(int CurrentItem, CheckState State)

{

for (int i = 0i <

lvwTables.CheckedItems.Counti++)

{

if (i !=

CurrentItem)

//some biz here //能正确检测

}

if (CurrentItem != -1

&&State == CheckState.Checked)

//some biz here

//这里补一下

}

为什么不直接在事件中biz?因为我有很多个地方要调用一样的代码,所以封装成函数。

方法有点笨。

转载仅供参考,版权属于原作者。祝你愉快,满意请采纳哦


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

原文地址: http://outofmemory.cn/bake/7925355.html

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

发表评论

登录后才能评论

评论列表(0条)

保存