I want to count the clicks for each item in my recycler View. In this recycler view there are some sort of contacts. I want that there is a counter (for every item for its own) so that I click for example contact 1 (then contact 1 has one click), then I click on contact 2 and after that I click again on contact 2 (then contact 2 has two clicks) --> but contact 1 has still only 1 click.
and so on.
I already tried it a few times, but the problem is, that when I create the counter and click on the item / cardview then the counter just continues and the second contact has then for example 3 clicks, because the counter just counts the clicks and not the clicks on the cardview / item.
How can I fix this ?
public class MainActivity extends AppCompatActivity {
ArrayList<Object> arrContacts = new ArrayList<>();
FloatingActionButton btnOpenDialog;
RecyclerView recyclerView;
RecyclerContactAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerContact);
btnOpenDialog = findViewById(R.id.btnOpenDialog);
btnOpenDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.add_update_lay);
EditText edtName = dialog.findViewById(R.id.edtName);
EditText edtNumber = dialog.findViewById(R.id.edtNumber);
Button btnAction = dialog.findViewById(R.id.btnAction);
btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = "", number = "";
if(!edtName.getText().toString().equals("")) {
name = edtName.getText().toString();
} else {
Toast.makeText(MainActivity.this, "Please Enter Contact Name!", Toast.LENGTH_SHORT).show();
}
if(!edtNumber.getText().toString().equals("")) {
number = edtNumber.getText().toString();
} else {
Toast.makeText(MainActivity.this, "Please Enter Contact Number!", Toast.LENGTH_SHORT).show();
}
arrContacts.add(new ContactModel(R.drawable.ic_baseline_person_24,name, number));
adapter.notifyItemInserted(arrContacts.size()-1);
recyclerView.scrollToPosition(arrContacts.size()-1);
dialog.dismiss();
}
});
dialog.show();
}
});
recyclerView.setLayoutManager(new LinearLayoutManager(this));
arrContacts.add(new ContactModel(R.drawable.ic_baseline_person_24,"a","1234"));
arrContacts.add(new ContactModel(R.drawable.ic_baseline_person_24,"b","3456"));
arrContacts.add(new ContactModel(R.drawable.ic_baseline_person_24,"c","4567"));
arrContacts.add(new ContactModel(R.drawable.ic_baseline_person_24,"d","5678"));
adapter = new RecyclerContactAdapter(this, arrContacts);
recyclerView.setAdapter(adapter);
}
}
public class ContactModel {
int img;
String name, number;
Integer counter;
public ContactModel(int img, String name, String number){
this.name = name;
this.number = number;
this.img = img;
}
public ContactModel(String name, String number){
this.name = name;
this.number = number;
}
}
public class RecyclerContactAdapter extends RecyclerView.Adapter<RecyclerContactAdapter.ViewHolder> {
Context context;
ArrayList<Object> arrContacts;
RecyclerContactAdapter(Context context, ArrayList<Object> arrContacts){
this.context = context;
this.arrContacts = arrContacts;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
View view = LayoutInflater.from(context).inflate(R.layout.contact_row, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position){
ContactModel model = (ContactModel) arrContacts.get(position);
holder.imgContact.setImageResource(model.img);
holder.txtName.setText(model.name);
holder.txtNumber.setText(model.number);
holder.IlRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.add_update_lay);
EditText edtName = dialog.findViewById(R.id.edtName);
EditText edtNumber = dialog.findViewById(R.id.edtNumber);
Button btnAction = dialog.findViewById(R.id.btnAction);
TextView txtTitle = dialog.findViewById(R.id.txtTitle);
txtTitle.setText("Update Contact");
btnAction.setText("Update");
edtName.setText(((ContactModel) arrContacts.get(position)).name);
edtNumber.setText(((ContactModel) arrContacts.get(position)).number);
btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name="", number="";
if(!edtName.getText().toString().equals("")) {
name = edtName.getText().toString();
} else {
Toast.makeText(context, "Please Enter Contact Name!", Toast.LENGTH_SHORT).show();
}
if(!edtNumber.getText().toString().equals("")) {
number = edtNumber.getText().toString();
} else {
Toast.makeText(context, "Please Enter Contact Number!", Toast.LENGTH_SHORT).show();
}
arrContacts.set(position, new ContactModel(name, number));
notifyItemChanged(position);
}
});
dialog.show();
}
});
holder.IlRow.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle("Delete Contact").
setMessage("Are your sure you want to delete?").setIcon(R.drawable.ic_baseline_delete_24)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
arrContacts.remove(position);
notifyItemRemoved(position);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
return true;
}
});
}
@Override
public int getItemCount(){return arrContacts.size(); }
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView txtName, txtNumber;
ImageView imgContact;
LinearLayout IlRow;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtName = itemView.findViewById(R.id.txtName);
txtNumber = itemView.findViewById(R.id.txtNumber);
imgContact = itemView.findViewById(R.id.imgContact);
IlRow = itemView.findViewById(R.id.LLRow);
}
}
public void onItemClicked(ContactModel contactModel){
Toast.makeText(context,contactModel.counter,Toast.LENGTH_SHORT).show();
}
}