I have a RecyclerView which has a checkbox and textview.Numbers 10,20,30,40... till 500 should be shown in textview.The Checked checkboxes should add the numbers in the textview corresponding to the checkbox.For eg. if User checks the value 10 only, the textView would show 10. If user checks 20 as well, then TextView would show 30 ( 20 +10). If user uncheck 10 again, the TextView would show 20, and so on.When i click on checkbox some random checkbox is also checked.I tried one solution in stackoverflow. It did not work.I am stuck with this.Please help me..Here is my code:
RecyclerAdapter.java:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerHolder>
{
@NonNull
@Override
public RecyclerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new RecyclerHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_lyt,parent,false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerHolder holder, final int position) {
holder.number.setText(Integer.toString((Integer) alldata.get(position)));
final String text=holder.number.getText().toString();
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.e("Checked","Checked");
if(checkeddata!=null)
{
if (checkeddata.size()==0)
{
checkeddata.add(text);
}
else {
if(checkeddata.contains(text))
{
checkeddata.remove(text);
}
else {
checkeddata.add(text);
}
}
Iterator iterator=checkeddata.iterator();
int sumnumber=0;
while (iterator.hasNext())
{
sumnumber= sumnumber+Integer.parseInt((String) iterator.next());
}
sum.setText(Integer.toString(sumnumber));
}
}
});
}
@Override
public int getItemCount() {
return alldata.size();
}
public class RecyclerHolder extends RecyclerView.ViewHolder
{
TextView number;
CheckBox checkBox;
public RecyclerHolder(View itemView) {
super(itemView);
number=itemView.findViewById(R.id.number);
checkBox=itemView.findViewById(R.id.check);
}
}
}
public void data()
{
int g=1;
for(int i=10;i<=500;i++)
{
if((i/10)==g)
{
g=g+1;
alldata.add(i);
}
}
}
recycler_lyt.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/number"
android:textSize="20sp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check"
android:checked="false"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
Aucun commentaire:
Enregistrer un commentaire