On Clicking one button, it Triggers another button in recycle view - java

I'm currently working on a attandance management Android app. In my recyclerview there are list of student detail being populated dynamically from database. When i click the first button in the list, it also change the button colour of the 11th button of the list Like wise to 2nd button click changes the colour of 12th View button
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;`enter code here`
List<StudentDetails> MainImageUploadInfoList;
public RecyclerViewAdapter(Context context, List<StudentDetails> TempList) {
this.MainImageUploadInfoList = TempList;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
StudentDetails studentDetails = MainImageUploadInfoList.get(position);
holder.StudentNameTextView.setText(studentDetails.getName());
holder.StudentNumberTextView.setText(studentDetails.getPhoneNumber());
holder.st.setText(studentDetails.getRollno());
holder.cardView.setCardBackgroundColor(getcolor(position));
}
private int getcolor(int position) {
return Color.parseColor("#" + Integer.toHexString(ContextCompat.getColor(context, R.color.normal)));
}
#Override
public int getItemCount() {
return MainImageUploadInfoList.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView StudentNameTextView;
public TextView StudentNumberTextView;
public TextView st;
DatabaseReference databaseReference;
String se,wh,su,d;
Button b;
CardView cardView;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
StudentNameTextView = (TextView) itemView.findViewById(R.id.ShowStudentNameTextView);
StudentNumberTextView = (TextView) itemView.findViewById(R.id.ShowStudentNumberTextView);
st = (TextView) itemView.findViewById(R.id.studentrollno);
cardView=itemView.findViewById(R.id.cardview1);
se=Teacher.getInstance().getSe();
wh=Teacher.getInstance().getWh();
su=Teacher.getInstance().getSu();
d=Teacher.getInstance().getD();
b=itemView.findViewById(R.id.button3);
itemView.findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view ) {
int position=getAdapterPosition();
String message = String.valueOf(position) ;
databaseReference= FirebaseDatabase.getInstance().getReference("attandance").child(wh).child(se).child(su).child(d).child(message);
databaseReference.setValue("p");
itemView.findViewById(R.id.button3).setBackgroundColor(Color.GREEN);
itemView.findViewById(R.id.button4).setEnabled(false);
itemView.findViewById(R.id.button3).setEnabled(false);
Toast.makeText( context,"position is "+String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
itemView.findViewById(R.id.button4).setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(final View view ) {
int position=getAdapterPosition();
String message = String.valueOf(position) ;
databaseReference= FirebaseDatabase.getInstance().getReference("attandance").child(wh).child(se).child(su).child(d).child(message);
databaseReference.setValue("A");
itemView.findViewById(R.id.button4).setBackgroundColor(Color.RED);
itemView.findViewById(R.id.button3).setEnabled(false);
itemView.findViewById(R.id.button4).setEnabled(false);
}
});
}
#Override
public void onClick(View view) {
// int position=this.getAdapterPosition();
// Intent intent=new Intent(context,mark.class);
// String message = String.valueOf(position);
// intent.putExtra("attan",message);
// context.startActivity(intent);
// Intent intent = new Intent(context,mark.class);
//String message = String.valueOf(position) ;
//intent.putExtra("attan",message);
//context.startActivity(intent);
// Toast.makeText( context,"position is "+String.valueOf(position), Toast.LENGTH_SHORT).show();
}
}
}
}

Related

How to block particular Position in Adapter class ANDROID JAVA

I have an adapter class Where i Want to show a BLOCK USER dialog on particular position.
for example if i want to block position 1 of recyclerView .
So can any one help me porgrammatically?
Given below is the Code Of adapter Please check
I want to block user profile based on Recycler View position
if someone could help me in it please;
public class TableProfileAdapter extends RecyclerView.Adapter<TableProfileAdapter.MyViewHolder> {
Context context;
Activity getActivity;
List<TableProfile> profileUser;
OnRecItemClick onRecItemClick;
private boolean isScaleAnimationDone = false;
private boolean isTimerTextViewActionUpCalled = false;
public TableProfileAdapter(List<TableProfile> profileUser, Context context) {
this.profileUser = profileUser;
this.context = context;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.profile_item, parent, false);
return new MyViewHolder(view, onRecItemClick);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
TableProfile data = profileUser.get(position);
holder.tvUserName.setText(data.getProfileName());
Bitmap image = BitmapManager.byteToBitmap(data.getProfileImage());
holder.profilePic.setImageBitmap(image);
if (context instanceof AccessProfileManagementActivity) {
context.startActivity(new Intent(context,HomeActivity.class));
} else if (context instanceof ProfileManagement) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, ProfileDetailsActivity.class);
TableProfile model = profileUser.get(holder.getAdapterPosition());
intent.putExtra("DATA", model);
context.startActivity(intent);
notifyDataSetChanged();
((Activity) context).finish();
}
});
}
}
#Override
public int getItemCount() {
return profileUser.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CircleImageView profilePic;
TextView tvUserName, birthday;
OnRecItemClick onRecItemClick;
RelativeLayout relativeLayout;
public MyViewHolder(#NonNull View itemView, OnRecItemClick onRecItemClick) {
super(itemView);
profilePic = itemView.findViewById(R.id.profileImage);
tvUserName = itemView.findViewById(R.id.userName);
relativeLayout = itemView.findViewById(R.id.relTop);
this.onRecItemClick = onRecItemClick;
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
onRecItemClick.onItemClick(getAdapterPosition());
}
}
public interface OnRecItemClick {
void onItemClick(int position);
}
}

How Can I get item id [duplicate]

This question already has answers here:
how to get ID of the items in recyclerview
(2 answers)
Closed 2 years ago.
I have recyclerview on my project . I want get item id . How Can I get item id ? . I used Item touchhelper and I need particular item id . How can I get particular item id .
Activty.java
public class todoactivity extends AppCompatActivity {
TextView title;
Button back;
ImageButton gorevo;
RecyclerView recyclerView;
List<String>Listsx = new ArrayList<>();
TodoActivityAdpter adapterx;
DatabaseHelper4 myDBxxx;
TextView textView;
CheckBox checkBox;
long id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todoactivity);
recyclerView=findViewById(R.id.recyclerviewxx);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
adapterx=new TodoActivityAdpter(Listsx);
recyclerView.setAdapter(adapterx);
title=findViewById(R.id.titlex);
textView=findViewById(R.id.text_viewx);
gorevo = findViewById(R.id.gorevo);
myDBxxx = new DatabaseHelper4(this);
Cursor datax = myDBxxx.getListContents();
if(datax.getCount() == 0){
}else{
while(datax.moveToNext()){
Listsx.add(datax.getString(1));
ListAdapter listAdapterx = new ArrayAdapter<>(this,R.layout.todoactivity_item,R.id.textitem,Listsx);
adapterx.notifyItemInserted(Listsx.size()-1);
}
}
gorevo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(todoactivity.this);
bottomSheetDialog.setContentView(R.layout.bottomsheetlayout3);
bottomSheetDialog.show();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
EditText editText = bottomSheetDialog.findViewById(R.id.editx);
Button ekle = bottomSheetDialog.findViewById(R.id.ekle);
ekle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = editText.getText().toString();
Listsx.add(text);
AddDataxxx(text);
adapterx.notifyItemInserted(Listsx.size()-1);
bottomSheetDialog.hide();
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
}
});
}
});
back=findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(todoactivity.this, pomodoroscreen.class);
startActivity(i);
overridePendingTransition(0,0);
}
});
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.LEFT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int positionx = viewHolder.getAdapterPosition();
Listsx.remove(positionx);
adapterx.notifyItemRemoved(positionx);
myDBxxx.deleteItem(// I want get id in there);
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
}
public void AddDataxxx(String newEntry) {
boolean insertDatax = myDBxxx.addDataxxx(newEntry);
}
}
MyAdapter.java
public class TodoActivityAdpter extends RecyclerView.Adapter<TodoActivityAdpter.Holder> {
List<String>Listsx;
public TodoActivityAdpter(List<String>itemxxx){
this.Listsx = itemxxx;
}
#NonNull
#Override
public TodoActivityAdpter.Holder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.todoactivity_item,parent,false);
Holder holder = new Holder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull TodoActivityAdpter.Holder holder, int position) {
holder.textView.setText(Listsx.get(position));
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.checkBox.isChecked()) {
holder.textView.setTextColor(view.getResources().getColor(R.color.grey));
} else {
holder.textView.setTextColor(view.getResources().getColor(R.color.Color_black));
}
}
});
}
#Override
public int getItemCount() {
return Listsx.size();
}
public class Holder extends RecyclerView.ViewHolder {
CheckBox checkBox;
TextView textView;
List<String>Listsx;
RecyclerView recyclerView;
Context mContext;
public Holder(View view) {
super(view);
textView=view.findViewById(R.id.text_viewx);
checkBox=view.findViewById(R.id.checkbox);
recyclerView=view.findViewById(R.id.recyclerviewxx);
}
}
}
I want get item id in Activty . How Can I get item id . Can you be fast . My actvity is activity.java . My adapter is MyAdapter.java
int positionx = viewHolder.getAdapterPosition();
int id = recyclerView.getChildAt(position).getId();
Listsx.remove(positionx);
adapterx.notifyItemRemoved(positionx);
myDBxxx.deleteItem(id);
maybe

How to add a recyclerview item dynamically when click a button in another activity?

I have a recyclerview in ShoppingCartActivity and a button in ProductDetailActivity. I want to send some data to ShoppingCartActivity when a button clicked in ProductDetailActivity and display them in recyclerview item that creates dynamically in ShoppingCartActivity.
I added addData method to recyclerview Adapter but it doesn't work and app crashes.
shop_btn = findViewById(R.id.add_to_shopping_cart);
shop_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent addToCart = new Intent(ProductDetailActivity.this, ShoppingCartActivity.class);
startActivity(addToCart);
}
});
//HERE IS MY RECYCLERVIEW ADAPTER
public class RecyclerViewAdapter_ShoppingCart extends RecyclerView.Adapter<RecyclerViewAdapter_ShoppingCart.MyViewHolder> {
private Context context;
List<CartItems> cartItemsList;
public RecyclerViewAdapter_ShoppingCart(Context context, List<CartItems> cartItemsList) {
this.context = context;
this.cartItemsList = cartItemsList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.card_cart, parent, false);
return new MyViewHolder(view);
}
#NonNull
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, final int position) {
holder.cart_title.setText(cartItemsList.get(position).getTitle());
holder.cart_price.setText(cartItemsList.get(position).getPrice());
Picasso.get().load(cartItemsList.get(position).getImageUrl()).into(holder.cart_img);
}
#Override
public int getItemCount() {
return cartItemsList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView cart_title, cart_price;
ImageView cart_img;
public MyViewHolder(View itemView) {
super(itemView);
cart_title = itemView.findViewById(R.id.cart_title);
cart_price = itemView.findViewById(R.id.cart_price);
//cart_delete = itemView.findViewById(R.id.cart_delete);
cart_img = itemView.findViewById(R.id.cart_image);
}
}
public void addItem(List<CartItems> cartItems) {
CartItems newValue = new CartItems();
newValue.setTitle("123");
newValue.setPrice("123");
newValue.setImageUrl("");
cartItems.add(newValue);
notifyDataSetChanged();
}
}
My goal is adding recyclerview items dynamically to ShoppingCartActivity when a button clicked in ProductDetailActivity but app crashes.

viewHolder.getAdapterPosition() in adapter return -1 position and it caused IndexOutOfBoundException

i try to implement best practice to handle onclick in adapter, but i got problem IndexOutOfBoundException when getAdapterPostition inside onCreateViewHolder, whats wrong with my code?
i already try to check but not solve yet.
public class VideoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private DataFrontVideo datas;
private Context context;
public VideoAdapter(DataFrontVideo datas, Context context) {
this.datas = datas;
this.context = context;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_video,null);
final ViewHolder viewHolder = new ViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DataVideo video = datas.getContent().get(viewHolder.getAdapterPosition()); // ERROR HERE
Intent intent = new Intent(context,DetailActivity.class);
intent.putExtra("datas",video);
context.startActivity(intent);
}
});
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, int position) {
DataVideo dataVideo = datas.getContent().get(position);
ViewHolder vh = (ViewHolder)viewHolder;
vh.tvTitle.setText(dataVideo.getJudul());
vh.tvArtis.setText(dataVideo.getAlias());
vh.tvPrice.setText((dataVideo.getPrice().equals("0")?"Gratis":dataVideo.getPrice()));
Glide.with(vh.img.getContext()).load(dataVideo.getThumb_pic()).into(vh.img);
}
#Override
public int getItemCount() {
return (datas == null)? 0 : datas.getContent().size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView img;
private TextView tvTitle,tvArtis,tvPrice;
public ViewHolder(#NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.imgVideo);
tvArtis = itemView.findViewById(R.id.tvartis);
tvTitle = itemView.findViewById(R.id.tvTitle);
tvPrice = itemView.findViewById(R.id.tvPrice);
}
}
}
it should not return -1
Inside your ViewHolder you need to apply the setOnClickListener and get the item position on click by using the getAdapterPosition() like below
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView img;
private TextView tvTitle,tvArtis,tvPrice;
public ViewHolder(#NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.imgVideo);
tvArtis = itemView.findViewById(R.id.tvartis);
tvTitle = itemView.findViewById(R.id.tvTitle);
tvPrice = itemView.findViewById(R.id.tvPrice);
YOUR_VIEW.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext,"Position==>> "+getAdapterPosition(),Toast.LENGTH_LONG).show();
DataVideo video = datas.getContent().get(viewHolder.getAdapterPosition()); // ERROR HERE
Intent intent = new Intent(context,DetailActivity.class);
intent.putExtra("datas",video);
context.startActivity(intent);
}
});
}
}
Actually, the problem just because you are registering click listener in your onCreateViewHolder which is wrong you need to register on Click listener in your onBindViewHolder like bellow
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, int position) {
DataVideo dataVideo = datas.getContent().get(position);
ViewHolder vh = (ViewHolder)viewHolder;
vh.tvTitle.setText(dataVideo.getJudul());
vh.tvArtis.setText(dataVideo.getAlias());
vh.tvPrice.setText((dataVideo.getPrice().equals("0")?"Gratis":dataVideo.getPrice()));
Glide.with(vh.img.getContext()).load(dataVideo.getThumb_pic()).into(vh.img);
vh.parentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context,DetailActivity.class);
intent.putExtra("datas",dataVideo);
context.startActivity(intent);
}
});
}
Move your click in onBindViewHolder .Here it will work for title click ,if you want all click make viewholder complete layout and implement click listener
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, int position) {
DataVideo dataVideo = datas.getContent().get(position);
ViewHolder vh = (ViewHolder)viewHolder;
vh.tvTitle.setText(dataVideo.getJudul());
vh.tvArtis.setText(dataVideo.getAlias());
vh.tvPrice.setText((dataVideo.getPrice().equals("0")?"Gratis":dataVideo.getPrice()));
Glide.with(vh.img.getContext()).load(dataVideo.getThumb_pic()).into(vh.img);
vh.tvTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context,DetailActivity.class);
intent.putExtra("datas",dataVideo );
context.startActivity(intent);
}
});
}

update recycleview row from item onclick

I have a recycleview with 3 texts, is there a way to update specific item onclick that position, (i have quantity of each item, and i want to rise the quantity of that item onclick that position, 0,1,2,3...)
here is my receycleview:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
String [] Item_names,Price_items,Item_quantity;
public RecyclerAdapter(String [] Item_names,String [] Price_items,String [] Item_quantity)
{
this.Item_names=Item_names;
this.Price_items=Price_items;
this.Item_quantity=Item_quantity;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,parent,false);
RecyclerViewHolder recyclerViewHolder=new RecyclerViewHolder(view);
return recyclerViewHolder;
}
#Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
holder.ItemNameRecycler.setText(Item_names[position]);
holder.ItemPriceRecycler.setText(Price_items[position]);
holder.ItemQuantityRecycler.setText(Item_quantity[position]);
}
#Override
public int getItemCount() {
return Item_names.length;
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView ItemPriceRecycler,ItemNameRecycler,ItemQuantityRecycler;
public RecyclerViewHolder(View view)
{
super(view);
ItemNameRecycler=(TextView)view.findViewById(R.id.RowItemName);
ItemPriceRecycler=(TextView)view.findViewById(R.id.RowItemPrice);
ItemQuantityRecycler=(TextView)view.findViewById(R.id.RowItemQuantity);
ItemQuantityRecycler.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
}
You can use getAdapterPosition
From Documentation:
Returns the Adapter position of the item represented by this
ViewHolder.
in your code:
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView ItemPriceRecycler,ItemNameRecycler,ItemQuantityRecycler;
public RecyclerViewHolder(View view)
{
super(view);
ItemNameRecycler=(TextView)view.findViewById(R.id.RowItemName);
ItemPriceRecycler=(TextView)view.findViewById(R.id.RowItemPrice);
ItemQuantityRecycler=(TextView)view.findViewById(R.id.RowItemQuantity);
ItemQuantityRecycler.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// get position
int pos = getAdapterPosition(); //get position here
}
});
}
EDIT
for updating the recylerview Item Textview in onClick()
public static class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView ItemPriceRecycler, ItemNameRecycler, ItemQuantityRecycler;
public RecyclerViewHolder(final View itemView) {
super(itemView);
ItemNameRecycler = (TextView) itemView.findViewById(R.id.RowItemName);
ItemPriceRecycler = (TextView) itemView.findViewById(R.id.RowItemPrice);
ItemQuantityRecycler = (TextView) itemView.findViewById(R.id.RowItemQuantity);
ItemQuantityRecycler.setOnClickListener(this);
}
// onClick Listener for view
#Override
public void onClick(View v) {
if (v.getId() == ItemQuantityRecycler.getId()) {
int pos = getAdapterPosition();
int value = pos + 1;
Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
ItemQuantityRecycler.setText(String.valueOf(value));
} else {
}
}
}

Categories