簡體   English   中英

Android:復選框的ListView響應setOnClickListener

[英]Android: ListView of Checkboxes to React to setOnClickListener

我正在使用數組適配器從ArrayList填充列表視圖。 對於下面的xml文件,列表視圖包含一個復選框和兩個文本視圖。 我還使用了一個自定義復選框類,該類包含來自ArrayList的信息。 我的自定義復選框(ListCheckBox.java)類(如下)具有名為get_is_complete和set_is_complete的方法。 set_is_complete方法當前正在從我的ArrayList中獲取boloean值,結果生成了一個復選框的列表視圖,其中,根據在ArrayList中傳遞的Planet對象的值,該復選框是選中還是未選中。 一切正常,但是當用戶單擊復選框時,我需要更改set_is_complete的布爾狀態。 我在適配器類中創建了下面的setOnClickListener,它工作正常。

問題在於,最初需要根據數組中的初始值來評估set_is_complete方法,但是用戶在單擊復選框時應該能夠更改set_is_complete方法。 我在想以下條件語句,但是我被條件束縛了。 請幫忙。

可能的條件語句,但我受條件限制。

    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>

下面是我的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;

    }




}

下面是我的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);

        }


}

自定義復選框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;
    }
}

我能夠弄清楚。 如果我通過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();
                }

            }

        });

下面是我的模型的代碼

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM