选择标题复选框后,未选中所有列表视图项复选框。 选定的列表视图复选框项不会从list1移动到list2。 以下是完整的代码 家庭活动课
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class HomeAct extends Activity {
List<DocItem> docDet1 = new ArrayList<DocItem>();
List<DocItem> docDet2 = new ArrayList<DocItem>();
ListView lv1, lv2;
Button btn1;
DocDetAdapter adapter1, adapter2;
int n = 0;
int marksValue;
int value = 0;
CheckBox boxheader;
ArrayList<Boolean> positionArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_act);
lv1 = (ListView) findViewById(R.id.lv_det1);
lv2 = (ListView) findViewById(R.id.lv_det2);
btn1 = (Button) findViewById(R.id.btn1);
boxheader = (CheckBox)findViewById(R.id.checkBoxHeader);
adapter1 = new DocDetAdapter(1);
adapter2 = new DocDetAdapter(2);
docDet1.add(new DocItem("1", "john", 20));
docDet1.add(new DocItem("2", "karan", 10));
docDet1.add(new DocItem("3", "james", 5));
docDet1.add(new DocItem("4", "shaun", 60));
docDet1.add(new DocItem("5", "jack", 50));
docDet1.add(new DocItem("6", "sam", 30));
docDet1.add(new DocItem("7", "tony", 6));
docDet1.add(new DocItem("8", "mark", 42));
lv1.setAdapter(adapter1);
lv2.setAdapter(adapter2);
positionArray = new ArrayList<Boolean>(docDet1.size());
for (int i = 0; i < docDet1.size(); i++) {
positionArray.add(false);
}
boxheader.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
int itemsCount = lv1.getChildCount();
for (int i = 0; i < itemsCount; i++) {
View view = lv1.getChildAt(i);
CheckBox checkBox = (CheckBox) view
.findViewById(R.id.checkBoxRow);
checkBox.setChecked(true);
}
} else {
int itemsCount = lv1.getChildCount();
for (int i = 0; i < itemsCount; i++) {
View view = lv1.getChildAt(i);
CheckBox checkBox = (CheckBox) view
.findViewById(R.id.checkBoxRow);
checkBox.setChecked(false);
}
}
}
});
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int itemsCount = lv1.getChildCount();
for (int i = 0; i < itemsCount; i++) {
View view = lv1.getChildAt(i);
CheckBox checkBox = (CheckBox) view
.findViewById(R.id.checkBoxRow);
if (checkBox.isChecked()) {
System.out.println("HomeAct.onCreate(...).new OnClickListener() {...}.onClick() -- 1111 " + i);
System.out.println("HomeAct.onCreate(...).new OnClickListener() {...}.onClick() -- 2222 " + docDet1.size());
if (docDet1.size() == 1) {
new AlertDialog.Builder(HomeAct.this)
.setTitle("Attention!")
.setMessage("Last name")
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
docDet1.remove(0);
docDet2.add(new DocItem(docDet1.get(0).docNo, docDet1.get(0).name, docDet1.get(0).marks));
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
}
}).show();
return;
}
docDet2.add(new DocItem(docDet1.get(i).docNo, docDet1.get(i).name, docDet1.get(i).marks));
docDet1.remove(i);
adapter2.notifyDataSetChanged();
adapter1.notifyDataSetChanged();
} else {
System.out.println("HomeAct.onCreate(...).new OnClickListener() {...}.onClick() -- 3333 none are checked ");
Toast.makeText(HomeAct.this, "Please select atleast one name", Toast.LENGTH_LONG).show();
}
}
}
});
}
DocItem findDocItem(String name) {
for (DocItem item : docDet2) {
if (item.name.equals(name)) {
return item;
}
}
return null;
}
DocItem findDocItem2(String name) {
for (DocItem item : docDet1) {
if (item.name.equals(name)) {
return item;
}
}
return null;
}
private class DocDetAdapter extends BaseAdapter {
int mode; // 1 or 2
public DocDetAdapter(int mode) {
this.mode = mode;
}
@Override
public int getCount() {
if (mode == 1)
return docDet1.size();
else
return docDet2.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater li = getLayoutInflater();
if (convertView == null)
convertView = li.inflate(R.layout.row_det, null);
TextView tvName = (TextView) convertView
.findViewById(R.id.tv_name);
TextView tvNo = (TextView) convertView.findViewById(R.id.tv_no);
TextView tvMarks = (TextView) convertView.findViewById(R.id.tv_marks);
CheckBox box = (CheckBox)convertView.findViewById(R.id.checkBoxRow);
DocItem invItem;
if (mode == 1)
invItem = docDet1.get(position);
else
invItem = docDet2.get(position);
tvNo.setText(invItem.docNo);
tvName.setText(invItem.name);
tvMarks.setText(invItem.marks + "");
box.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
positionArray.set(position, true);
} else {
positionArray.set(position, false);
}
}
});
box.setChecked(positionArray.get(position));
return convertView;
}
}
}
DocItem类
public class DocItem {
public String docNo, name;
public Integer marks;
public DocItem(String docNo, String name, Integer marks) {
super();
this.docNo = docNo;
this.name = name;
this.marks = marks;
}
}
home_act.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"
android:orientation="vertical" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="3"
android:text="Add" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:text="list 1"
android:textSize="20sp"
android:textStyle="bold" >
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/row_bg_transparent_white"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_tv_no"
style="@android:style/TextAppearance.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:gravity="center_vertical"
android:padding="10dp"
android:text="Sl no"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ems="10"
android:gravity="center_vertical"
android:text="Name"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_marks"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ems="10"
android:gravity="center_vertical"
android:paddingRight="3dp"
android:text="Marks"
android:textSize="16sp"
android:textStyle="bold" />
<CheckBox
android:id="@+id/checkBoxHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select all" />
</LinearLayout>
<ListView
android:id="@+id/lv_det1"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:text="list 2"
android:textSize="20sp"
android:textStyle="bold" >
</TextView>
<ListView
android:id="@+id/lv_det2"
android:layout_width="fill_parent"
android:layout_height="250dp" />
</LinearLayout>
row_det.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="wrap_content"
android:orientation="vertical" >
<View
android:id="@+id/v_doc_seperator"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="@color/blue"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/row_bg_transparent_white"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_no"
style="@android:style/TextAppearance.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:gravity="center_vertical"
android:padding="10dp"
android:text="Sl no"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ems="10"
android:gravity="center_vertical"
android:text="Name"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_marks"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ems="10"
android:gravity="center_vertical"
android:paddingRight="3dp"
android:text="Marks"
android:textSize="16sp"
android:textStyle="bold" />
<CheckBox
android:id="@+id/checkBoxRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
</LinearLayout>
请查看以下截图
如果我选择标题复选框(全选),则只选择前五行 未选择Remainig行
如果我选择第2行和第3行并点击添加我得错了输出(参见最后的截图)
第2行和第4行在list2中添加(但正确的输出是第2行,第3行应该添加)点击后添加所有复选框应该是未选中的。
请事先帮助我。
答案 0 :(得分:2)
试试这个:
public class HomeAct extends Activity {
List<DocItem> docDet1 = new ArrayList<DocItem>();
List<DocItem> docDet2 = new ArrayList<DocItem>();
ListView lv1, lv2;
Button btn1;
DocDetAdapter adapter1, adapter2;
int n = 0;
int marksValue;
int value = 0;
CheckBox boxheader;
ArrayList<Boolean> positionArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_act);
lv1 = (ListView) findViewById(R.id.lv_det1);
lv2 = (ListView) findViewById(R.id.lv_det2);
btn1 = (Button) findViewById(R.id.btn1);
boxheader = (CheckBox)findViewById(R.id.checkBoxHeader);
adapter1 = new DocDetAdapter(1);
adapter2 = new DocDetAdapter(2);
docDet1.add(new DocItem("1", "john", 20));
docDet1.add(new DocItem("2", "karan", 10));
docDet1.add(new DocItem("3", "james", 5));
docDet1.add(new DocItem("4", "shaun", 60));
docDet1.add(new DocItem("5", "jack", 50));
docDet1.add(new DocItem("6", "sam", 30));
docDet1.add(new DocItem("7", "tony", 6));
docDet1.add(new DocItem("8", "mark", 42));
lv1.setAdapter(adapter1);
lv2.setAdapter(adapter2);
positionArray = new ArrayList<Boolean>(docDet1.size());
for (int i = 0; i < docDet1.size(); i++) {
positionArray.add(false);
}
boxheader.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
/* int itemsCount = lv1.getChildCount();
for (int i = 0; i < itemsCount; i++) {
View view = lv1.getChildAt(i);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBoxRow);
checkBox.setChecked(true);
} */
// Update DataSet instead of view
for (int i = 0; i < docDet1.size(); i++){
positionArray.set(i, true);
}
} else {
/* int itemsCount = lv1.getChildCount();
for (int i = 0; i < itemsCount; i++) {
View view = lv1.getChildAt(i);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBoxRow);
checkBox.setChecked(false);
} */
// Update DataSet instead of view
for (int i = 0; i < docDet1.size(); i++){
positionArray.set(i, false);
}
}
// Update ListView after dataSet updated.
adapter1.notifyDataSetChanged();
}
});
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/* int itemsCount = lv1.getChildCount(); */
// Get items from end to beginning of list.
// Otherwise position may be wrong after remove item.
for (int i = docDet1.size() - 1; i >= 0; i--) {
/* View view = lv1.getChildAt(i);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBoxRow);
if (checkBox.isChecked()) { */
if(positionArray.get(i)) {
System.out.println("HomeAct.onCreate(...).new OnClickListener() {...}.onClick() -- 1111 " + i);
System.out.println("HomeAct.onCreate(...).new OnClickListener() {...}.onClick() -- 2222 " + docDet1.size());
if (docDet1.size() == 1) {
new AlertDialog.Builder(HomeAct.this)
.setTitle("Attention!")
.setMessage("Last name")
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
docDet1.remove(0);
docDet2.add(new DocItem(docDet1.get(0).docNo, docDet1.get(0).name, docDet1.get(0).marks));
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
}
}).show();
return;
}
docDet2.add(0, new DocItem(docDet1.get(i).docNo, docDet1.get(i).name, docDet1.get(i).marks));
docDet1.remove(i);
/* adapter2.notifyDataSetChanged();
adapter1.notifyDataSetChanged(); */
} else {
System.out.println("HomeAct.onCreate(...).new OnClickListener() {...}.onClick() -- 3333 none are checked ");
Toast.makeText(HomeAct.this, "Please select atleast one name", Toast.LENGTH_LONG).show();
}
}
// Reset all CheckBox data
for (int i = 0; i < positionArray.size(); i++){
positionArray.set(i, false);
}
// Update ListViews after all data updated.
adapter2.notifyDataSetChanged();
adapter1.notifyDataSetChanged();
}
});
}
DocItem findDocItem(String name) {
for (DocItem item : docDet2) {
if (item.name.equals(name)) {
return item;
}
}
return null;
}
DocItem findDocItem2(String name) {
for (DocItem item : docDet1) {
if (item.name.equals(name)) {
return item;
}
}
return null;
}
private class DocDetAdapter extends BaseAdapter {
int mode; // 1 or 2
public DocDetAdapter(int mode) {
this.mode = mode;
}
@Override
public int getCount() {
if (mode == 1)
return docDet1.size();
else
return docDet2.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater li = getLayoutInflater();
if (convertView == null) convertView = li.inflate(R.layout.row_det, null);
TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
TextView tvNo = (TextView) convertView.findViewById(R.id.tv_no);
TextView tvMarks = (TextView) convertView.findViewById(R.id.tv_marks);
CheckBox box = (CheckBox)convertView.findViewById(R.id.checkBoxRow);
DocItem invItem;
if (mode == 1)
invItem = docDet1.get(position);
else
invItem = docDet2.get(position);
tvNo.setText(invItem.docNo);
tvName.setText(invItem.name);
tvMarks.setText(invItem.marks + "");
box.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
positionArray.set(position, true);
} else {
positionArray.set(position, false);
}
}
});
box.setChecked(positionArray.get(position));
return convertView;
}
}
}
希望这有帮助!
答案 1 :(得分:0)
因为在检查之后你正在添加包含在适配器中的数组,并且在滚动时它将刷新。因此,您应该在选中或取消选中后使用界面,您应该在主类中添加状态,然后通知您的适配器并使列表视图无效。