我想知道我是否可以从ListView检查项目(复选框)并使用所选项目。但我不想使用'ListView.OnItemClickListener',因为我有一个单独的功能来单击列表项。像'CheckBox.OnClickListener'之类的东西,我可以抓住所有被检查的列表项。任何帮助都非常感谢。下面是我目前的代码,但不起作用:
public class MainActivity extends AppCompatActivity {
ListView list;
GoodAdapter imageAdapter;
CheckBox cb;
@Override
protected void onCreate(Bundle savedInstanceState) {
//populates the details as in imageBeanArray
imageAdapter = new GoodAdapter(this, imageBeanArray);
list.setAdapter(imageAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
final int position, long id) {
//does something
}
//throws NPE
cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cb.isSelected()) {
checkedArray.add((ImageBean) list.getItemAtPosition(list.getSelectedItemPosition()));
}
}
});
}
GoodAdapter.java: 所有其他变量初始化
public class GoodAdapter extends BaseAdapter{
boolean[] itemChecked;
CheckBox[] checkBoxArray;
public GoodAdapter(Context context, ArrayList<ImageBean> imageBean) {
mInflater = LayoutInflater.from(context);
mImageBeans = imageBean;
itemChecked=new boolean[imageBean.size()];
checkBoxArray=new CheckBox[itemChecked.length];
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null) {
view = mInflater.inflate(R.layout.good_adapter, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView)view.findViewById(R.id.image);
holder.name = (TextView)view.findViewById(R.id.docName);
holder.imageMore = (TextView)view.findViewById(R.id.imageMore);
holder.imageMore.setVisibility(View.GONE);
holder.checkbox=(CheckBox)view.findViewById(R.id.checkBox);
view.setTag(holder);
} else {
view = convertView;
holder = (ViewHolder) view.getTag();
holder.checkbox.setChecked(false);
if (itemChecked[position]){
holder.checkbox.setChecked(true);
mImageBeans.get(position).setSelected(true);
}
else
holder.checkbox.setChecked(false);
}
ImageBean imageBean = mImageBeans.get(position);
Bitmap bm = BitmapFactory.decodeByteArray(imageBean.getImage(), 0, imageBean.getImage().length);
holder.imageView.setImageBitmap(bm);
holder.name.setText(imageBean.getDocName());
if (imageBean.isMoreThanOne()!=0)
holder.imageMore.setVisibility(View.VISIBLE);
holder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.checkbox.isChecked())
itemChecked[position] = true;
else
itemChecked[position] = false;
}
});
checkBoxArray[position].setChecked(itemChecked[position]);
checkBoxArray[position].setOnCheckedChangeListener(mListener);
return view;
}
CompoundButton.OnCheckedChangeListener mListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
itemChecked[(Integer)buttonView.getTag()] = isChecked; // get the tag so we know the row and store the status
}
};
private class ViewHolder {
public ImageView imageView;
public TextView name,imageMore;
public CheckBox checkbox;
}
Good Adapter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="64dip"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:weightSum="1"
android:id="@+id/imageList">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<ImageView
android:id="@+id/image"
android:scaleType="centerCrop"
android:layout_marginLeft="2dp"
android:layout_gravity="center_vertical"
android:layout_width="90dip"
android:layout_height="match_parent"
android:background="@drawable/image_border"
android:padding="4dp"
android:cropToPadding="true"
/>
<TextView
android:id="@+id/docName"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:textColor="@color/black"
android:textSize="20dp"
android:gravity="center_vertical"/>
<TextView
android:id="@+id/imageMore"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="5dp"
android:background="@drawable/more"/>
</LinearLayout>
请帮助我。我没有找到任何有用的教程/解决方案。
答案 0 :(得分:0)
根据我的经验,你不能在行的组件上有一个监听器,在行本身上有一个监听器,如果你这样做,那么就会利用另一个永远不会被调用的东西。