[英]Android: ListView of Checkboxes to React to setOnClickListener
I'm using an array adapter to populate a listview from an ArrayList. 我正在使用数组适配器从ArrayList填充列表视图。 Per the below xml file, the listview contains a checkbox and two textviews. 对于下面的xml文件,列表视图包含一个复选框和两个文本视图。 I'm also using a custom checkbox class that holds information from my ArrayList. 我还使用了一个自定义复选框类,该类包含来自ArrayList的信息。 My custom checkbox (ListCheckBox.java) class (below) has a method called get_is_complete and set_is_complete. 我的自定义复选框(ListCheckBox.java)类(如下)具有名为get_is_complete和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. set_is_complete方法当前正在从我的ArrayList中获取boloean值,结果生成了一个复选框的列表视图,其中,根据在ArrayList中传递的Planet对象的值,该复选框是选中还是未选中。 Everything works fine, however I need to change the boolean state of set_is_complete when a user clicks on the checkbox. 一切正常,但是当用户单击复选框时,我需要更改set_is_complete的布尔状态。 I created the below setOnClickListener within my adapter class that works fine. 我在适配器类中创建了下面的setOnClickListener,它工作正常。
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. 问题在于,最初需要根据数组中的初始值来评估set_is_complete方法,但是用户在单击复选框时应该能够更改set_is_complete方法。 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 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 下面是我的PlanetAdpapter.java类
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 下面是我的MainActivity.java类
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 自定义复选框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 如果我通过Adapter类中的以下if语句直接更新模型,则可以正常工作
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;
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.