2016-03-28 14 views
1

私はRecyclerViewでどこで間違いを犯したのか把握しようとしています。クリックは機能していません。このコードは私のアプリケーションに追加する私の更新のための準備ですので、コンストラクタとパラメータについて尋ねないでください。あなたが私を助けることができ、ここに私のコードです:Android RecyclerViewクリックリスナーが動作しない

public class PlanRecyclerAdapter extends RecyclerView.Adapter<PlanRecyclerAdapter.PlanViewHolder> { 

    private ClickListener clickListener; 
    private List<PlanRecycler> planList; 
    private boolean isDone[]; 
    private int count = 0; 
    private List<String> current_workout = new ArrayList<>(); 
    private String Plan_Name; 
    private Context context; 

    private Dbhelper dbhelper; 
    SQLController dbcon; 
    private SQLiteDatabase database; 
    public String rest = ""; 


    public PlanRecyclerAdapter(List<PlanRecycler> planList, boolean isDone[], int count, String Plan_Name, Context context, Dbhelper dbhelper, SQLController dbcon, SQLiteDatabase database) { 
     this.planList = new ArrayList<>(); 
     this.planList.addAll(planList); 
     this.isDone = isDone; 
     this.count = count; 
     this.Plan_Name = Plan_Name; 
     this.context = context; 
     this.dbhelper = dbhelper; 
     this.dbcon = dbcon; 
     this.database = database; 
    } 

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

    @Override 
    public void onBindViewHolder(PlanViewHolder holder, int position) { 

     PlanRecycler addWorkout = planList.get(position); 
     holder.Day_Holder.setText("Day " + (position + 1)); 
     holder.Workout_Name.setText(" - " + addWorkout.getPlan_Workout_Title()); 
     rest = addWorkout.getPlan_Workout_Title(); 

     current_workout.add(addWorkout.getPlan_Workout_Title()); 

    } 

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

    public class PlanViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

     protected TextView Workout_Name; 
     protected TextView Day_Holder; 

     public PlanViewHolder(View itemView) { 
      super(itemView); 

      itemView.setOnClickListener(this); 

      Typeface Roboto_Medium = Typeface.createFromAsset(itemView.getResources().getAssets(), "Roboto-Medium.ttf"); 
      Typeface Roboto_Regular = Typeface.createFromAsset(itemView.getResources().getAssets(), "Roboto-Regular.ttf"); 

      Day_Holder = (TextView) itemView.findViewById(R.id.plan_day_holder_id); 
      Day_Holder.setTypeface(Roboto_Regular); 

      Workout_Name = (TextView) itemView.findViewById(R.id.plan_workout_title); 
      Workout_Name.setTypeface(Roboto_Medium); 

     } 

     @Override 
     public void onClick(View v) { 

      if (clickListener != null) { 
       clickListener.itemClick(v, getPosition()); 
      } 
     } 
    } 

    public void setClickListener(ClickListener clickListener) { 
     this.clickListener = clickListener; 
    } 


    public interface ClickListener { 
     void itemClick(View view, int position); 
    } 
} 

第二部:

public class PlanAdapter extends AppCompatActivity implements PlanRecyclerAdapter.ClickListener{ 
....... 

    @Override 
    public void itemClick(View view, int position) { 

     Toast.makeText(this,"radi",Toast.LENGTH_SHORT).show(); 


     Bundle ExerciseBundle = new Bundle(); 
     ExerciseBundle.putString("plan_current_plan", Plan_Name); 
     ExerciseBundle.putString("plan_current_workout", current_workout.get(position)); 
     ExerciseBundle.putInt("plan_from_workout_id", position); 
     Intent i = new Intent(
       getApplicationContext(), 
       MenuRound.class); 

     i.putExtras(ExerciseBundle); 
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(i); 

    } 
} 
+0

ここで、setClickListener()を呼び出していますか? –

+0

Sir私はこのクラスを他の呼び出しで呼び出しています。このクラスを実装し、ClickListenerを呼び出すよりも –

+0

多分あなたはあなたがしようとしていることを見ることができるようにすべてのコードを共有することができます。 –

答えて

0

はちょうどこの

のように、viewholderへとitemViewのclickリスナー設定 onBindViewHolder内itemViewを追加
public class PlanRecyclerAdapter extends RecyclerView.Adapter<PlanRecyclerAdapter.PlanViewHolder> { 

private List<PlanRecycler> planList; 
private boolean isDone[]; 
private int count = 0; 
private List<String> current_workout = new ArrayList<>(); 
private String Plan_Name; 
private Context context; 

private Dbhelper dbhelper; 
SQLController dbcon; 
private SQLiteDatabase database; 
public String rest = ""; 


public PlanRecyclerAdapter(List<PlanRecycler> planList, boolean isDone[], int count, String Plan_Name, Context context, Dbhelper dbhelper, SQLController dbcon, SQLiteDatabase database) { 
    this.planList = new ArrayList<>(); 
    this.planList.addAll(planList); 
    this.isDone = isDone; 
    this.count = count; 
    this.Plan_Name = Plan_Name; 
    this.context = context; 
    this.dbhelper = dbhelper; 
    this.dbcon = dbcon; 
    this.database = database; 
} 

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

@Override 
public void onBindViewHolder(PlanViewHolder holder, int position) { 

    PlanRecycler addWorkout = planList.get(position); 
    holder.Day_Holder.setText("Day " + (position + 1)); 
    holder.Workout_Name.setText(" - " + addWorkout.getPlan_Workout_Title()); 
    rest = addWorkout.getPlan_Workout_Title(); 

    current_workout.add(addWorkout.getPlan_Workout_Title()); 
    holder.Item_View.setOnClickListener(new OnClickListener() {//set the click listener 
     @Override 
     public void onClick(View v) 
     { 
      // add code here 
     } 
    }); 
} 

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

public class PlanViewHolder extends RecyclerView.ViewHolder { 

    protected TextView Workout_Name; 
    protected TextView Day_Holder; 
    protected View Item_View;//add item view to PlanViewHolder 

    public PlanViewHolder(View itemView) { 
     super(itemView); 

     Typeface Roboto_Medium = Typeface.createFromAsset(itemView.getResources().getAssets(), "Roboto-Medium.ttf"); 
     Typeface Roboto_Regular = Typeface.createFromAsset(itemView.getResources().getAssets(), "Roboto-Regular.ttf"); 
     Item_View = itemView;//set item view 
     Day_Holder = (TextView) itemView.findViewById(R.id.plan_day_holder_id); 
     Day_Holder.setTypeface(Roboto_Regular); 

     Workout_Name = (TextView) itemView.findViewById(R.id.plan_workout_title); 
     Workout_Name.setTypeface(Roboto_Medium); 

    } 
} 
} 
+0

私は配置した最初の方法を優先しますが、私はあなたが私の神経を救うために追加したものを追加します。私のclickListenerがnullであることに気付きました。方法 –

関連する問題