2016-04-08 81 views
0

我正在使用數組適配器從ArrayList填充列表視圖。根據下面的xml文件,listview包含一個複選框和兩個textview。我也使用自定義的複選框類來保存我的ArrayList中的信息。我的自定義複選框(ListCheckBox.java)類(如下)有一個名爲get_is_complete和set_is_complete的方法。 set_is_complete方法當前正從我的ArrayList中獲取boloean值,並因此生成複選框的列表視圖,其中複選框被選中或取消選中,取決於在ArrayList中傳遞的Planet對象的值。 一切工作正常,但是當用戶點擊複選框時,我需要更改set_is_complete的布爾狀態。我在我的適配器類中創建了下面的setOnClickListener,它工作正常。Android:ListView的複選框,以響應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; 
    } 
} 
+0

您可以直接更新模型右邊的代碼? – Krish

回答

0

我能弄明白。如果我直接通過下面的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; 
} 

}