2011.04.15——— android listView带checkbox

2011.04.15——— android listView带checkbox

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

[b]整个类的xml:[/b]
<?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>



[b]listview的xml[/b]

<?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>


[b]一个重要的pojo类:[/b]
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;// 接收人ID
private String SEND_TIME;// Y 发送时间
private int SEND_USERID;// 发送人ID
private String MSG_CONTENT;// 消息内容
private int STATE;// 状态
private int XCJL_ID;// 巡查记录ID
private 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;
}



}



[b]activity代码:[/b]

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;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.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 stub
final 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 stub
try {
GC_MOBILE_MSGService.delete(l);
pageNow = 1;
init(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.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 stub
return list.size();
}

public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 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;
}

}
}

@Override
protected void onRestart() {
// TODO Auto-generated method stub
pageNow = 1;
mt = new MyThread(0);
mt.start();
super.onRestart();
}



}



[color=red]这个activity的作用 就是展示一个带checkbox的listview,并且没一条记录前面会显示出“new”的图片 点击后 就不再显示此图片
并接选择checkbox后 可以删除该记录[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值