Listview implement to checkbox in android.checkbox is use to multiple item checked.listview set adapter to checkbox when user check item to display toast. and customize adapter to listview in android. In this example of implement custom layout adapter and create class of getter setter and get data in list and set data to send adapter and display.It can implement to checkbox OnCheckedChanged click listener to user check item to display toast.
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </RelativeLayout>
Create Custom Layout in XML File
item_row.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="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_margin="10dp" android:padding="10dp"> <CheckBox android:id="@+id/check_box" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
Create Class Of Getter Setter Methods Implements.
Versions.java
package com.example.bhaumik.listviewchechbox; public class Versions { public String version; boolean isSelected = true; public Versions(String version) { this.version = version; } public Versions() { } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public boolean isSelected() { return isSelected; } public void setSelected(boolean selected) { isSelected = selected; } }
MainActivity.java
package com.example.bhaumik.listviewcheckbox; import android.graphics.Color; import android.support.v4.view.MenuItemCompat; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.CheckedTextView; import android.widget.CompoundButton; import android.widget.ListView; import android.widget.SearchView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { ListView listView; List<Versions> list = new ArrayList<>(); VersionAdapter versionAdapter; String[] version = {"Aestro","Blender","CupCake","Donut","Eclair","Froyo","GingerBread","HoneyComb","IceCream Sandwich", "Jelly Bean","Kitkat","Lolipop","Marshmallow","Nought","Oreo"}; SwipeRefreshLayout swipeRefreshLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.list_view); list = new ArrayList<>(); list.add(new Versions("Aestro")); list.add(new Versions("Blender")); list.add(new Versions("CupCake")); list.add(new Versions("Donut")); list.add(new Versions("Eclair")); list.add(new Versions("Froyo")); list.add(new Versions("GingerBread")); list.add(new Versions("HoneyComb")); list.add(new Versions("IceCream Sandwich")); list.add(new Versions("Jelly Bean")); list.add(new Versions("Kitkat")); list.add(new Versions("Lolipop")); list.add(new Versions("Marshmallow")); list.add(new Versions("Nought")); list.add(new Versions("Oreo")); versionAdapter = new VersionAdapter(MainActivity.this,R.layout.item_row,list); listView.setAdapter(versionAdapter); } @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { int position = listView.getPositionForView(compoundButton); if (position != ListView.INVALID_POSITION){ Versions versions = list.get(position); versions.setSelected(b); Toast.makeText(this, "Selected : "+ versions.getVersion(), Toast.LENGTH_SHORT).show(); } } }
Create Adapter And ViewHolder of Listview And Set Data
VersionAdapter.java
package com.example.harsh.listviewradiobutton; import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class VersionAdapter extends ArrayAdapter<Versions>{ Context context; List<Versions> list = new ArrayList<>(); public VersionAdapter(@NonNull Context context,int resource, List<Versions> list) { super(context, resource, list); this.context = context; this.list = list; list.addAll(list); } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { VersionHolder holder = new VersionHolder(); if (convertView == null){ LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.item_row,null); holder.checkBox = convertView.findViewById(R.id.check_box); holder.textView = convertView.findViewById(R.id.tv_name); holder.checkBox.setOnCheckedChangeListener((MainActivity)context); convertView.setTag(holder); }else{ holder = (VersionHolder) convertView.getTag(); } Versions versions = list.get(position); holder.textView.setText(versions.getVersion()); holder.checkBox.setTag(list); return convertView; } public static class VersionHolder{ public CheckBox checkBox; public TextView textView; } }
0 comments:
Post a Comment