i have recyclerview and button on the all item of list. when i click the button in my adapter item deleted and i want refresh recycler and show new data.
#Override
public void onBindViewHolder(final LietnerViewHolder holder, final int position) {
lietner = lietners.get(position);
holder.txt_en.setText(lietner.getText_en());
holder.txt_pe.setText(lietner.getText_pe());
holder.time.setText(lietner.getTime());
holder.data.setText(HelperCalendar.g2j(lietner.getTime_create()));
holder.next_level.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
G.databaseOpenHelper.goToNextLevel(lietners.get(position).getId());
}
});
}
For re-initializing the recycler view and get callback on onBindViewHolder
simple call notifyDataSetChanged();
#Override
public void onBindViewHolder(final LietnerViewHolder holder, final int position) {
lietner = lietners.get(position);
holder.next_level.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// delete the item, or change the data.
notifyDataSetChanged();
}
});
}
i do this and it work for me!
#Override
public void onBindViewHolder(final LietnerViewHolder holder, final int position) {
lietner = lietners.get(position);
holder.txt_en.setText(lietner.getText_en());
holder.txt_pe.setText(lietner.getText_pe());
holder.time.setText(lietner.getTime());
holder.data.setText(HelperCalendar.g2j(lietner.getTime_create()));
holder.next_level.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
G.databaseOpenHelper.goToNextLevel(lietners.get(position).getId());
lietners.remove(position);
notifyDataSetChanged();
}
});
}
Related
I have a simple RecyclerView with an Interface that is linked to the activity. The Issue is the items in the recyclerView need double click in order to perform an action.
This is the AllProductsAdapter Code :
public class AllProductsAdapter extends RecyclerView.Adapter<AllProductsAdapter.ViewHolder> {
RecyclerViewClickInterfaceNew recyclerViewClickInterface;
List<ProductsModel> productsModelList;
Context context;
public AllProductsAdapter(List<ProductsModel> productsModelList, Context context, RecyclerViewClickInterfaceNew recyclerViewClickInterface) {
this.productsModelList = productsModelList;
this.context = context;
this.recyclerViewClickInterface = recyclerViewClickInterface;
}
#NonNull
#Override
public AllProductsAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.product_items, parent, false);
return new AllProductsAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull AllProductsAdapter.ViewHolder holder, int position) {
ProductsModel productsModel = productsModelList.get(position);
PicassoTrustAll.getInstance(context)
.load(productsModel.getProduct_image())
.into(holder.catImage);
}
#Override
public int getItemCount() {
return productsModelList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ImageView catImage;
TextView catName;
CardView categoryCard;
public ViewHolder(#NonNull View itemView) {
super(itemView);
catImage = itemView.findViewById(R.id.catImage);
catName = itemView.findViewById(R.id.catName);
categoryCard = itemView.findViewById(R.id.categoryCard);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (recyclerViewClickInterface != null)
recyclerViewClickInterface.onItemClick(v, getAdapterPosition());
}
});
}
}
This is my RecyclerViewClickInterfaceNew :
public interface RecyclerViewClickInterfaceNew {
void onItemClick(View view , int position);
void onLongItemClick(int position);
}
This is my MainActivity code :
#Override
public void onItemClick(View view , int position) {
ImageView img= view.findViewById(R.id.catImage);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(AllProductsList.this, "Imgclciked", Toast.LENGTH_SHORT).show();
}
});
}
Here in the MainActivity, the Toast shows after clicking 2 times on the Image. How can i fix it to a normal single click ? Please let me know your answer.
From this code
#Override
public void onItemClick(View view , int position) {
//when different item is FIRST clicked, a new instance of catImage is produced
ImageView img= view.findViewById(R.id.catImage);
//the img onClick is set here to require a second click
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(AllProductsList.this, "Imgclciked", Toast.LENGTH_SHORT).show();
}
});
}
For each new item on the Recycler View, you reset the click handling.
I'll suggest you set the onClickListener ON the ViewHolder. It's not the best practice but it'll suffice for your code
I think that's because of you are using NestedScrollView ,
you should add this line to your recyclerview
android:nestedScrollingEnabled="false"
I am implementing interface to detect when view is clicked in recycler view. I am removing the item when button is clicked. It works fine when once button is clicked. But when I click second time it removes the view present next to it.
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
optionsClickEvent.onDeleteClicked(position, imagePath);
showToast(String.valueOf(position));
}
});
}
Above code is executing in onBind. Next I am removing:
#Override
public void onDeleteClicked(int position, ArrayList<String> filePath) {
filePath.remove(position);
imageAdapter.notifyItemRemoved(position);
}
This works one time perfect. But items are not updated. And when I click delete button second time the item is removed at position+1.
User holder.getAdapterPosition() instead of layout position in onClick
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
optionsClickEvent.onDeleteClicked(holder.getAdapterPosition(), imagePath);
showToast(String.valueOf(position));
}
});
}
I want to access an ArrayList from a nonactivity class to a fragment.
#Override
public void onBindViewHolder(final DataObjectHolder holder, final int position)
{
holder.setIsRecyclable(false);
holder.itemid.setText(mDataset.get(position).getItem_id());
holder.itemname.setText(mDataset.get(position).getItem_name());
holder.qty.setText(mDataset.get(position).getQty());
int rt=Integer.parseInt(mDataset.get(position).getRate().substring(0,mDataset.get(position).getRate().length() - 3));
holder.rate.setText(formatter.format(rt)+".00");
ItemDetails listItem = filterList.get(position);
holder.itemname.setText(listItem.getItem_name());
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
holder.qty.setText(String.valueOf(++qt));
mDataset.get(position).setQty(String.valueOf(qt));
finalDataset.add(new ItemDetails(ItemFragment.getInstance().cat_id,String.valueOf(holder.itemid.getText()),String.valueOf(holder.itemname.getText()),String.valueOf(holder.rate.getText()),String.valueOf(holder.qty.getText())));
}
});
holder.minus.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(qt==0)
{
holder.qty.setText(String.valueOf(0));
}
else
{
holder.qty.setText(String.valueOf(--qt));
}
mDataset.get(position).setQty(String.valueOf(qt));
finalDataset.add(new ItemDetails(ItemFragment.getInstance().cat_id,String.valueOf(holder.itemid.getText()),String.valueOf(holder.itemname.getText()),String.valueOf(holder.rate.getText()),String.valueOf(holder.qty.getText())));
}
});
}
finalDataset is the mentioned ArrayList. I added the values to the ArrayList inside the onBindViewHolder method in recycleradapter class. And I want to access it in another fragment.
Create method to return List in adapter class,
public List<ItemDetails> getDataset() {
return finalDataset;
}
In fragment class access it using,
List<ItemDetails> finalDataset = adapter.getDataset();
When I click item of RecyclerView and I want to be able to open another activity. How can I do this?
This code is my recycler adapter onBindViewHolder code.
#Override
public void onBindViewHolder(ViewHolder holder, int position)
{
holder.setItemclickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
if(isLongClick){
Toast.makeText(context,"Long Click"+ dataset.get(position),Toast.LENGTH_SHORT).show();
}
else{
Intent intent=new Intent(context,MainActivity.class);
startActivity(intent);
Toast.makeText(context,"short Click"+ dataset.get(position),Toast.LENGTH_SHORT).show();
}
}`enter code here`
});
Groups tiklanilan=dataset.get(position);
holder.groupnametext.setText(tiklanilan.getGroupname());
//holder.groupimage.setImageResource(tiklanilan.getImageURL());
}
You should not use setItemClickListener but instead in your onBindViewHolder add a clickListener on your root view and call the method startActivity in that clickListener.
#Override
public void onBindViewHolder(AccountListViewHolder holder, int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.getContext().startActivity(yourIntent);
}
});
}
How can I select all checkboxes in recyclerView?
I try to do it like this:
in Adapter:
public void selectAll(){
Log.e("onClickSelectAll","yes");
isSelectedAll=true;
notifyDataSetChanged();
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
PersonDTO item = dataSet.get(position);
holder.tvName.setText(item.getName());
if (!isSelectedAll) holder.cbSelect.setSelected(false);
else holder.cbSelect.setSelected(true);}
In layout Activity I have a button with onClickListener:
private void onClickSelectAll(View view) {
getSelectPersonsAdapter().selectAll();
}
You made a simple mistake here
if (!isSelectedAll) holder.select_item.setChecked(false);
else holder.select_item.setChecked(true);
change setSelected into setChecked thats it
for both Select All and Unselect All
activty
checkall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(checkall.isChecked()){
categoryAdapter.selectAll();
}
else {
categoryAdapter.unselectall();
}
}
});
in adapter
public void selectAll(){
isSelectedAll=true;
notifyDataSetChanged();
}
public void unselectall(){
isSelectedAll=false;
notifyDataSetChanged();
}
#Override
public void onBindViewHolder(#NonNull final MyviewHolder holder, int position) {
final CategoryListItems list = categoryListItems.get(position);
holder.text.setText(list.getName());
if (!isSelectedAll){
holder.checkbox.setChecked(false);
}
else holder.checkbox.setChecked(true);
Select all Checkbox in recycler view with data binding
In adapter assign globally boolean parameter
public boolean isAllChecked = false;
Create select all method
public void setAllChecked(boolean isAllChecked) {
this.isAllChecked = isAllChecked;
notifyDataSetChanged();
}
In onBind view to write here
if (!isAllChecked) holder.ulbCheckboxBinding.chkBoxUlbName.setChecked(false);
else holder.ulbCheckboxBinding.chkBoxUlbName.setChecked(true);
Go to your activity and used onclick listener
binding.checkSelectAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
adapter.setAllChecked(binding.checkSelectAll.isChecked());
adapter.notifyDataSetChanged();
}
});
thank you
#android