Open top menu

        Custom ListView with CheckBox in Android

In this blog we will explain how to implement Custom ListView with CheckBox and TextView in android.

1. In this code Add some Extra functions Multiple choice with Counter, how Much select user list Item 1,2,3,4,5 etc according to Requirement. 

2. Explain how to update ListView row via index.

show.

mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 int counter = 2;
 int j = 0;
 
SparseBooleanArray sparseBooleanArray = mainListView.getCheckedItemPositions();
@Override
public void onItemClick(AdapterView<?> parent, View item,int position, long id) {
mItems list_item = listAdapter.getItem(position);
String str=planet.getName();
SelectViewHolder viewHolder = (SelectViewHolder) item.getTag();  
if (sparseBooleanArray.get(position)==true) {
if (counter > j) {
  viewHolder.getCheckBox().setChecked(true);  
  j++;
  
}else {
mainListView.setItemChecked(position, false);
   
}
} else {
boolean chk=list_item .getChecked();
if(chk==true){
   j--;    
}
}
}
});

Explain source code Step By Step

Step 1. first ,you can create item Entity class inside the src folder.

mItem.java
.............................................................................................................................................................

public class Items {
private String name = "";
private boolean checked = false;
private String subtext = "";
public Items() {
}

public Items(String name) {
this.name = name;
}

public mItems(String name, boolean checked) {
this.name = name;
this.checked = checked;
}
public mItems(String name,String subtext, boolean checked) {
this.name = name;
this.subtext = subtext;
this.checked = checked;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public boolean isChecked() {
return checked;
}

public void setChecked(boolean checked) {
this.checked = checked;
}
public boolean getChecked() {
return checked;
}

public String toString() {
return name;
}

/*public void toggleChecked() {
checked = !checked;
}
*/
public String getSubtext() {
return subtext;
}

public void setSubtext(String subtext) {
this.subtext = subtext;
}
}

Stepe 2. create the ViewHolder class inside src folder

SelectViewHolder.java
...................................................................................................................................................................
public class SelectViewHolder {
private CheckBox checkBox;
private TextView textView;
private TextView subtextView;

public SelectViewHolder() {
}

public SelectViewHolder(TextView textView, TextView subtextView, CheckBox checkBox) {
this.checkBox = checkBox;
this.textView = textView;
this.subtextView = subtextView;
}

public CheckBox getCheckBox() {
return checkBox;
}

public void setCheckBox(CheckBox checkBox) {
this.checkBox = checkBox;
}

public TextView getTextView() {
return textView;
}

public void setTextView(TextView textView) {
this.textView = textView;
}

public TextView getSubtextView() {
return subtextView;
}

public void setSubtextView(TextView subtextView) {
this.subtextView = subtextView;
}
}
Stepe 3.create a item xml class inside res folder

Simplerow.xml
in this class set the textview and Checkbox.
...................................................................................................................................................................


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/rowTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="16sp"
        android:textStyle="bold" >
    </TextView>

    <CheckBox
        android:id="@+id/CheckBox01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="6sp"
        android:focusable="false"
        android:padding="10dp" >
    </CheckBox>

    <TextView
        android:id="@+id/itempart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>


Stepe 4. create adapter class inside src folder.

SelectArralAdapter.java
...................................................................................................................................................................

public class SelectArralAdapter extends ArrayAdapter<mItems> {
private LayoutInflater inflater;

public SelectArralAdapter(Context context, List<mItems> planetList) {
super(context, R.layout.simplerow, R.id.rowTextView, planetList );
inflater = LayoutInflater.from(context);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

mItems item_text = (mItems) this.getItem(position);

CheckBox checkBox;
TextView textView,textitemss = null;


if (convertView == null) {
convertView = inflater.inflate(R.layout.simplerow, null);
textView = (TextView) convertView.findViewById(R.id.rowTextView);
checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);
textitemss=(TextView) convertView.findViewById(R.id.itempart);


convertView.setTag(new SelectViewHolder(textView,textitemss,checkBox));

checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
cb.setChecked(false);
}
});
}

else {

SelectViewHolder viewHolder = (SelectViewHolder) convertView.getTag();
checkBox = viewHolder.getCheckBox();
textView = viewHolder.getTextView();
textitemss= viewHolder.getSubtextView();
}


checkBox.setTag(item_text);
checkBox.setChecked(item_text.getChecked());
textView.setText(item_text.getName());
textitemss.setText(item_text.getSubtext());

return convertView;
}
}





Stepe 5 create the activity_mainclass inside the res folder

activity_main.xml
..................................................................................................................................................................
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools">

            <LinearLayout
                android:id="@+id/layout_dal_subj"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="4dp"
                android:gravity="center"
                android:orientation="vertical"
                android:paddingBottom="2dp" >

                <TextView
                    android:id="@+id/textView_dal_subj"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="3dp"
                    android:layout_marginRight="3dp"
                    android:layout_marginTop="3dp"
                    android:gravity="center"
                    android:padding="7dp"
                    android:textSize="24sp"
                    android:textStyle="bold"
                     android:text="Androidbeginner Point"/>

                <ListView
                    android:id="@+id/listView_dal_subj"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="3dp"
                    android:scrollbars="none"
                    tools:ignore="NestedScrolling" >
                </ListView>
            </LinearLayout>


</RelativeLayout>




Stepe 6 create the MainActivity class inside src folder....
MainActivity .java
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.


@SuppressLint("InflateParams")
public class MainActivity extends Activity {

private ListView mainListView,mainListView1,mainListView2;

private ArrayAdapter<mItems> listAdapter;
ArrayList<String> checked = new ArrayList<String>();
ArrayList<String>  list;
ArrayList<mItems> Item_List;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainListView = (ListView) findViewById(R.id.listView_dal_subj);


       Item_List = new ArrayList<mItems>();

 
planetList.add(new mItems("Plain Roti","",false));

planetList.add(new mItems("Tandoori Roti","",false));

planetList.add(new mItems("Rice","",false));

planetList.add(new mItems("fry Rice","",false));

planetList.add(new mItems("Dal Makhni","",false));

planetList.add(new mItems("Sahi Panner","",false));


listAdapter = new SelectArralAdapter(this, Item_List);

mainListView.setAdapter(listAdapter);
mainListView.setItemsCanFocus(false);
mainListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Utility.setListViewHeightBasedOnChildren(mainListView);

mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
int counter = 2;
int j = 0;

  SparseBooleanArray sparseBooleanArray = mainListView.getCheckedItemPositions();
 @Override
  public void onItemClick(AdapterView<?> parent, View item,int position, long id) {

mItems list_item = listAdapter.getItem(position);
String str=list_item.getName();
SelectViewHolder viewHolder = (SelectViewHolder) item.getTag();


if (sparseBooleanArray.get(position)==true) {

if (counter > j) {

  viewHolder.getCheckBox().setChecked(true);
 
  int size = mainListView.getCount();
  int totalture=0;
  Log.i("hello", String.valueOf(size));

for (int i = 0; i < size; i++) {  
   boolean value = sparseBooleanArray.get(i);  
   if(value==true){
   Log.d("all true value", String.valueOf(i)+value);
   totalture++;
       }  
   }

if(totalture==1){
planetList.set(position, new mItems(str,"full plate",true)) ;

}else {
for (int i = 0; i < size; i++) {
mItems indexvalue=listAdapter.getItem(i);
String str3=String.valueOf(indexvalue.toString());
boolean value = sparseBooleanArray.get(i);  
if(value==true){
   planetList.set(i, new mItems(str3,"half plate",true)) ;
   }
}
}    
  listAdapter.notifyDataSetChanged();
 
  j++;

}else {
mainListView.setItemChecked(position, false);
           Toast.makeText(getApplicationContext(), "only 3 checks items",                                                                                                   Toast.LENGTH_LONG).show(); 
}

} else {

boolean chk=list_item.getChecked();
if(chk==true){
int size = mainListView.getCount();
int totalture=0;
for (int i = 0; i < size; i++) {
   boolean value = sparseBooleanArray.get(i);  
   if(value==true){
   totalture++;
   }  
}
for (int i = 0; i< size; i++) {

mItems indexvalue=listAdapter.getItem(i);
String str3=String.valueOf(indexvalue.toString());
boolean value = sparseBooleanArray.get(i);  
if(value==true){  
     if(totalture==1){
       viewHolder.getCheckBox().setChecked(true);
       planetList.set(i, new mItems(str3,"full plate",true)) ;
}
     else {
     viewHolder.getCheckBox().setChecked(true);
         planetList.set(i,new mItems(str3,"half plate",true));
}
       } else {
        viewHolder.getCheckBox().setChecked(false);
         planetList.set(i,new mItems(str3,"",false));
          }
                       }

   listAdapter.notifyDataSetChanged();
   j--;  
}
}
}
});






}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

menu.add(0, 1, Menu.NONE, "Products");
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case 1:

for (int i = 0; i < checked.size(); i++) {
Log.d("pos : ", "" + checked.get(i));
}
break;
}
return super.onOptionsItemSelected(item);
}



}
...................................................................................................................................................................




Update the list Index Onclickitem possition........




Tagged

0 comments