CustomAdapter.java
package com.example.admin.listviewcheckbox;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.List;
/**
* Created by admin on 12-02-2016.
*/
public class CustomAdapter extends ArrayAdapter {
Model[] modelItems=null;
Context context;
ListView listview;
LayoutInflater inflater=null;
public CustomAdapter(Context context, Model[] resource, ListView listView) {
super(context,R.layout.row_view, resource);
this.listview=listView;
this.context=context;
this.modelItems=resource;
inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public class Holder
{
CheckBox checkBox;
TextView tv;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder;
View rowView = null;
if(convertView==null) {
rowView = inflater.inflate(R.layout.row_view, null);
holder=new Holder();
holder.tv=(TextView) rowView.findViewById(R.id.text_view);
holder.checkBox= (CheckBox) rowView.findViewById(R.id.check_box);
holder.tv.setText(modelItems[position].getName());
if(modelItems[position].getValue()==1) {
holder.checkBox.setChecked(true);
}
else
{
holder.checkBox.setChecked(false);
}
rowView.setTag(holder);
}
else {
holder= (Holder) convertView.getTag();
}
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<modelItems.length;i++)
{
modelItems[i]= new Model(modelItems[i].getName(),0);
}
modelItems[position]= new Model(modelItems[position].getName(),1);
listview.setAdapter(new CustomAdapter(context,modelItems,listview));
}
});
return rowView;
}
}
MainActivity.java
package com.example.admin.listviewcheckbox;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Model[] models;
CustomAdapter adapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
models=new Model[7];
models[0]=new Model("Dotsquares",0);
models[1]=new Model("Sony",0);
models[2]=new Model("Samsung",0);
models[3]=new Model("Apple",0);
models[4]=new Model("Lenovo",1);
models[5]=new Model("Intex",0);
models[6]=new Model("Iball",0);
listView= (ListView) findViewById(R.id.list_item);
adapter=new CustomAdapter(this,models,listView);
listView.setAdapter(adapter);
Button saveButton= (Button) findViewById(R.id.save);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer selectedText=new StringBuffer();
selectedText.append("SELECTED TEXTS ARE: \n\n");
Model[] getModel=adapter.modelItems;
for(int i=0;i<getModel.length;i++)
{
if(getModel[i].getValue()==1)
{
selectedText.append(getModel[i].getName()+"\n");
}
}
Toast.makeText(getApplicationContext(),selectedText,Toast.LENGTH_LONG).show();
}
});
}
}
Model.java
package com.example.admin.listviewcheckbox;
/**
* Created by admin on 12-02-2016.
*/
public class Model {
String name;
int value;
Model(String name,int value)
{
this.name=name;
this.value=value;
}
public String getName()
{
return name;
}
public int getValue()
{
return value;
}
}
Layouts
activity_main.xml
<LinearLayoutandroid:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:paddingTop="14dp"
android:gravity="center_horizontal"/>
</LinearLayout>
row_view.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="horizontal">
<CheckBox
android:id="@+id/check_box"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="test"
android:textSize="25dp"/>
</LinearLayout>
0 comments:
Post a Comment