After selecting the header checkbox all listview item checkbox not selected. Selected listview checkbox items are not moved from list1 to list2. Below is the full code Home Activity class
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 class
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>
Please find the below screenshots
If I select header checkbox(select all), only first five rows are selected Remainig rows are not selected
If I select row 2 and row 3 and click on add I got wrong output(refer last screenshot)
row 2 and row 4 are added in list2 (but correct output is row 2 and row 3 should be added) after clicking on add all the checkboxes should be unchecked.
Please help me thanks in advance.