我有一个名为“SelectUserAdapter”的自定义适配器类,用于通过电话联系人,图片,电话号码和复选框对列表视图进行充气。问题是我无法选中或取消选中该复选框。 这是代码:
package com.trendsetter.vyshnav.vyshnavgetcontact;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.trendsetter.vyshnav.vyshnavgetcontact.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
class SelectUserAdapter extends BaseAdapter {
public List<SelectUser> _data;
private ArrayList<SelectUser> arraylist;
Context _c;
ViewHolder v;
public SelectUserAdapter( Context context, List<SelectUser> selectUsers)
{
_data = selectUsers;
_c = context;
this.arraylist = new ArrayList<SelectUser>();
this.arraylist.addAll(_data);
}
@Override
public int getCount() {
return _data.size();
}
@Override
public Object getItem(int i) {
return _data.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view = convertView;
if (view == null) {
LayoutInflater li = (LayoutInflater)
_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.contact_info, null);
Log.e("Inside", "here--------------------------- In view1");
} else {
view = convertView;
Log.e("Inside", "here--------------------------- In view2");
}
v = new ViewHolder();
v.title = (TextView) view.findViewById(R.id.name);
v.check = (CheckBox) view.findViewById(R.id.check);
v.phone = (TextView) view.findViewById(R.id.no);
v.imageView = (ImageView) view.findViewById(R.id.pic);
final SelectUser data = (SelectUser) _data.get(i);
v.title.setText(data.getName());
v.check.setChecked(data.getCheckedBox());
v.phone.setText(data.getPhone());
// Set image if exists
try {
if (data.getThumb() != null) {
v.imageView.setImageBitmap(data.getThumb());
} else {
v.imageView.setImageResource(R.drawable.image);
}
// Setting round image
Bitmap bm = BitmapFactory.decodeResource(view.getResources(),
R.drawable.image); // Load default image
v.imageView.setImageBitmap(bm);
} catch (OutOfMemoryError e) {
// Add default picture
v.imageView.setImageDrawable(this._c.getDrawable(R.drawable.image));
e.printStackTrace();
}
Log.e("Image Thumb", "--------------" + data.getThumb());
// Set check box listener android
v.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox checkBox = (CheckBox) view;
if (checkBox.isChecked()) {
data.setCheckedBox(true);
} else {
data.setCheckedBox(false);
}
}
});
view.setTag(data);
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
_data.clear();
if (charText.length() == 0) {
_data.addAll(arraylist);
} else {
for (SelectUser wp : arraylist) {
if (wp.getName().toLowerCase(Locale.getDefault())
.contains(charText)) {
_data.add(wp);
}
}
}
notifyDataSetChanged();
}
static class ViewHolder {
ImageView imageView;
TextView title, phone;
CheckBox check;
}
}
自定义行xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="5dp"
android:weightSum="3">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/pic"
android:src="@drawable/image" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="2.6"
android:layout_margin="5dp"
android:paddingLeft="10dp"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:text="User name"
android:textSize="18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/no"
android:layout_marginTop="10dp"
android:text="Phone no."
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/check" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
getset class:
package com.trendsetter.vyshnav.vyshnavgetcontact;
import android.graphics.Bitmap;
class SelectUser {
String name;
public Bitmap getThumb() {
return thumb;
}
public void setThumb(Bitmap thumb) {
this.thumb = thumb;
}
Bitmap thumb;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
String phone;
public Boolean getCheckedBox() {
return checkedBox;
}
public void setCheckedBox(Boolean checkedBox) {
this.checkedBox = checkedBox;
}
Boolean checkedBox = false;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
如何成功选中复选框?
答案 0 :(得分:3)
我认为您的问题是当您点击checkbox
时......
你必须在v.check.setOnclick
中改变你的身体方法,如下所示:
v.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox checkBox = (CheckBox) view;
if (checkBox.isChecked()) {
((SelectUser) _data.get(i)).setCheckedBox(true);
} else {
((SelectUser) _data.get(i)).setCheckedBox(false);
}
notifyDatasetChanged();
}
});
答案 1 :(得分:0)
在您的xml文件中,您只需在父布局中添加一行,即
android:descendantFocusability=”blocksDescendants”
您也可以参考以下链接:
http://howcanicode.com/listview-row-not-getting-focused-or-selected-while-placing-checkbox-on-it/