我有listview,其中包含checkbox和imageview。问题是当我点击listView右边的复选框没有选中但是随机选中了不同的复选框并且还检查了mulptiple复选框..
cell.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView android:layout_width="wrap_content" android:src="@drawable/icon"
android:id="@+id/imageView1" android:layout_height="wrap_content"/>
<CheckBox android:id="@+id/checkBox1"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:textSize="30dp" android:padding="2dp"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:focusable="false"
></CheckBox>
</LinearLayout>
list.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">
<Button android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="saveSetting"
android:text="Save"/>
<ListView android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
/>
</LinearLayout>
MainActivity.java
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
String[] string = { "First", "Second", "third", "Forth", "Fifth", "Sixth",
"Seven", "Eight" };
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.list);
List<String> arrayList = new ArrayList<String>();
for (String s : string) {
arrayList.add(s);
}
ListView listview = (ListView) findViewById(R.id.list);
final CustomAdapter ada = new CustomAdapter(arrayList, this);
listview.setAdapter(ada);
// listview.setItemsCanFocus(true);
listview.setChoiceMode(0);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
ada.setPosition(arg2);
}
});
}
class CustomAdapter extends BaseAdapter {
private List<String> items;
private LayoutInflater inflater;
int selectedPos = -1;
public CustomAdapter(List<String> list, Context context) {
super();
this.items = list;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
public void setPosition(int position) {
selectedPos = position;
notifyDataSetChanged();
}
public int getPosition() {
return selectedPos;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.cell, null);
holder = new ViewHolder();
holder.cBox = (CheckBox) convertView
.findViewById(R.id.checkBox1);
holder.iView = (ImageView) convertView
.findViewById(R.id.imageView1);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.iView.setImageResource(R.drawable.icon);
holder.cBox.setText(items.get(position));
if (selectedPos == position) {
holder.cBox.setChecked(!holder.cBox.isChecked());
}
final String c=(String) holder.cBox.getText();
holder.cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),c , Toast.LENGTH_LONG).show();
}
});
return convertView;
}
class ViewHolder {
ImageView iView;
CheckBox cBox;
}
}
public void saveSetting(View view) {
}
}
我可能知道我的代码有什么问题 提前谢谢
答案 0 :(得分:0)
首先,您必须自己选择是添加复选框还是使用ListView的CHOICE_MODE_MULTIPLE。
首先删除它:
if (selectedPos == position) {
holder.cBox.setChecked(!holder.cBox.isChecked());
}
案例1:如果您想使用CHOICE_MODE_MULTIPLE: 删除:
<CheckBox android:id="@+id/checkBox1"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:textSize="30dp" android:padding="2dp"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:focusable="false"
></CheckBox>
卸下:
listview.setChoiceMode(0);
你已经拥有:
<ListView android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice" <!-- This is for checkbox to use it as multiple choice -->
/>
案例2 :您想在行中添加自己的复选框: 在ListView属性中删除:
android:choiceMode="multipleChoice"
卸下:
listview.setChoiceMode(0); // Useless