I'm using an array adapter to populate a listview from an ArrayList. Per the below xml file, the listview contains a checkbox and two textviews. I'm also using a custom checkbox class that holds information from my ArrayList. My custom checkbox (ListCheckBox.java) class (below) has a method called get_is_complete and set_is_complete. The set_is_complete method is currently taking boloean values from my ArrayList and as a result generates a listview of checkboxes where the checkbox is either checked or unchecked depending on the value of Planet object which is passed in the ArrayList. Everything works fine, however I need to change the boolean state of set_is_complete when a user clicks on the checkbox. I created the below setOnClickListener within my adapter class that works fine.
The problem is that initially the set_is_complete method needs to be evaluated based on inital values in the array, but the user should be able to change set_is_complete method when they click on the checkbox. I was thinking of the below of conditional statement, but I'm stuck on the condition. Please help.
Possible conditional statement, but I'm stuck on the condition.
if (condiition){
holder.chkBox.set_is_complete(p.isSelected());
}else{
holder.chkBox.set_is_complete(boolean_value);
}
single_list_view_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false">
<com.example.chad.checkbox.ListCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chk_box"
android:layout_alignParentLeft="true"
android:textSize="40dp"
/>
<TextView
android:text="Mercury"
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/chk_box"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:text="570000000"
android:id="@+id/dist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_toRightOf="@id/chk_box"
android:textSize="16sp"
android:textStyle="italic" />
</RelativeLayout>
Below is my PlanetAdpapter.java class
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import com.example.chad.checkbox.ListCheckBox;
/**
* Created by Chad on 3/23/2016.
*/
class Planet{
String name;
int distance;
boolean selected;
public Planet(String name, int distance, boolean selected) {
super();
this.name = name;
this.distance = distance;
this.selected = selected;
}
public String getName() {
return name;
}
public int getDistance() {
return distance;
}
public boolean isSelected() {
return selected;
}
public void setName(String name) {
this.name = name;
}
public void setDistance(int distance) {
this.distance = distance;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
public class PlanetAdapter extends ArrayAdapter<Planet> {
private List<Planet> planetList;
private Context context;
View v;
PlanetHolder holder;
Boolean boolean_value;
public PlanetAdapter(List<Planet> planetlist, Context context) {
super(context, R.layout.single_list_view_item, planetlist);
this.planetList = planetlist;
this.context = context;
}
private static class PlanetHolder{
public TextView planetName;
public TextView distView;
public ListCheckBox chkBox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
//View v = convertView;
v = convertView;
//PlanetHolder holder = new PlanetHolder();
holder = new PlanetHolder();
if(v ==null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.single_list_view_item, null);
holder.planetName = (TextView)v.findViewById(R.id.name);
holder.distView = (TextView)v.findViewById(R.id.dist);
holder.chkBox = (ListCheckBox)v.findViewById(R.id.chk_box);
v.setTag(holder);
//holder.chkBox.setOnCheckedChangeListener((MainActivity) context);
}else{
holder = (PlanetHolder)v.getTag();
}
Planet p = planetList.get(position);
holder.planetName.setText(p.getName());
holder.distView.setText("" + p.getDistance());
holder.chkBox.set_is_complete(p.isSelected());
final PlanetHolder finalHolder = holder;
final String name;
name = p.getName();
holder.chkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (finalHolder.chkBox.isChecked()) {
boolean_value = true;
Toast.makeText(v.getContext(), name + "Is Checked -- " + boolean_value.toString(), Toast.LENGTH_LONG).show();
} else {
boolean_value = false;
Toast.makeText(v.getContext(), name + "Is Unchecked -- " + boolean_value.toString(), Toast.LENGTH_LONG).show();
}
}
});
holder.chkBox.setPlanet(p.getName());
holder.chkBox.setDistance(p.getDistance());
if(holder.chkBox.get_is_complete() == false){
holder.chkBox.setChecked(false);
}else{
holder.chkBox.setChecked(true);
}
holder.chkBox.setTag(p);
return v;
}
}
Below is my MainActivity.java Class
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends Activity {
ListView lv;
ArrayList<Planet> planetList;
PlanetAdapter plAdapter;
View vv;
ListCheckBox chBox;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.listview);
displayPlanetList();
vv = lv.getAdapter().getView(0, null,null);
chBox = (ListCheckBox) vv.findViewById(R.id.chk_box);
String p = chBox.getPlanet();
Toast.makeText(getApplicationContext(), p, Toast.LENGTH_LONG).show();
b = (Button)findViewById(R.id.btn);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer c = plAdapter.getCount();
Integer lcount = lv.getCount();
vv = lv.getAdapter().getView(0, null, null);
chBox = (ListCheckBox) vv.findViewById(R.id.chk_box);
Boolean bb = chBox.get_is_complete();
String x = bb.toString();
Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
}
});
}
private void displayPlanetList(){
planetList = new ArrayList<Planet>();
planetList.add(new Planet("Mercury", 5700000, true));
planetList.add(new Planet("Venus", 2370000, false));
planetList.add(new Planet("Mars", 3500000, true));
planetList.add(new Planet("Jupiter", 5000000, false));
planetList.add(new Planet("Saturn", 7460000, true));
plAdapter = new PlanetAdapter(planetList, this);
lv.setAdapter(plAdapter);
}
}
Custom checkbox ListCheckBox.java
import android.annotation.TargetApi;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.CheckBox;
public class ListCheckBox extends CheckBox {
private String planet;
private Boolean is_complete;
private Integer distance;
public ListCheckBox(Context context) {
super(context);
//setButtonDrawable(new StateListDrawable());
}
public ListCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
//setButtonDrawable(new StateListDrawable());
}
public ListCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
super(context,attrs,defStyleAttr);
//setButtonDrawable(new StateListDrawable());
}
@TargetApi(21)
public ListCheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{super(context,attrs,defStyleAttr,defStyleRes);}
public String getPlanet(){
return planet;
}
public void setPlanet(String planet){
this.planet = planet;
}
public Boolean get_is_complete(){return is_complete;}
public void set_is_complete(Boolean is_complete){
this.is_complete = is_complete;
}
public Integer getDistance(){
return distance;
}
public void setDistance(Integer distance){
this.distance = distance;
}
}
I was able to figure it out. If I update the model directly via the below if statement within my Adapter class if works
final TaskHolder finalHolder = holder;
final String name;
name = t.getMy_task_name();
holder.chkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (finalHolder.chkBox.isChecked()) {
//boolean_value = true;
t.setMy_task_is_complete(true);
Toast.makeText(v.getContext(), name + "Is Checked -- " + t.getMy_task_is_complete().toString(), Toast.LENGTH_LONG).show();
} else {
//boolean_value = false;
t.setMy_task_is_complete(false);
Toast.makeText(v.getContext(), name + "Is Unchecked -- " + t.getMy_task_is_complete().toString(), Toast.LENGTH_LONG).show();
}
}
});
Below is the code for my model
public class Task {
Integer my_task_id;
String my_task_name;
Boolean my_task_is_complete;
Integer my_barcode;
public Task(){
}
public Task(Integer my_task_id, String my_task_name, Boolean my_task_is_complete, Integer my_barcode){
super();
this.my_task_id = my_task_id;
this.my_task_name = my_task_name;
this.my_task_is_complete = my_task_is_complete;
this.my_barcode = my_barcode;
}
public Integer getMy_task_id() {
return my_task_id;
}
public void setMy_task_id(Integer my_task_id) {
this.my_task_id = my_task_id;
}
public String getMy_task_name() {
return my_task_name;
}
public void setMy_task_name(String my_task_name) {
this.my_task_name = my_task_name;
}
public Boolean getMy_task_is_complete() {
return my_task_is_complete;
}
public void setMy_task_is_complete(Boolean my_task_is_complete) {
this.my_task_is_complete = my_task_is_complete;
}
public Integer getMy_barcode() {
return my_barcode;
}
public void setMy_barcode(Integer my_barcode) {
this.my_barcode = my_barcode;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.