在附加图片中,活动中存在SelectAll checkbox
,适配器中存在checkboxes
。
如果我checked SelectAll
,那么在适配器中检查所有复选框,如果我取消选中,则取消选中所有复选框。它发生得很好。
但是如果我checked SelectAll
我想要,之后如果我uncheked
在适配器中有一个或多个复选框,那么SelectAll checkbox should unchecked
。
如果没有检查SelectAll如果我手动检查适配器中的所有复选框,然后自动SelectAll checkbox should checked
。
我该怎么做。
贝洛是我的代码。, http://pastebin.com/5pUJJC42
答案 0 :(得分:7)
我确实喜欢下面,我得到了我所需的输出,
答案 1 :(得分:3)
我试过这样的事情,
将checkAll复选框传递给Adapter类构造函数,并在其中设置Listener,这样我们就不需要从Main Class声明任何标志public static。
此外,我还使用了几个保持复选框状态的标志,这是我试图维护的,当checkAll复选框检查更改时,它不会影响列表项复选框,反之亦然,列表项复选框检查。
所以,试试这个
public class myAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
private CheckBox checkAll;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public myAdapter(Activity context, List<Model> list, CheckBox checkAll) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
this.checkAll = checkAll;
checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton checkbox, boolean arg1) {
if(!checkItem_flag){
checkAll_flag = true;
notifyDataSetChanged();
}
}
});
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
private boolean areAllSelected() {
boolean areAllSelected = false;
for (int i = 0; i < list.size(); i++) {
if(list.get(i).isSelected()){
areAllSelected = true;
}
else{
areAllSelected = false;
return areAllSelected;
}
}
return areAllSelected;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Model element = (Model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
if(!checkAll_flag){
checkItem_flag = true;
if(buttonView.isChecked()){
checkAll.setChecked(areAllSelected());
}
if(!buttonView.isChecked()){
checkAll.setChecked(areAllSelected());
}
checkItem_flag = false;
}
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
if(checkAll_flag){
if(checkAll.isChecked()){
holder.checkbox.setChecked(true);
}
else if(!checkAll.isChecked()){
holder.checkbox.setChecked(false);
}
if(position == (list.size() -1)){
checkAll_flag = false;
}
}
return view;
}
}
答案 2 :(得分:3)
我过去也遇到过类似的问题。您需要行视图来实现Checkable
。检查this question。
答案 3 :(得分:2)
你可以创建一个与项目列表相对应的布尔数组,并且在适配器中你应该切换相应的选中/取消选中的布尔值,并在适配器上的每个检查中,布尔数组的所有元素都是真的吗?然后setchecked(true)selectall_checkbox,否则保持setchecked(未选中)。
答案 4 :(得分:2)
修改强>
您的适配器应如下所示:
public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
private CheckBox selectAll;
public InteractiveArrayAdapter( Activity context, List<Model> list, CheckBox selectAll ) {
super( context, R.layout.list_items_attendance_payment, list );
this.context = context;
this.list = list;
this.selectAll = selectAll;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView( final int position, View convertView, ViewGroup parent ) {
View view = null;
if( convertView == null ) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate( R.layout.list_items_attendance_payment, null );
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById( R.id.name );
viewHolder.checkbox = (CheckBox) view.findViewById( R.id.check );
view.setTag( viewHolder );
}
else {
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText( list.get( position ).getName() );
viewHolder.checkbox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ) {
AttendanceActivity.listMember.get( position ).setSelected( isChecked );
list.get( position ).setSelected( buttonView.isChecked() );
selectAll.setChecked( areAllSelected() ); // We just use the method as parameter as it returns a boolean.
}
} );
if( selectAll.isChecked() )
holder.checkbox.setChecked( true );
else
holder.checkbox.setChecked( list.get( position ).isSelected() );
return view;
}
public void deselectAll() {
for( Model element : list )
element.setSelected( false );
notifyDataSetChanged();
}
public void selectAll() {
for( Model element : list )
element.setSelected( true );
notifyDataSetChanged();
}
private boolean areAllSelected() {
for( Model element : list )
if( !element.getSelected() )
return false; // We conclude that not all are selected.
return true; // All of the items were selected
}
}
设置Activity
的{{1}}中的方法应更改为:
OnCheckedChangedListener
答案 5 :(得分:2)
这是一个简单的解决方案。
@Override
public View getView(int position, View v, ViewGroup arg2) {
final PillTime pillTime = arrPillTimes.get(position);
if(v == null) {
v = inflater.inflate(R.layout.select_time_row, null);
}
TextView tvTitle = (TextView)v.findViewById(R.id.tvTitle);
TextView tvTime = (TextView)v.findViewById(R.id.tvTime);
tvTime.setText(pillTime.getTime());
final CheckBox cbtime = (CheckBox)v.findViewById(R.id.cbtime);
cbtime.setChecked(false);
for (PillTime pillTime2 : arrSelectedTimes) {
if(pillTime2.getId().equals(pillTime.getId())) {
cbtime.setChecked(true);
break;
}
}
tvTitle.setText(pillTime.getTitle());
cbtime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(cbtime.isChecked()) {
boolean contains = false;
for (PillTime pillTime2 : arrSelectedTimes) {
if(pillTime2.getId().equals(pillTime.getId())) {
contains = true;
break;
}
}
if(!contains) {
arrSelectedTimes.add(pillTime);
if(arrPillTimes.size() == arrSelectedTimes.size()) {
cbSelectAll.setChecked(true);
}
}
}else {
for (PillTime pillTime2 : arrSelectedTimes) {
if(pillTime2.getId().equals(pillTime.getId())) {
arrSelectedTimes.remove(pillTime2);
cbSelectAll.setChecked(false);
break;
}
}
}
}
});
return v;
}
我有一个主arrayList arrPillTimes
,它包含所有数据,我有第二个arrayList
arrSelectedTimes
将保留已检查项目的跟踪。我们将检查所选项目是否在arrSelectedTimes
中,这意味着您已检查该项目,因此您必须将该复选框的checked属性设置为true。
答案 6 :(得分:1)
尝试使用CheckedTextView解决此问题
<强> main.xml中强>
<?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" android:background="#ffffff">
<ListView
android:id="@+id/listView"
android:scrollbars="vertical" android:divider="#C0C0C0" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:cacheColorHint="#00000000" android:dividerHeight="1dip">
</ListView>
<LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent">
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="All"></Button>
<Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="None"></Button>
<Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Done"></Button>
</LinearLayout>
</LinearLayout>
<强> listrow.xml 强>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:background="#ffffff" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="#000000" android:padding="15dip"/>
<强> CheckedTextVwActivity.java 强>
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;
public class CheckedTextVwActivity extends Activity {
/** Called when the activity is first created. */
ListView listView;
ListAdapter adapter;
ArrayList<String> strings = new ArrayList<String>();
Button button1,button2,button3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
strings.add(new String("B"));
strings.add(new String("A"));
TopicSelectionListAdapter topicSelectionListAdapter = new TopicSelectionListAdapter(
CheckedTextVwActivity.this, R.layout.listrow, strings);
listView.setAdapter(topicSelectionListAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
for(int i=0;i<strings.size();i++){
listView.setItemChecked(i, true);
}
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
CheckedTextView selectedItem = (CheckedTextView) view;
boolean isChecked = selectedItem.isChecked();
Log.e("TAG","item clicked position = " + position + " isChecked = " + isChecked);
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<strings.size();i++){
listView.setItemChecked(i, true);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<strings.size();i++){
listView.setItemChecked(i, false);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = listView.getAdapter().getCount();
String savedItems = null;
for (int i = 0; i < count; i++) {
if (listView.isItemChecked(i)) {
savedItems = savedItems + listView.getItemAtPosition(i).toString() + ",";
}
}
Toast.makeText(CheckedTextVwActivity.this, ""+savedItems, Toast.LENGTH_LONG).show();
}
});
}
public class TopicSelectionListAdapter extends ArrayAdapter {
@SuppressWarnings("unchecked")
public TopicSelectionListAdapter(Context context,
int textViewResourceId, List objects) {
super(context, textViewResourceId, objects);
}
@Override
public long getItemId(int currentPosition) {
return super.getItemId(currentPosition);
}
@Override
public View getView(int currentPosition, View convertView,
ViewGroup parent) {
View v = convertView;
final LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listrow, null);
final CheckedTextView textView = (CheckedTextView) v
.findViewById(R.id.text);
textView.setText(strings.get(currentPosition));
return v;
}
}
}