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

ListView嵌套CheckBox (解决滑动时重复选择的问题)

ListView嵌套CheckBox (解决滑动时重复选择的问题)
adapter中做处理:
package com.chenglang.adapter;
 
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
 
 
import com.chenglang.bean.MytxtBean;
import com.chenglang.work.MytxtActivity;
import com.chenglang.work.R;
 
 
import android.content.Context;
import android.view.LayoutInflater;
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 Mytxtl_list_adapter extends BaseAdapter {
 
 
Context context;
List<MytxtBean> list = new ArrayList<MytxtBean>();
 
 
public Mytxtl_list_adapter(Context context, List<MytxtBean> list) {
// TODO Auto-generated constructor stub
this.context = context;
this.list = list;
 
}
 
 
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
 
 
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
 
 
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
 
 
Gridholder gridholder = new Gridholder();
 
 
@Override
public View getView( final int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
 
 
if (arg1 == null) {
arg1 = LayoutInflater.from(context).inflate(
R.layout.mytext_list_item_view, null);
arg1.setTag(gridholder);
} else {
gridholder = (Gridholder) arg1.getTag();
}
 
gridholder.cb = (CheckBox) arg1.findViewById(R.id.cb);
gridholder.cbtv = (TextView) arg1.findViewById(R.id.cbtv);
gridholder.dingao = (TextView) arg1.findViewById(R.id.daidinggao);
gridholder.person = (TextView) arg1.findViewById(R.id.person);
gridholder.leixin = (TextView) arg1.findViewById(R.id.putong);
gridholder.time = (TextView) arg1.findViewById(R.id.time);
MytxtBean bean = list.get(arg0);
 
gridholder.cbtv.setText(bean.getCbdata());
gridholder.dingao.setText(bean.getDingaodata());
gridholder.leixin.setText(bean.getLeixingdata());
gridholder.person.setText(bean.getPersondata());
gridholder.time.setText(bean.getTimedata());
 
if(MytxtActivity.checkPos.get(arg0)){
gridholder.cb.setChecked(true);
}else{
gridholder.cb.setChecked(false);
}
 
return arg1;
}
 
 
public static class Gridholder {
TextView dingao, leixin, person, time,cbtv;
public CheckBox cb;
}
 
 
}
 
在主代码中:
package com.chenglang.work;
 
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
 
import com.chenglang.adapter.Mytxtl_list_adapter;
import com.chenglang.adapter.Mytxtl_list_adapter.Gridholder;
import com.chenglang.bean.MytxtBean;
import com.chenglang.resource.MytxtData;
 
 
import android.animation.ArgbEvaluator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInte易做图ce;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
 
 
public class MytxtActivity extends Activity implements OnClickListener,
OnItemClickListener {
ListView mytextlist;
List<MytxtBean> datalistm = new ArrayList<MytxtBean>();
MytxtBean bean;
ImageButton backbtn;
Mytxtl_list_adapter myadapter;
TextView caogaotv,searchtv;
public static Map<Integer, Boolean> checkPos;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mytext_view);
init();
}
 
 
private void init() {
// TODO Auto-generated method stub
 
 
caogaotv = (TextView) findViewById(R.id.caogaotv);
searchtv = (TextView) findViewById(R.id.searchtv);
 
mytextlist = (ListView) findViewById(R.id.mytext_list);
setlistdata();
backbtn = (ImageButton) findViewById(R.id.mytextback);
backbtn.setOnClickListener(this);
caogaotv.setOnClickListener(this);
searchtv.setOnClickListener(this);
 
checkPos = new HashMap<Integer, Boolean>();
for (int i = 0; i < datalistm.size(); i++) {
checkPos.put(i, false);
}
}
 
 
private void setlistdata() {
for (int i = 9; i < 15; i++) {
 
bean = new MytxtBean(MytxtData.CBDATA[0], MytxtData.DINGGAODATA[0],
MytxtData.LEIXINGDATA[0], MytxtData.PERSON[0],
MytxtData.TIME[0]);
datalistm.add(bean);
}
myadapter = new Mytxtl_list_adapter(MytxtActivity.this, datalistm);
mytextlist.setAdapter(myadapter);
 
 
mytextlist.setItemsCanFocus(true);
mytextlist.setOnItemClickListener(this);
 
 
 
}
 
 
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
 
 
switch (arg0.getId()) {
case R.id.mytextback:
Intent intent = new Intent(MytxtActivity.this, MainActivity.class);
startActivity(intent);
finish();
break;
case R.id.caogaotv:
case R.id.searchtv:
 
Toast.makeText(MytxtActivity.this, "没有提供界面", Toast.LENGTH_SHORT).show();
break;
 
 
}
 
 
}
 
 
@Override补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,