2016-04-08 16 views
0

私はArrayListを使ってリストビューを作成しています。以下のxmlファイルの場合、listviewにはチェックボックスと2つのテキストビューが含まれています。私はまた、私のArrayListからの情報を保持するカスタムのチェックボックスクラスを使用しています。私のカスタムチェックボックス(ListCheckBox.java)クラス(下記)には、get_is_completeとset_is_completeというメソッドがあります。 set_is_completeメソッドは現在、私のArrayListからboloean値を取得しています。その結果、ArrayListに渡されたPlanetオブジェクトの値に応じて、チェックボックスがオンまたはオフのチェックボックスのリストビューが生成されます。 すべてうまく動作しますが、ユーザーがチェックボックスをクリックすると、set_is_completeのブール状態を変更する必要があります。私はうまく動作する私のアダプタクラス内で以下のsetOnClickListenerを作成しました。Android: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

私のモデル

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; } 

}のコードがある場合、私は私のアダプタクラス内で以下のif文で直接モデルを更新した場合は? – Krish

答えて

0

私はそれを理解することができました。作品

以下
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(); 
       } 

      } 

     }); 

あなたは直接モデル権を更新することができ

関連する問題