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:
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
Post a Comment