Android RecyclerView with Single and Multiple Selection

Jyotishgher Astrology
By -
0

 

Android getting value from selected radio button

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

private static final int TYPE_TRACE = 1;
private static final int TYPE_OBSER = 2;
Context context;
public List<GetDataAdapter> getDataAdapter;

public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context) {
super();

this.getDataAdapter = getDataAdapter;
this.context = context;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view;

view = LayoutInflater.from(context).inflate(R.layout.survey_check_diseases, viewGroup, false);
return new EmailViewHolder(view);

}


@Override
public int getItemViewType(int position) {

return TYPE_OBSER;

}

@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {


((EmailViewHolder) holder).setEmailDetails(getDataAdapter.get(position));


if (position % 2 == 0) {
holder.itemView.setBackgroundColor(0x80E0EEEE);
} else {
holder.itemView.setBackgroundColor(0XFFFFFF);
}


}


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

class CallViewHolder extends RecyclerView.ViewHolder {

TextView f, s, t, senglish;

CallViewHolder(@NonNull View itemView) {
super(itemView);
f = (TextView) itemView.findViewById(R.id.f);
s = (TextView) itemView.findViewById(R.id.s);
t = (TextView) itemView.findViewById(R.id.t);
senglish = (TextView) itemView.findViewById(R.id.senglish);

}

}


class EmailViewHolder extends RecyclerView.ViewHolder {

public TextView f, s, t, senglish;
// public CheckBox ok,nok,na;
private RadioGroup rdGroup;
RadioButton ok, nok, na;

EmailViewHolder(@NonNull View itemView) {
super(itemView);
f = (TextView) itemView.findViewById(R.id.f);
s = (TextView) itemView.findViewById(R.id.s);
senglish = (TextView) itemView.findViewById(R.id.senglish);

ok = (RadioButton) itemView.findViewById(R.id.ok);
nok = (RadioButton) itemView.findViewById(R.id.nok);
// na = (RadioButton) itemView.findViewById(R.id.na);

rdGroup = (RadioGroup) itemView.findViewById(R.id.rdGroup);

/* rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int selectedId = rdGroup.getCheckedRadioButtonId();
RadioButton yesnoradioButton = (RadioButton)itemView. findViewById(selectedId);
if(selectedId==-1){
Toast.makeText(context,"Nothing selected", Toast.LENGTH_SHORT).show();
yesnoradioButton.setChecked(false);

}
else{

yesnoradioButton.setChecked(true);

// Toast.makeText(context,yesnoradioButton.getText(), Toast.LENGTH_SHORT).show();
}
}
});*/

}

void setEmailDetails(GetDataAdapter getDataAdapter1) {

if (getDataAdapter1.getFinalize_flag().equalsIgnoreCase("UPDATE")) {

// Toast.makeText(context, getDataAdapter1.getCODE()+ok.getText(), Toast.LENGTH_SHORT).show();
// getDataAdapter1.setSelected(!getDataAdapter1.getSelected());
if (getDataAdapter1.getEntry_by().equalsIgnoreCase("Y")) { ////FLAG HAI YAH -YES OR NO
f.setText(getDataAdapter1.getDIS_ID());
senglish.setText(getDataAdapter1.getDIS_NAMES());
ok.setChecked(true);
nok.setChecked(false);
} else {
ok.setChecked(false);
nok.setChecked(true);
f.setText(getDataAdapter1.getDIS_ID());
senglish.setText(getDataAdapter1.getDIS_NAMES());
}
ok.setTag(getDataAdapter1);
////here is the logic,it will show lists with the inserted values in database as selected while others
non selected then we will need to update and select options as per our's wishes

ok.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
getDataAdapter1.setSelected(isChecked);
rdGroup.check(getDataAdapter1.getSelctedId());
rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
getDataAdapter1.setSelctedId(checkedId);
}
});

}
});
nok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDataAdapter1.setSelected(false);
// Toast.makeText(context, getDataAdapter1.getDIS_ID(), Toast.LENGTH_SHORT).show();


}
});



/* ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDataAdapter1.setSelected(true);



}
});*/

} else if (getDataAdapter1.getFinalize_flag().equalsIgnoreCase("SUBMIT")) {

f.setText(getDataAdapter1.getDIS_ID());
senglish.setText(getDataAdapter1.getDIS_NAME());
// s.setText(getDataAdapter1.getDESC_HINDI());


ok.setOnCheckedChangeListener(null);
ok.setChecked(getDataAdapter1.getSelected());
ok.setTag(getDataAdapter1);


ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDataAdapter1.setSelected(true);

// Toast.makeText(context, getDataAdapter1.getCODE()+ok.getText(), Toast.LENGTH_SHORT).show();
// getDataAdapter1.setSelected(!getDataAdapter1.getSelected());

}
});

}


}
}
}
UPDATE CLASS:

btnnext = (Button) findViewById(R.id.next);//on click collect values and use your code to update the
database


btnnext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
JSONArray Obserlists = null;
Obserlists = new JSONArray();

String flag = null;
for (int i = 0; i < GetDataAdapter3.size(); i++) {
Log.d("tracevalues1-", ":" + GetDataAdapter3.get(i).getEntry_by());

if(GetDataAdapter3.get(i).getSelctedId() == R.id.ok ){

flag = "Y";
// Toast.makeText(updateSurvey.this, flag, Toast.LENGTH_SHORT).show();
Log.d("tracevalues11-", ":" +flag);//GetDataAdapter3.get(i).getEntry_by()
}
else if (GetDataAdapter3.get(i).getSelctedId() == R.id.nok){
flag = "N";
// Toast.makeText(updateSurvey.this, flag, Toast.LENGTH_SHORT).show();
Log.d("tracevalues11-", ":" +flag);



}


JSONObject Obserlist = new JSONObject();
JSONObject ObserlistF = new JSONObject();
try {

Obserlist.put("obserFlag", flag);
Obserlist.put("traceValue", GetDataAdapter3.get(i).getDIS_ID());

Log.d("tracevalues11-", "onClick: " + GetDataAdapter3.get(i).getDIS_ID());

if (GetDataAdapter3.get(i).getSelected()) {
ObserlistF.put("traceValue", GetDataAdapter3.get(i).getDIS_ID());
Obserlists.put(ObserlistF);

Log.d("tracevalues111-", "onClick: " +GetDataAdapter3.get(i).getDIS_ID());
}

} catch (JSONException e) {
e.printStackTrace();
System.out.println("Error:" + e);
}


}
Log.d("tracevalues1111-", "onClick: " + Obserlists);///tracevalues11-: onClick: [{"obserFlag":"Y","obserCode":"1"},{"obserFlag":"Y","obserCode":"3"}]

// SubmitData(EMPNO, Obserlists);
}
});

Post a Comment

0Comments

Post a Comment (0)