Skip to content

Instantly share code, notes, and snippets.

@mosluce
Last active April 3, 2016 08:50
Show Gist options
  • Save mosluce/72193ee22804198dde49f63ea673741b to your computer and use it in GitHub Desktop.
Save mosluce/72193ee22804198dde49f63ea673741b to your computer and use it in GitHub Desktop.
public class ChecklistAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int ROW_TITLE = 100;
public static final int ROW = 101;
public static final String ANS_YES = "1";
public static final String ANS_NO = "0";
public static final String ANS_DEFAULT = "-1";
private List<ChecklistRow> checklistRows;
private OnEditIconClickedListener onEditIconClickedListener;
public ChecklistAdapter(OnEditIconClickedListener onEditIconClickedListener, List<ChecklistRow> checklistRows) {
this.onEditIconClickedListener = onEditIconClickedListener;
this.checklistRows = checklistRows;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View layout;
switch (viewType) {
case ROW_TITLE:
layout = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_checklist_row_title, parent, false);
return new RowTitleViewHolder(layout);
case ROW:
layout = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_checklist_row, parent, false);
return new RowViewHolder(layout);
default:
return null;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == ROW_TITLE) {
ChecklistAns checklistAns = ((RowTitle) checklistRows.get(position)).getChecklistAns();
RowTitleViewHolder rowTitleViewHolder = (RowTitleViewHolder) holder;
rowTitleViewHolder.name.setText(checklistAns.getName());
} else if (getItemViewType(position) == ROW) {
Row row = (Row) checklistRows.get(position);
RowViewHolder rowViewHolder = (RowViewHolder) holder;
rowViewHolder.txt_id.setText("" + row.getListItem().getId());
rowViewHolder.txt_context.setText(row.getListItem().getContent());
if (row.getAns().getIsOK().equals(ANS_YES)) {
rowViewHolder.radio_yes.setChecked(true);
rowViewHolder.radio_no.setChecked(false);
} else if (row.getAns().getIsOK().equals(ANS_NO)) {
rowViewHolder.radio_yes.setChecked(false);
rowViewHolder.radio_no.setChecked(true);
} else if (row.getAns().getIsOK().equals(ANS_DEFAULT)) {
rowViewHolder.radio_yes.setChecked(false);
rowViewHolder.radio_no.setChecked(false);
}
// Log.d("test", "radio_yes: " + rowViewHolder.radio_yes.isChecked() + " Position: " + position);
// Log.d("test", "radio_no : " + rowViewHolder.radio_no.isChecked() + " Position: " + position);
}
}
@Override
public int getItemCount() {
return checklistRows.size();
}
@Override
public int getItemViewType(int position) {
return checklistRows.get(position).getViewType();
}
public class RowTitleViewHolder extends RecyclerView.ViewHolder {
TextView name;
public RowTitleViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
}
}
public class RowViewHolder extends RecyclerView.ViewHolder implements
CompoundButton.OnCheckedChangeListener, View.OnClickListener {
TextView txt_id;
TextView txt_context;
RadioButton radio_yes;
RadioButton radio_no;
ImageView img_edit;
public RowViewHolder(View itemView) {
super(itemView);
txt_id = (TextView) itemView.findViewById(R.id.txt_id);
txt_context = (TextView) itemView.findViewById(R.id.txt_context);
radio_yes = (RadioButton) itemView.findViewById(R.id.radio_yes);
radio_no = (RadioButton) itemView.findViewById(R.id.radio_no);
img_edit = (ImageView) itemView.findViewById(R.id.img_edit);
radio_yes.setOnCheckedChangeListener(this);
radio_no.setOnCheckedChangeListener(this);
img_edit.setOnClickListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isPressed()) {
Log.d("test", "isPressed");
Row row = (Row) checklistRows.get(getAdapterPosition());
Ans ans = row.getAns();
if (buttonView.equals(radio_yes) && isChecked) {
ans.setIsOK(ANS_YES);
} else if (buttonView.equals(radio_no) && isChecked) {
ans.setIsOK(ANS_NO);
}
ans.update();
notifyItemChanged(getAdapterPosition());
}
}
@Override
public void onClick(View v) {
onEditIconClickedListener.onEditIconClicked((Row) checklistRows.get(getAdapterPosition()));
}
}
public interface OnEditIconClickedListener {
void onEditIconClicked(Row row);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment