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.DialogInterface;
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
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
System.out.println("第"+arg2+"項");
new AlertDialog.Builder(this).setIcon(R.drawable.ic_about)
.setTitle("hello:")
.setMessage(datalistm.get(arg2).getCbdata())
.setNegativeButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
}).show();
}
}