2011.04.15——— android listView带checkbox

用带checkbox的listview时,总是会出现选择的项 会重复,会自己多选几项等问题

整个类的xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"   android:orientation="vertical"  >    <ListView    android:layout_width="wrap_content"     android:layout_height="420dip"     android:id="@+id/msg_listView" /><LinearLayout                 android:orientation="horizontal"                android:layout_width="fill_parent"                android:layout_height="80px"                android:layout_gravity="bottom"                android:background="#555555"                android:gravity="center_horizontal"                >   <Button   android:layout_width="wrap_content"   android:layout_height="fill_parent"     android:text="删除"     android:textSize="18sp"    android:id="@+id/msg_del"      /></LinearLayout></LinearLayout>



listview的xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="horizontal"  >     <CheckBox android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:id="@+id/msg_checkBox"    android:focusable="false"   android:paddingLeft="15dip"   />  <ImageView  android:layout_width="50dip"  android:layout_height="wrap_content"  android:id="@+id/msg_img"  android:paddingTop="5px"  android:layout_marginTop="20px"  />  <LinearLayout  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:orientation="vertical"  >   <TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content" android:id="@+id/msg_zt"  android:textSize="18sp"  />  <LinearLayout  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:orientation="horizontal"  >   <TextView  android:layout_width="50dip"  android:layout_height="30dip" android:id="@+id/msg_fjr"   />    <TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content" android:id="@+id/msg_sj"   />  </LinearLayout>  </LinearLayout></LinearLayout>


一个重要的pojo类:
package com.huitu.pojo;import java.util.Date;public class GC_MOBILE_MSG {private Integer MSGID;//主键ID(序列SEQ_GC_MOBILE_MSG)private int RECIVE_USERID;//接收人IDprivate String SEND_TIME;//Y发送时间private int SEND_USERID;//发送人IDprivate String MSG_CONTENT;//消息内容private int STATE;//状态private int XCJL_ID;//巡查记录IDprivate String SEND_USERNAME;//发送人private boolean cCheck;private int rn;public boolean iscCheck() {return cCheck;}public void setcCheck(boolean cCheck) {this.cCheck = cCheck;}public Integer getMSGID() {return MSGID;}public void setMSGID(Integer msgid) {MSGID = msgid;}public int getRECIVE_USERID() {return RECIVE_USERID;}public void setRECIVE_USERID(int recive_userid) {RECIVE_USERID = recive_userid;}public String getSEND_TIME() {return SEND_TIME;}public void setSEND_TIME(String send_time) {SEND_TIME = send_time;}public int getSEND_USERID() {return SEND_USERID;}public void setSEND_USERID(int send_userid) {SEND_USERID = send_userid;}public String getMSG_CONTENT() {return MSG_CONTENT;}public void setMSG_CONTENT(String msg_content) {MSG_CONTENT = msg_content;}public int getSTATE() {return STATE;}public void setSTATE(int state) {STATE = state;}public int getXCJL_ID() {return XCJL_ID;}public void setXCJL_ID(int xcjl_id) {XCJL_ID = xcjl_id;}public String getSEND_USERNAME() {return SEND_USERNAME;}public void setSEND_USERNAME(String send_username) {SEND_USERNAME = send_username;}public int getRn() {return rn;}public void setRn(int rn) {this.rn = rn;}}




activity代码:

package com.huitu.project;import java.io.File;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.app.AlertDialog;import android.app.ProgressDialog;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;import com.huitu.pojo.GC_MOBILE_MSG;import com.huitu.service.GC_MOBILE_MSGService;import com.huitu.util.JSONUtil;public class GC_MOBILE_MSGActivity extends Activity{private ListView lv;private Button bn_del;private List<GC_MOBILE_MSG> list;private MyAdapter adapter;private ProgressDialog pd;private Handler mProgressHandler;private MyThread mt ;private String user_id;private int pageNow = 1;private int temp;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.msg);Intent intent = getIntent();user_id = intent.getStringExtra("user_id");pd = new ProgressDialog(this);pd.setIndeterminate(true);pd.setMessage("加载数据...");pd.setCancelable(true);pd.show();mProgressHandler = new Handler() {            @Override            public void handleMessage(Message msg) {            int length = msg.getData().getInt("length");            switch (length){            case 1 :            adapter.notifyDataSetChanged();            pd.dismiss();            break;            case 2 :            lv.setAdapter(adapter);            lv.setSelection(5 * (pageNow-1));            lv.setOnItemClickListener(new OnItemClickListener(){public void onItemClick(AdapterView<?> parent,View view, int position, long id) {Intent i = new Intent(GC_MOBILE_MSGActivity.this,XCJLAllItem_for_MSGActivity.class);Bundle b = new Bundle();b.putInt("id", list.get(position).getXCJL_ID());b.putInt("MSGID", list.get(position).getMSGID());b.putInt("state", list.get(position).getSTATE());i.putExtras(b);startActivity(i);}                        });        pageNow++;        pd.dismiss();        break;            case 3 :            pd.dismiss();            new AlertDialog.Builder(GC_MOBILE_MSGActivity.this)    .setMessage("网络异常!")           .setCancelable(false)           .setPositiveButton("确定", new DialogInterface.OnClickListener() {               public void onClick(DialogInterface dialog, int id) {               //finish();               }           }).create().show();    break;            }                        }        };lv = (ListView)findViewById(R.id.msg_listView);bn_del = (Button)findViewById(R.id.msg_del);bn_del.setOnClickListener(new OnClickListener(){public void onClick(View v) {// TODO Auto-generated method stubfinal List<Integer> l = new ArrayList<Integer>();for(GC_MOBILE_MSG bean : list){if(bean.iscCheck()){l.add(bean.getMSGID());}}new AlertDialog.Builder(GC_MOBILE_MSGActivity.this).setMessage("确定删除吗?").setNegativeButton("取消",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int which) {}}).setPositiveButton("确定",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int whichButton) {pd.show();new Thread(new Runnable() {public void run() {// TODO Auto-generated method stubtry {GC_MOBILE_MSGService.delete(l);pageNow = 1;init(0);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();Message msg = new Message();msg.getData().putInt("length", 3);mProgressHandler.sendMessage(msg);}}}).start();}}).show();}});mt = new MyThread(0);mt.start();adapter = new MyAdapter(this);}public void init(int temp) throws Exception{String json = GC_MOBILE_MSGService.query(user_id, pageNow);list = JSONUtil.parseJSON_GC_MOBILE_MSG(json);if(temp==1){Message msg = new Message();msg.getData().putInt("length", 1);mProgressHandler.sendMessage(msg);}else{Message msg = new Message();msg.getData().putInt("length", 2);mProgressHandler.sendMessage(msg);}}public final class ViewHolder {public TextView fjr;public TextView zt;public TextView sj;public ImageView xin;public CheckBox chenk_box;}public class MyAdapter extends BaseAdapter {private LayoutInflater mInflater;public MyAdapter(Context context) {this.mInflater = LayoutInflater.from(context);}public int getCount() {// TODO Auto-generated method stubreturn list.size();}public Object getItem(int arg0) {// TODO Auto-generated method stubreturn null;}public long getItemId(int arg0) {// TODO Auto-generated method stubreturn 0;}public View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder = null;if (convertView == null) {holder = new ViewHolder();convertView = mInflater.inflate(R.layout.msg_item, null);holder.fjr = (TextView) convertView.findViewById(R.id.msg_fjr);holder.zt = (TextView) convertView.findViewById(R.id.msg_zt);holder.sj = (TextView) convertView.findViewById(R.id.msg_sj);holder.xin = (ImageView) convertView.findViewById(R.id.msg_img);holder.chenk_box = (CheckBox) convertView.findViewById(R.id.msg_checkBox);System.out.println("position" + position);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();} holder.chenk_box.setId(position + 100);holder.chenk_box.setChecked(list.get(position).iscCheck());holder.chenk_box.setOnCheckedChangeListener(new OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {int id = buttonView.getId() - 100;list.get(id).setcCheck(isChecked);//adapter.notifyDataSetChanged();lv.setAdapter(adapter);}});holder.fjr.setText(list.get(position).getSEND_USERNAME());holder.zt.setText( list.get(position).getMSG_CONTENT());holder.sj.setText(list.get(position).getSEND_TIME());if(list.get(position).getSTATE()==0){holder.xin.setImageResource(R.drawable.icon_email);}return convertView;}} private class MyThread extends Thread {public MyThread(int t){temp = t;}public void run(){Message msg = new Message();try {init( temp);} catch (Exception e) {e.printStackTrace();msg.getData().putInt("length", 3);mProgressHandler.sendMessage(msg);return;}}}@Overrideprotected void onRestart() {// TODO Auto-generated method stubpageNow = 1;mt = new MyThread(0);mt.start();super.onRestart();} }



这个activity的作用 就是展示一个带checkbox的listview,并且没一条记录前面会显示出“new”的图片 点击后 就不再显示此图片
并接选择checkbox后 可以删除该记录









更多相关文章

  1. android:drawSelectorOnTop含义
  2. 天天记录 - Android创建应用窗口分析2
  3. GridView属性集合 drawSelectorOnTop
  4. Android(安卓)资源记录
  5. android数据库使用小结
  6. Android(安卓)SSL 过程记录(证书的生成+例子代码)
  7. 记录关于Gradle : Build Running的解决方法
  8. Android联系人表结
  9. CactiPhone: 移动设备上查看Cacti

随机推荐

  1. 时间戳转换
  2. jenkins学习笔记---'android' 不是内部或
  3. 【FF7】关于多成Layer嵌套的问题
  4. Android(安卓)Studio添加volley以及volle
  5. Android(安卓)context空指针异常
  6. android 底部tab
  7. 【高通SDM660平台 Android(安卓)10.0】(1
  8. Android(安卓)两种串口实现方法总结
  9. Android(安卓)8.0系统以后你该这样启动Se
  10. View点击涟漪效果