Skip to main content

CheckBox in Listview Using Custom Adapter

 In this Application we just need to add a CheckBox along with the TextView. This Tutorial is an extension of ListView Using Custom Adapter 

We need the following things:
1. CustomAdapter class
2. DataModel Class
3. MainActivity Class

and with these classes we need only two xml files
1. activity_main.xml(xml file for MainActivity.java)
2. row.xml(To hold listview components)

Here is your Code :



MainActivity.java

package com.AndroidDevelopmentGuru.checkbox;

import java.util.ArrayList;



import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity  {


    ListView list;
    DataModel[] items;
   
    DataModel data;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
       
        list =(ListView)findViewById(R.id.listView1);
        list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
     
        data = new DataModel();
        items = new DataModel[5];
        items[0] = new DataModel("BTech", 0);
        items[1]= new DataModel("MCA", 0);
        items[2]= new DataModel("Mtech", 0);
        items[3]= new DataModel("MBA", 0);
        items[4]= new DataModel("PhD", 0);
       
        CustomAdapter adapter = new CustomAdapter(this,items);
        list.setAdapter(adapter);
   
            
        }


   
   
}



CustomAdapter.java

package com.AndroidDevelopmentGuru.checkbox;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class CustomAdapter extends ArrayAdapter<DataModel> {


    DataModel[] items = null;
    Context context;
    ArrayList<String> checkedValues;
    boolean[] CheckBoxState;
   
    public CustomAdapter(Context context, DataModel[] resource) {
        super(context, R.layout.row,resource);
        // TODO Auto-generated constructor stub
       
        this.context=context;
        this.items=resource;
        CheckBoxState = new boolean[resource.length];
        checkedValues = new ArrayList<String>();
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
       
       
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.row, parent,false);
       
        final TextView stream = (TextView)convertView.findViewById(R.id.textView1);
        CheckBox ch = (CheckBox)convertView.findViewById(R.id.checkBox1);
       
        stream.setText(items[position].getStream());
       
   
        convertView.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
           
                Toast.makeText(context.getApplicationContext(), "you Clicked"+items[position].getStream(), Toast.LENGTH_LONG).show();
            }
        });
       
        ch.setChecked(false);
       
       
ch.setOnClickListener(new View.OnClickListener() {
       
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
           
        if(((CheckBox)v).isChecked()){
            CheckBoxState[position]=true;
            checkedValues.add(stream.getText().toString());
        Toast.makeText(context.getApplicationContext(), "You Have Selected: "+checkedValues, Toast.LENGTH_LONG).show();
        }
        else if(!((CheckBox)v).isChecked()){
           
            CheckBoxState[position]=false;
            checkedValues.remove(stream.getText().toString());
        }
           
       
        }
       
           
    });
       
   
       
        return convertView;
                }
   
   
    }


DataModel.java

package com.AndroidDevelopmentGuru.checkbox;



public class DataModel {

    String stream;
    int value; //for check box
   
    public DataModel(String string, int value) {
        // TODO Auto-generated constructor stub
       
        this.stream=string;
        this.value=value;
    }
    public DataModel() {
        // TODO Auto-generated constructor stub
    }
    public String getStream() {
        return stream;
    }
    public void setStream(String stream) {
        this.stream = stream;
    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
   
}











activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>







 In row.xml, keep the orientation of Linear layout horizontal for better view.

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

   

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

Download the Source code 

Output:





 

Comments

Popular posts from this blog

Android: Login Screen using Fragments

In this tutorial, our focus is on making a User/Member login activity using fragments. With the help of Fragments we will use the same activity to show User login area and also Members Login area. Prerequisite for this tutorial: You should be know how to make an Activity And most importantly you should have prior Knowledge of Fragments. For practising basic Fragment implementation refer to Android Simple Fragment Example      We have used only three activities for this: MainActivity(which represents the Login Screen) Fragments for Members area Fragment for New Users

Simple Login/Register Example using SQLite database

MainActivity.java(login Screen) package com.AndroidDevelopmentGuru.database_new; import java.util.List; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity {                                 EditText user, pass;                 Button login, not_reg;                 DatabaseHandler db;                 Cursor cursor;                                 @Override                 protected void onCreate(Bundle savedInstanceState) {                                 super.onCreate(savedInstanceState);                                 setContentView(R.layout.activity_main);

Android: Current Location Using Fused APi on Google Maps

This tutorial gives us the simple implementation of "Fused API" to fetch the current location on google map in android. Fused API is latest among all techniques to get the location. It provides you very precise results and also uses less battery of your device. It chooses GPS or Network provider to get to your current location. And it helps your device remember about the last saved location. Let's implement the Fused API to fetch/get the current location. Step 1: Create a new project in Android studio. and select Maps Activity.