2017-02-09 2 views
0

私は、リサイクルビューにスイッチ、クロノメーター、およびテキストビューを埋め込んでいます。このリサイクルビューには、スイッチのブール値、テキストビューの文字列、時間の長さを渡すarraylistのデータが挿入されています。ここで私の問題は、スイッチのリスナーをどこに置く必要がありますか?リスナーをアダプタクラスに配置すると、値を格納するarraylistは参照できず、メインのtimeKeeping.javaクラスにリスナーを置くと、スイッチを参照できなくなります。私はアンドロイドデベロッパーには新人だから、明らかに何かが欠けているかもしれない。これまでに書いたすべてのコードを提供します。ここで RecyclerView内のスイッチにonClickリスナーを使用

はここに私のメインのtimeKeeping.javaクラス

public class timeKeeping extends AppCompatActivity { 
    private List<timeTrackCell> timesList = new ArrayList<>(); 
    //TODO replace empName with data from server 
    public String empName = "Zach"; 
    private RecyclerView rv; 
    public timeTrackCellAdapter tAdapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //Button that shows who is logged in 
     setContentView(R.layout.activity_time_keeping2); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setImageResource(R.drawable.ic_temp_profile_image); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       String greetingString = "Welcome back, " + empName + "!"; 
       Snackbar.make(view, greetingString, Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     rv = (RecyclerView) findViewById(R.id.timeListTest); 
     tAdapter = new timeTrackCellAdapter(timesList); 
     RecyclerView.LayoutManager tLayoutManager = new LinearLayoutManager(getApplicationContext()); 
     rv.setLayoutManager(tLayoutManager); 
     rv.setItemAnimator(new DefaultItemAnimator()); 
     rv.setAdapter(tAdapter); 

     prepareData(); 

    } 

であること。ここ

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="I took this out because it contained my name"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

     <DatePicker 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:datePickerMode="spinner" 
      android:id="@+id/dateSelect" 
      android:calendarViewShown="false" 
      android:layout_marginTop="-100dp" 
      android:layout_marginBottom="-110dp" 
      android:translationY="-35dp" 
      ></DatePicker> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_time_keeping2" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     app:srcCompat="@android:drawable/ic_dialog_email" /> 


    <android.support.v7.widget.RecyclerView 
     android:id="@+id/timeListTest" 
     android:layout_gravity="start" 
     android:layout_below="@id/toolbar" 
     android:layout_height="fill_parent" 
     android:layout_width="match_parent" 
     android:clipToPadding="false" 
     android:paddingTop="55dp" 
     android:paddingBottom="70dp" 
     android:divider="@android:color/transparent" 
     android:focusable="true" 
     android:scrollbars="vertical" 
     ></android.support.v7.widget.RecyclerView> 


</android.support.design.widget.CoordinatorLayout> 

に対応したXMLファイルである私のメインクラスと私のアダプタを綴じる私timeKeepingCell.javaクラスです一緒に

public class timeTrackCell { 
     private boolean switchPosition; 
     private long chronometerTime; 
     private String jobString; 

     public timeTrackCell(boolean switchPosition, long chronometerTime, String jobString){ 
      this.switchPosition = switchPosition; 
      this.chronometerTime = chronometerTime; 
      this.jobString = jobString; 
     } 
     //sets 
     public void setSwitchPosition(boolean switchPosition){ 
      this.switchPosition = switchPosition; 
     } 
     public void setChronometerTime(long chronometerTime){ 
      this.chronometerTime = chronometerTime; 
     } 
     public void setJobString(String jobString){ 
      this.jobString = jobString; 
     } 
     //gets 
     public boolean getSwitchPosition(){ 
      return switchPosition; 
     } 
     public long getChronometerTime(){ 
      return chronometerTime; 
     } 
     public String getJobString(){ 
      return jobString; 
     } 


    } 

ここに私のアダプターである私のtimeTrackCellAdapter.javaクラスがありますター

public class timeTrackCellAdapter extends RecyclerView.Adapter<timeTrackCellAdapter.MyViewHolder> { 
    private List<timeTrackCell> jobDataList; 

    public class MyViewHolder extends RecyclerView.ViewHolder { 
     public TextView jobDesc; 
     public Switch jobSwitch; 
     public Chronometer jobTime; 

     public MyViewHolder(View view) { 

      super(view); 
      jobDesc = (TextView) view.findViewById(R.id.secondaryRowText); 
      jobSwitch = (Switch) view.findViewById(R.id.timeSwitch); 
      jobTime = (Chronometer) view.findViewById(R.id.timeTracker); 
     } 


    } 

    public timeTrackCellAdapter(List<timeTrackCell> jobDataList) { 
     this.jobDataList = jobDataList; 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.cell_layout, parent, false); 
     return new MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
     timeTrackCell tView = jobDataList.get(position); 
     holder.jobDesc.setText(tView.getJobString()); 
     holder.jobSwitch.setChecked(tView.getSwitchPosition()); 
     long elapsedRealtimeOffset = SystemClock.elapsedRealtime() - tView.getChronometerTime(); 
     holder.jobTime.setBase(elapsedRealtimeOffset); 
    } 

    @Override 
    public int getItemCount() { 
     return jobDataList.size(); 
    } 

} 

そしてここでは、カスタムセルのためのXMLファイルです私はあなたがあれば、次のコードは、onBindViewHolderの内側に行くことができsetOnCheckedChangeListenerなくonClickListener

を実装する必要がSwitchについては

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="140px" 
    > 
    <!--140px Seems to be the right height for 7 cells per page--> 
    <!-- Block for custom listview items --> 
    <Chronometer 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/timeTracker" 
     android:layout_gravity="left" 
     android:textSize="25sp" 
     android:paddingLeft="10px" 
     android:layout_centerVertical="true"> 
    </Chronometer> 

    <TextView 
     android:id="@+id/secondaryRowText" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/timeTracker" 
     android:textSize="15sp" 
     android:paddingLeft="10px" 
     android:paddingTop="30px" 
     > 
    </TextView> 

    <Switch 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/timeSwitch" 
     android:gravity="right" 
     android:layout_centerVertical="true" 

     > 
    </Switch> 
</RelativeLayout> 

答えて

0

を構築しましたRecyclerView内の各スイッチごとに、または独立している場合はViewHolderの内部で何か異なる操作を実行する必要があります。

jobSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     // do something, the isChecked param will be 
     // true if the switch is in the On position 
    } 
}); 
関連する問題