vendredi 9 mars 2018

getting checkbox values from a reyclerview android

I am a newbie in android. I am trying to get values of a checkbox in a recyclerview which contains a header and button after the recyclerview but I am getting an error. My adapter

package com.example.system2.tranxav.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.system2.tranxav.R;
import com.example.system2.tranxav.model.Problems;

import java.util.List;


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{

Context context;
private List<Problems> mDataset;
private static final int TYPE_HEADER = 0;
private static final int TYPE_FOOTER = 1;
private static final int TYPE_ITEM = 2;

public class ViewHolder extends RecyclerView.ViewHolder{
    public TextView tvProblems, tvPrice;
    public Button btnProblem;
    Button btnSubmitProblem;
    CheckBox cbProblems;
    public ViewHolder(View itemView) {
        super(itemView);
        tvProblems = (TextView) itemView.findViewById(R.id.tvProblems);
        tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
        btnSubmitProblem = (Button) itemView.findViewById(R.id.btnProblem);
        cbProblems = (CheckBox) itemView.findViewById(R.id.cbProblems);

    }
}

public void add(int position, Problems item){
    mDataset.add(position, item);
    notifyItemInserted(position);
}

public void remove(Problems item){
    int position = mDataset.indexOf(item);
    mDataset.remove(position);
    notifyItemRemoved(position);
}

public MyAdapter(List<Problems> myDataset){
    mDataset = myDataset;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


    View itemView;
    if (viewType == TYPE_ITEM) {
        //Inflating recycle view item layout
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_problems, parent, false);
        return new ItemViewHolder(itemView);

    } else if (viewType == TYPE_HEADER) {
        //Inflating header view
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_recycler_header, parent, false);
        return new HeaderViewHolder(itemView);
    } else if (viewType == TYPE_FOOTER) {
        //Inflating footer view
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_recycler_footer, parent, false);
        return new FooterViewHolder(itemView);
    }
    throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}

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

    try {
    if (holder instanceof HeaderViewHolder) {

        HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
        headerHolder.tvHeader.setText("Please kindly ask the mechanic what the problem is and check them to continue");
        headerHolder.tvHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "You clicked at Header View!", Toast.LENGTH_SHORT).show();
            }
        });
    } else if (holder instanceof FooterViewHolder) {
        FooterViewHolder footerHolder = (FooterViewHolder) holder;
        footerHolder.btnSubmitProblem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                CheckboxViewHolder checkboxViewHolder = (CheckboxViewHolder) holder;
                checkboxViewHolder.cbProblems.setOnClickListener(null);
                checkboxViewHolder.cbProblems.setChecked(mDataset.get(position).isSelected());
                checkboxViewHolder.cbProblems.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        mDataset.get(holder.getAdapterPosition()).setSelected(isChecked);

                    }
                });
            }
        });
    }
    else if (holder instanceof ItemViewHolder) {
        ItemViewHolder itemView = (ItemViewHolder) holder;
        itemView.tvPrice.setText(mDataset.get(position-1).getPrice());
        itemView.tvProblems.setText(mDataset.get(position-1).getProblems());

    }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

@Override
public int getItemCount() {
    return mDataset.size()+2;
}

public int getItemViewType(int position) {

    if (position == 0) {
        return TYPE_HEADER;
    } else if (position == mDataset.size() + 1) {
        return TYPE_FOOTER;
    }
    return TYPE_ITEM;
}

private class HeaderViewHolder extends MyAdapter.ViewHolder {
    TextView tvHeader;

    public HeaderViewHolder(View view) {
        super(view);
        tvHeader = (TextView) view.findViewById(R.id.tvHeader);
    }
}

private class FooterViewHolder extends MyAdapter.ViewHolder {
    Button btnSubmitProblem;

    public FooterViewHolder(View view) {
        super(view);
        btnSubmitProblem = (Button) view.findViewById(R.id.btnProblem);
    }
}

private class ItemViewHolder extends MyAdapter.ViewHolder {
    TextView tvProblems, tvPrice;

    public ItemViewHolder(View itemView) {
        super(itemView);
        tvProblems = (TextView) itemView.findViewById(R.id.tvProblems);
        tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
    }
}

private class CheckboxViewHolder extends MyAdapter.ViewHolder {
    CheckBox cbProblems;

    public CheckboxViewHolder(View itemView) {
        super(itemView);
        cbProblems = (CheckBox) itemView.findViewById(R.id.cbProblems);

    }
}

}

Logcat

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.system2.tranxav, PID: 5177
              java.lang.ClassCastException: com.example.system2.tranxav.adapters.MyAdapter$FooterViewHolder cannot be cast to com.example.system2.tranxav.adapters.MyAdapter$CheckboxViewHolder
                  at com.example.system2.tranxav.adapters.MyAdapter$2.onClick(MyAdapter.java:129)
                  at android.view.View.performClick(View.java:5610)
                  at android.view.View$PerformClick.run(View.java:22260)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

I have implemented the recyclerview, the header, the checkbox and the submit button my problem now is getting the values of the selected checkbox.




Aucun commentaire:

Enregistrer un commentaire