How to select all when i check mark All CheckBox? - android

This image shows how my listview looks like
When we check All then all checkboxes should be checked and when unchecked then all checkboxes should be unchecked, here is the ListView All is also part of the list view, its position is 0 in map list...
public class MultiSelectBaseAdapter extends BaseAdapter{
Context context;
LayoutInflater inflater;
List<Boolean> selectedList;
Map<Integer, MultipleSelect> mapList;
public MultiSelectBaseAdapter(Context context, Map<Integer, MultipleSelect> mapList) {
this.context = context;
this.mapList = mapList;
selectedList = new ArrayList<>();
for(int i=0 ; i< mapList.size() ; i++){
selectedList.add(i, mapList.get(i).isSelected());
}
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public List<Boolean> getSelected(){
return selectedList;
}
public Map<Integer, MultipleSelect> getMapList(){
return mapList;
}
#Override
public int getCount() {
if(mapList != null){
return mapList.size();
}
return 0;
}
#Override
public Object getItem(int position) {
return mapList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.multiselect_item_layout, parent, false);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.textview_multiselect);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox_multiselect);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkBox.setTag(position);
holder.textView.setText(mapList.get(position).getKeyValueType().getName());
if(selectedList.get(position)){
holder.checkBox.setChecked(true);
} else{
holder.checkBox.setChecked(false);
}
holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int index = (Integer)buttonView.getTag();
if(isChecked){
selectedList.set(index, true);
}else{
selectedList.set(index, false);
}
notifyDataSetChanged();
}
});
return convertView;
}
public static class ViewHolder {
TextView textView;
CheckBox checkBox;
}
}
please help me I am new in android, I didn't work with CheckBox
[enter image description here][2]

add all selected item ids in your selectedList and then call adapter's notifyDataSetChanged() method

Add boolean value to your MultipleSelect model and change it when you checked or unchecked and for displaying in onBindViewHolder check this value and set checkbox state.
to select all change this value to true for each item in map and call:
yourMultiselectAdapter.notifyDataSetChanged();
same for deselect all - value to true and above call )

i did something like this.
this is y class side code
SelectAll =(Button)findViewById(R.id.footerapproveselectall);
SelectAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String btnlbl = SelectAll.getText().toString();
if(btnlbl.equalsIgnoreCase("SelectAll"))
{
// TODO Auto-generated method stub
CustomListAdapter adapter=new CustomListAdapter(Approver.this,AttendanceModelList,1);
lv1.setAdapter(adapter);
SelectAll.setText("Uncheck All");
}else
{
// TODO Auto-generated method stub
CustomListAdapter adapter=new CustomListAdapter(Approver.this, AttendanceModelList,2);
lv1.setAdapter(adapter);
SelectAll.setText("SelectAll");
}
}
});
And this is my adapter side code
public class CustomListAdapter extends ArrayAdapter{
private final Activity context;
/* private final List<String> empname;
private final List<String> reason;
private final List<String> empno;
private final List<String> empdate;
private final List<String> empDay ;
private final List<String> empaid;
*/
private Dialog dialog;
private Dialog dialog1;
private Dialog dialogz;
private AlertDialog BackDialog;
int cheki;
Long id;
String Givenregon;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
private final List<AttendanceModel> AttendanceModelList ;
private TextView NameShow;
private TextView NoShow;
private TextView DayShow;
private TextView DateShow;
private TextView ReasonShow;
private Button Approve;
private Button Cansel;
private Button Reject;
//Date date;
public CustomListAdapter(Activity context, List<AttendanceModel> AttendanceModelList,int cheki)
{
super(context, R.layout.mylist,AttendanceModelList);
// TODO Auto-generated constructor stub
this.context=context;
this.AttendanceModelList =AttendanceModelList;
this.cheki =cheki;
}
static class ViewHolder {
protected TextView txtTitle;
protected TextView textemployeedate;
protected ImageButton View1;
protected TextView textempday;
protected CheckBox Chek1 ;
}
public View getView(int position,View convertview,ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertview == null){
LayoutInflater inflater=context.getLayoutInflater();
convertview=inflater.inflate(R.layout.mylist, null);
viewHolder = new ViewHolder();
viewHolder.txtTitle = (TextView) convertview.findViewById(R.id.idemployeename);
//TextView extratxt = (TextView) rowView.findViewById(R.id.idreason);
//TextView txemployee = (TextView) rowView.findViewById(R.id.idempno);
viewHolder. textemployeedate = (TextView)convertview.findViewById(R.id.idempdate);
viewHolder. textempday = (TextView) convertview.findViewById(R.id.idempday);
/* Button Submit = (Button) rowView.findViewById(R.id.btapprove);
*
*/
/*Button Cansel = (Button) rowView.findViewById(R.id.btreject);*/
viewHolder. View1 =(ImageButton)convertview.findViewById(R.id.btmylistviewshow);
viewHolder. Chek1 = (CheckBox)convertview.findViewById(R.id.checkmylist);
viewHolder.Chek1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
AttendanceModelList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
convertview.setTag(viewHolder);
convertview.setTag(R.id.idemployeename,viewHolder.txtTitle);
convertview.setTag(R.id.idempday,viewHolder.textempday);
convertview.setTag(R.id.idempdate,viewHolder.textemployeedate);
convertview.setTag(R.id.checkmylist,viewHolder.Chek1);
}
else {
viewHolder = (ViewHolder) convertview.getTag();
}
viewHolder.Chek1.setTag(position);
viewHolder.Chek1.setChecked(AttendanceModelList.get(position).isSelected());
viewHolder.txtTitle.setText(AttendanceModelList.get(position).getEmpname());
if(cheki==1)
{
viewHolder. Chek1.setChecked(true);
}
else if(cheki==2)
{
viewHolder. Chek1.setChecked(false);
}
return convertview;
};

MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ListView listView;
String Name[]={"All","One","Two","Three","Four","Five"};
boolean Some[]={false,false,false,false,false,false};
MyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listView = (ListView) findViewById(R.id.listView);
adapter = new MyAdapter(MainActivity.this,Name,Some);
listView.setAdapter(adapter);
}
public void myDAta1(int pos)
{
Some[pos]=false;
adapter.notifyDataSetChanged();
}
public void myDAta2(int pos)
{
Some[pos]=true;
adapter.notifyDataSetChanged();
}
public void myDAta3()
{
for(int i=0;i<Name.length;i++)
{
Some[i]=false;
}
adapter.notifyDataSetChanged();
}
public void myDAta4()
{
for(int i=0;i<Name.length;i++)
{
Some[i]=true;
}
adapter.notifyDataSetChanged();
}
}
MyAdapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
String Name[];
boolean Some[];
Context context;
LayoutInflater inflater;
MyAdapter(Context mContext,String mName[],boolean mSome[])
{
this.context=mContext;
this.Name= mName;
this.Some=mSome;
}
#Override
public int getCount() {
return Name.length;
}
#Override
public Object getItem(int position) {
return Name[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder ;
if (convertView == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_layout, parent, false);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.textView);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(Name[position]);
holder.checkBox.setChecked(Some[position]);
if(position==0) {
if (holder.checkBox.isChecked()) {
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (context instanceof MainActivity) {
((MainActivity) context).myDAta3();
}
}
});
} else {
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (context instanceof MainActivity) {
((MainActivity) context).myDAta4();
}
}
});
}
}
else
{
if(holder.checkBox.isChecked())
{
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(context instanceof MainActivity){
((MainActivity)context).myDAta1(position);
}
}
});
}
else
{
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(context instanceof MainActivity){
((MainActivity)context).myDAta2(position);
}
}
});
}
}
return convertView;
}
public static class ViewHolder {
TextView textView;
CheckBox checkBox;
}
}

replace this code
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int index = (Integer)buttonView.getTag();
if(index==0)
{
if (isChecked)
{
for (int i = 0; i < mapList.size(); i++)
{
MultipleSelect obj = (MultipleSelect) getMapList().get(i);
// obj.setSelected(false);
// arrayList2.set(i, obj);
if(obj.isSelected())
{
selectedList.set(i, false);
}
else
{
selectedList.set(i, true);
}
}
}
else
{
selectedList.set(index, false);
}
}
else
{
if (isChecked)
{
selectedList.set(index, true);
}
else
{
selectedList.set(index, false);
selectedList.set(0, false);
}
}
notifyDataSetChanged();
}
});
return convertView;
}
public static class ViewHolder {
TextView textView;
CheckBox checkBox;
}

Related

checked all checkbox android listview

I had a listview with checkbox in every item, and I need to checked all the checkbox in my listview when I click in my checkbox checkbox_all nothing is happened , this is my code, I don't know why it dosen't work.
checkbox_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
for(int i = 0; i < list_view_themes.getAdapter().getCount(); i++){
LinearLayout itemLayout = (LinearLayout)list_view_themes.getAdapter().getView(i, null, list_view_themes);
if(itemLayout != null)
{
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.checkbox_item);
cb.setChecked(b);
((BaseAdapter) list_view_themes.getAdapter()).notifyDataSetChanged();
}
}
}
});
This is my adapter :
public class ConformityAdapter extends BaseAdapter {
private MainActivity mainActivity;
private List<Theme> themes;
public ConformityAdapter(MainActivity mainActivity, List<Theme> themes) {
this.mainActivity = mainActivity;
this.themes = themes;
}
#Override
public int getCount() {
return themes.size();
}
#Override
public Object getItem(int i) {
return themes.get(i);
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = mainActivity.getLayoutInflater();
convertView = inflater.inflate(R.layout.listview_item, null);
holder.text_title = (TextView) convertView.findViewById(R.id.text_title);
holder.text_info= (ImageView) convertView.findViewById(R.id.info);
holder.text_label = (TextView) convertView.findViewById(R.id.text_label);
holder.text_message = (TextView) convertView.findViewById(R.id.text_message);
holder.image_circle= (ImageView) convertView.findViewById(R.id.image_circle);
holder.checkBox= (CheckBox) convertView.findViewById(R.id.checkbox_item);
holder.line_listview_item = (View) convertView.findViewById(R.id.line_listview_item);
holder.star_empty = (ImageView) convertView.findViewById(R.id.star_empty);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text_info.setVisibility(View.INVISIBLE);
holder.text_label.setVisibility(View.GONE);
holder.image_circle.setVisibility(View.GONE);
holder.star_empty.setVisibility(View.GONE);
holder.line_listview_item.setVisibility(View.INVISIBLE);
holder.text_message.setPadding((int) Utils.dpToPixels(mainActivity, 20F),0,0,0);
holder.text_title.setPadding((int) Utils.dpToPixels(mainActivity, 20F),0,0,0);
holder.checkBox.setVisibility(View.VISIBLE);
holder.text_title.setText(themes.get(i).getTitle());
holder.text_label.setText("T");
holder.text_message.setText(themes.get(i).getQuestions().size()+" Questions");
holder.image_circle.setColorFilter(setColorFilter());
convertView.findViewById(R.id.item).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ConformityFragment c = new ConformityFragment();
final int [] ids = new int[1];
ids[0] = themes.get(i).getId();
Bundle b = new Bundle();
b.putIntArray("ids", ids);
c.setArguments(b);
((MyApplication) mainActivity.getApplication()).setFragment(mainActivity,c, R.id.fragment_container);
}
});
return convertView;
}
class ViewHolder {
TextView text_title;
TextView text_label;
TextView text_message;
ImageView text_info;
ImageView image_circle;
ImageView star_empty;
CheckBox checkBox;
View line_listview_item;
}
private int setColorFilter(){
return ContextCompat.getColor(mainActivity, R.color.three_round);
}
}
This is the answer :
checkbox_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
list_view_themes.setAdapter(new ConformityAdapter((MainActivity) getActivity(), themes, b));
((BaseAdapter) list_view_themes.getAdapter()).notifyDataSetChanged();
}
});
in my adapter I add a new constructor and a new line
public ConformityAdapter(MainActivity mainActivity, List<Theme> themes, boolean b) {
this.mainActivity = mainActivity;
this.themes = themes;
this.b = b;
}
new line
holder.checkBox.setChecked(b);

listView checkbox scrolling bug

I have a problem with a listView consisting of check-boxes. When I scroll some items get selected automatically.
This is my code for the getView method in my custom adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.prerequisite_course, parent, false);
holder = new ViewHolder();
holder.name = (CheckBox) convertView.findViewById(R.id.course_checkbox);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
Course course = (Course) cb.getTag();
if (cb.isChecked())
checkBoxChecked(cb, course);
else
checkBoxNotChecked(cb, course);
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
Course course = coursesDataManager.getCourseInPosition(position);
String currentId = COURSES_SUBJECT + " " + course.getId();
holder.name.setText(currentId);
holder.name.setTag(course);
// for the clear button:
if (!holder.name.isChecked()) {
checkBoxNotChecked(holder.name, (Course) holder.name.getTag());
}
return convertView;
}
private void checkBoxChecked(CheckBox cb, Course add) {
cb.setBackgroundColor(Color.rgb(1, 67, 121));
cb.setTextColor(Color.WHITE);
myCoursesManager.addPrerequisite(add);
}
private void checkBoxNotChecked(CheckBox cb, Course remove) {
cb.setBackgroundColor(Color.WHITE);
cb.setTextColor(Color.BLACK);
myCoursesManager.removePrerequisite(remove);
}
Also, criticism of the code would be welcomed as I'm new to android development. Thank you.
You need to maintain boolean array for checkbox,try this way this worked for me
public class CustomAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private final Context context;
private List<ModelPooja> listData;
public CustomAdapter(Context mainActivity, List<ModelPooja> listData) {
context = mainActivity;
this.listData = listData;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.checks.setOnCheckedChangeListener(null);
holder.checks.setFocusable(false);
if (listData.get(position).isselected) {
holder.checks.setChecked(true);
} else {
holder.checks.setChecked(false);
}
holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton cb, boolean b) {
if (checkMaxLimit()) {
if (listData.get(position).isselected && b) {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
} else {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
}
} else {
if (b) {
listData.get(position).isselected = true;
} else {
listData.get(position).isselected = false;
}
}
}
});
holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
return convertView;
}
public boolean checkMaxLimit() {
int countermax = 0;
for(ModelPooja item : listData){
if(item.isselected){
countermax++;
}
}
return countermax >= 5;
}
public class ViewHolder {
TextView tv;
public CheckBox checks;
}
}
Create a Model Class for saving the checkBox status for the list items.
public class ModelClass
{
int position=null;
String item=null;
boolean selected=false;
public ModelClass(int position,String item, boolean selected) {
super();
this.position=position;
this.item=item;
this.selected = selected;
}
}
Then in your Adapter Class
private ArrayList<ModelClass> arl;
Inside your getView()
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
arl.get(position).selected=isChecked;
}
});
holder.checkBox.setChecked(arl.get(position).selected);
And now no matter how much you scroll your checkBoxes state will be consistent(Trust me, I have used this code in my Project).

Load More option in ListView Android

Android ListView] ListView should display first 10 result and then it should display an option like "LoadMore" when I click on 'LoadMore option ' then listview will display with 10 more result and again the "LoadMore" option should display..How to do.
XML
<ListView
android:id="#+id/subCategory"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:cacheColorHint="#android:color/transparent"
android:divider="#fff"
android:dividerHeight="1dp"
android:fadingEdge="none">
</ListView>
AdapterClass
public class ListAdapter extends BaseAdapter {
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
public MobileListAdapter(Activity activity) {
mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(String item) {
mData.add(item);
notifyDataSetChanged();
}
public int getCount() {
return mData.size();
}
public String getItem(int position) {
return mData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.mobile_page, parent, false);
holder.textView = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String str = mData.get(position);
holder.textView.setText(str);
return convertView;
}
public class ViewHolder {
public TextView textView;
}
}
ActivityClass
public void setData() {
mobileList = new ArrayList<String>();
mAdapter = new ListAdapter(this);
for (int i = 0; i < str.length; i++) {
mAdapter.addItem(str[i]);
mobileList.add(str[i]);
}
Button btnLoadMore = new Button(this);
btnLoadMore.setText("Load More");
lv.addFooterView(btnLoadMore);
lv.setAdapter(mAdapter);
btnLoadMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// Starting a new async task
//new loadMoreListView().execute();
}
});
Now what else i have to do in this to show the loadmore option
Add FooterView for ListView as follows
lv.addFooterView(footerView);
Take Button as footerView and write onClick() for loading remaining items.
UPDATE :
You are storing data in ArrayList but you are not using anywhere, just write following in onClick()
for (int i = 0; i < str.length; i++) {
mAdapter.addItem(str[i]);
}
Hope this will helps you.
Just use this code...I have edited your code...based on your question..
public class ListAdapter extends BaseAdapter {
private ArrayList<String> mData;// = new ArrayList<String>();
private LayoutInflater mInflater;
public ListAdapter(Context context,ArrayList<String> data) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mData=data;
}
public void addItem(String item) {
mData.add(item);
notifyDataSetChanged();
}
public int getCount() {
return mData.size();
}
public String getItem(int position) {
return mData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
View row=convertView;
if (row == null) {
row=mInflater.inflate(R.layout.mobile_page, parent, false);
holder=new ViewHolder(row);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.textView.setText(mData.get(position).toString());
return row;
}
public class ViewHolder {
public TextView textView;
public ViewHolder(View v) {
textView=(TextView)v.findViewById(R.id.text);
}
}
}
package com.example.androidexam;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
String str[]={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29"};
ListAdapter mAdapter;
ArrayList<String> mobileList;
int index=0;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.subCategory);
setData();
}
public void setData() {
mobileList = new ArrayList<String>();
if(str.length<=10)
{
for (int i = 0; i < str.length; i++) {
/* mAdapter.addItem(str[i]);*/
mobileList.add(str[i]);
}
}else
{
for (int i = 0; i <10; i++) {
/* mAdapter.addItem(str[i]);*/
mobileList.add(str[i]);
index=i;
}
}
mAdapter=new ListAdapter(this, mobileList);
Button btnLoadMore = new Button(this);
btnLoadMore.setText("Load More");
lv.setAdapter(mAdapter);
lv.addFooterView(btnLoadMore);
btnLoadMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
for (int i = index+1; i < str.length; i++) {
/* mAdapter.addItem(str[i]);*/
mobileList.add(str[i]);
index=i;
if((i+1)%10==0)
break;
}
mAdapter=new ListAdapter(MainActivity.this, mobileList);
lv.setAdapter(mAdapter);
}
});
}
}

CustomListView with checkbox issues?

Hi I've created CustomListView with checkbox. When I click first item, it automaically enable third item also. How to solve it?
holder.orderCheckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(((CheckBox)v).isChecked()) {
checkboxstate[position]=true;
if(observationDetailsList.get(position).LaboratoryUid.toString().equalsIgnoreCase(GlobalVariables.UUIDEmpty.toString()))
{
displayWarning();
}
patientOrderList.add(orderDetailsList.get(position));
patientObservationList.add(observationDetailsList.get(position));
} else {
checkboxstate[position]=false;
patientOrderList.remove(orderDetailsList.get(position));
patientObservationList.remove(observationDetailsList.get(position));
}
}
});
Here is the complete adapter class to handle this
i am using a toogle button for that and the same you can do with check box you have to maintain state of check boxes
public class SettingsListAdapter extends BaseAdapter implements
{
private LayoutInflater mInflater;
private DealsSTRUC[] setings_Adapt_Catagories;
Context mContext;
String isNotificationEnnabled;
public SettingsListAdapter( Context context, DealsSTRUC[] catagories ,String isEnnabled)
{
mContext = context;
setings_Adapt_Catagories = catagories;
mInflater = LayoutInflater.from(context);
isNotificationEnnabled=isEnnabled;
}
#Override
public int getCount()
{
return setings_Adapt_Catagories.length;
}
#Override
public Object getItem(int arg0)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
SettingsAdptorViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.adapter_settingslist, null);
holder = new SettingsAdptorViewHolder();
holder.settings_catagory = (TextView) convertView.findViewById(R.id.settings_lable_text);
holder.settings_list_checkbox = (ToggleButton) convertView.findViewById(R.id.settings_checkbox);
holder.settings_list_checkbox.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
ToggleButton cb = (ToggleButton) v;
if (cb.isChecked() == true)
{
setings_Adapt_Catagories[position].setStatus("true");
}
else
{
setings_Adapt_Catagories[position].setStatus("false");
}
}
});
convertView.setTag(holder);
}
else
{
holder = (SettingsAdptorViewHolder) convertView.getTag();
}
if(setings_Adapt_Catagories[position].getStatus().equals("true")&&isNotificationEnnabled.equals("on"))
{
holder.settings_list_checkbox.setChecked(true);
}
else if(setings_Adapt_Catagories[position].getStatus().equals("false"))
{
holder.settings_list_checkbox.setChecked(false);
}
holder.settings_catagory.setText(setings_Adapt_Catagories[position].getDealName());
return convertView;
}
class SettingsAdptorViewHolder
{
TextView settings_catagory;
ToggleButton settings_list_checkbox;
}
}

android checkbox is not checked for custom listview when listview is in scrollview

i have implemented custom list view with check box and textviews. It is working fine when list view is not in scroll but if the rows are increased then listviews is in scroll view that time when i click on check box the checked mark is not staying. here is my code, Please suggest me if anything wrong in that code
public class ArchivedAdapter extends ArrayAdapter<MessageBean> {
final ArrayList<MessageBean> updatedList = new ArrayList<MessageBean>();
private List<MessageBean> list;
private TextView officenameView, sentOnView, messageView;
public final static String MY_ACTION = "com.android.threeptalk.broadcast";
private boolean isDeleted = false;
int count = 0;
HashMap<Integer, CheckBox> checkList = new HashMap<Integer, CheckBox>();
public ArchivedAdapter(Context context, int resourseID,
List<MessageBean> list) {
super(context, resourseID, list);
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public MessageBean getItem(int arg0) {
return list.get(arg0);
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = (LayoutInflater) this.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.archievedmsgrow, parent, false);
row.setId(position);
Object obj = getItem(position);
if (obj instanceof MessageBean) {
final MessageBean message = (MessageBean) obj;
CheckBox checkBox = (CheckBox) row.findViewById(R.id.checkBox);
officenameView = (TextView) row.findViewById(R.id.officename);
sentOnView = (TextView) row.findViewById(R.id.time);
messageView = (TextView) row.findViewById(R.id.message);
officenameView.setText(message.getOfficeName());
checkList.put(position, checkBox);
checkBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
CheckBox c = (CheckBox) v;
if (c.isChecked()) {
c.setChecked(true);
count++;
updatedList.add(message);
} else {
c.setChecked(false);
count--;
updatedList.remove(message);
updatedList.size());
}
checkedCount(count, updatedList);
}
});
row.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
rowclicked(message, position);
}
});
sentOnView.setText(Common.getAppropriateTimeUsingTimeZone(message
.getmDate()));
if (message.getMessage() != null) {
messageView.setText((message.getMessage()));
} else {
if (message.getMessageType().equals("OPTION")) {
if (message.getOptions() != null
&& message.getOptions().size() > 0) {
String data = message.getOptions().get(0);
if (data != null && !data.equals("")) {
messageView.setText(data);
}
}
}
}
}
return row;
}
protected void messageSelected() {
}
protected void rowclicked(MessageBean message, int position) {
// TODO Auto-generated method stub
}
protected void checkedCount(int count2, ArrayList<MessageBean> updatedList) {
}
}
`
Try this example ListView with CheckBox Scrolling Issue
Seems like you are checking the check status of checkbox inside its onClickListener().
First define an Hashmap or SparseBooleanArray to save your selections.
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
Then add the following code to your getView() method.
CheckBox checkBox = (CheckBox) row.findViewById(R.id.checkBox);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mSelection.put(position, isChecked);
}
});
if (mSelection.get(position) != null) {
if (mSelection.get(position))
checkBox.setChecked(true);
else
checkBox.setChecked(false);
}
Try this, Assuming that you are using the Custom Adapter for your ListView, you will be able to set Tag for each Item inside your Adapter's getView(),
public View getView(int position, View convertView, ViewGroup parent) {
EvenViewHolder holdereven;
if (convertView == null) {
convertView = mInfaltor.inflate(R.layout.schedule_adapter_view,
null);
holdereven = new EvenViewHolder();
holdereven.time = (TextView) convertView
.findViewById(R.id.time);
holdereven.title = (TextView) convertView
.findViewById(R.id.title);
holdereven.des = (TextView) convertView.findViewById(R.id.des);
convertView.setTag(holdereven);
} else {
holdereven = (EvenViewHolder) convertView.getTag();
}
static class EvenViewHolder {
TextView time, title, des;
}
}

Categories

Resources