这是我的类
public class AdapterContact extends BaseAdapter {
private Context context;
private ArrayList<Datamodel> arrModel;
public ArrayList<Datamodel> arrSelectedModel;
// private ArrayList<Boolean> arrCheckboxState;
ViewHolder holder;
ImageLoader imageloader;
public AdapterContact(Context context, ArrayList<Datamodel> arrModel) {
this.context = context;
this.arrModel = arrModel;
arrSelectedModel = new ArrayList<Datamodel>();
imageloader = new ImageLoader(context);
}
@Override
public int getCount() {
return arrModel.size();
}
@Override
public Object getItem(int position) {
return arrModel.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row_contact, null);
holder.checkBox = (CheckBox) convertView
.findViewById(R.id.rowcontact_checkbox);
holder.txtContactName = (TextView) convertView
.findViewById(R.id.rowcontact_txtName);
holder.imgProfilepic = (ImageView) convertView
.findViewById(R.id.rowcontact_imgProfile);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtContactName.setText(arrModel.get(position).contactName);
holder.checkBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int position = (Integer) buttonView.getTag();
if (isChecked) {
// arrCheckboxState.set(position, true);
arrModel.get(position).setChecked(true);
arrSelectedModel.add(arrModel.get(position));
} else {
arrModel.get(position).setChecked(false);
// arrCheckboxState.set(position, false);
arrSelectedModel.remove(arrModel.get(position));
}
}
});
holder.checkBox.setTag(position);
holder.checkBox.setChecked(arrModel.get(position).isChecked);
if (arrModel.get(position).isActive) {
holder.checkBox.setVisibility(View.GONE);
}
AQuery aq = new AQuery(context);
aq.id(holder.imgProfilepic).image(arrModel.get(position).picturePath,
false, false, 0, R.drawable.imge);
String url2 = arrModel.get(position).picturePath + "?t="
+ System.currentTimeMillis();
aq.id(holder.imgProfilepic).image(url2, false, false, 0,
R.drawable.imge);
convertView.setBackgroundColor(Color.parseColor("#ffffff"));
return convertView;
}
class ViewHolder {
TextView txtContactName;
ImageView imgProfilepic;
CheckBox checkBox;
}
}
这是我的 XMl :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dip"
android:background="#ffffff"
android:orientation="vertical" >
<com.lociiapp.utils.RoundedImageView
android:id="@+id/rowcontact_imgProfile"
android:layout_width="46dip"
android:layout_height="46dip"
android:layout_centerVertical="true"
android:layout_marginBottom="7dip"
android:layout_marginLeft="7dip"
android:layout_marginTop="7dip" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dip"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/rowcontact_imgIcon"
android:layout_toRightOf="@+id/rowcontact_imgProfile"
android:orientation="vertical" >
<TextView
android:id="@+id/rowcontact_txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginTop="17dip"
android:text="Usama Sadiq"
android:textColor="#3c3b3b"
android:textSize="17dip" />
</RelativeLayout>
<CheckBox
android:id="@+id/rowcontact_checkbox"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:button="@drawable/ic_launcher" />
</RelativeLayout>
我正在列表视图中打印数据,当项目未选中或dFault时,复选框选择,然后复选框背景图像将x,如果我们选中或启用任何项目的复选框,则背景图像应该更改我绑定到XML中的dFault图像,并且在检查后背景没有更改请检查代码我做错了。 复选框rowcontact_checkbox ID请建议我
使用此复选框选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checkbox_checked" android:state_checked="true"/>
<!-- checked -->
<item android:drawable="@drawable/checkbox_unchecked" android:state_checked="false"/>
<!-- unchecked -->
</selector>
并为UR复选框添加这些行
android:background="@drawable/checkbox_selector"
android:button="@android:color/transparent"
在 ListView 中使用此代码,而不是在适配器中
lstview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterview, View view,
int position, long id) {
CheckBox chkbox = (CheckBox) view
.findViewById(R.id.chkboxid);
chkbox.performClick();
}
});
将复选框背景设置为selector
,将其放在drawable/your_background.xml
下。selector
将决定选中/取消选中/禁用复选框等时要显示的背景...您可以尝试android-holo-colors
快速生成一个
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/your_checked_background" />
<item android:drawable="@drawable/your_unchecked_background" />
</selector>