How to access adapter delete button into activity? - android

I have a RecyclerView and adapter. Now in that adapter, I'm inflating one row. In that row, there are one delete button and one progressbar. So what I'm doing is when user clicks on delete button, I make invisible that delete button, and make visible small progress bar in place of delete button from Adapter class. And also I'm sending position via listener to that attached activity, from that I'm calling AsyncTask.
Now the problem is:
When I got to know via AsyncTask that item is deleted, I again want to make visible delete button and to make invisible progressbar. But this time - from Activity (not from adapter), because I want to do something in activity when I get to know that item is deleted. So I can't implement AsyncTask in adapter.
code:
Adapter
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null) {
delete.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
listener.onClicked(getAdapterPosition(), eventList.get(getAdapterPosition()).getEventId());
}
}
});
Activity (in activity I want to visible/invisible adapter row button and p.bar:
#Override
public void onDeleteDataReceived(Boolean status, int position) {
stopShimmerLayout();
if (status) {
try {
eventsList.remove(position);
mAdapter.notifyItemRemoved(position);
showToast(context, "Deleted", Toast.LENGTH_SHORT);
} catch (Exception e) {
e.printStackTrace();
}
} else {
showToast(context, "Failed", Toast.LENGTH_SHORT);
}
}
See the video for better understanding: https://drive.google.com/open?id=13ZAtnyfGbi2X4JjUTmJsIDy-gt5y51Gr

To fix your problem you can take the below approach.
1) Inside your Eventpojo/model class, declare a boolean isSelectedwhich will be initially false. Now whenever user clicks the row, do `
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
eventList.get(position).isSelected=true;
delete.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
}
So by doing this, we are keeping in memory which object is selected and which is not, but once you recycle your views bindViewHolder will be invoked aagain and UI elements setter will be called again so put a check inside onBindViewHolder()
if(eventList.get(position).isSelected){
//show progress bar
}else{
// show delete icon
}
To remove the item, just do the following changes in your adapter-
public void removeItem(int position){
eventList.remove(position)
notifyItemRemoved(position)
}

Related

Delete edittext value in the recyclerview

Currently: i have a recycler view where it has delete button and an edit text for each item.
Each item in the recycler view contains edit text that the user can input.
The scenario is i have 4 item in the list resulting 4 edit text.
When i try to fill up edit Text in the item(n) and tried to delete
the item(n) and add again. The edit Text value in the edit text remains.
How can i clear the value of the edit text?
Here is my current code when i try to delete a specific item in the list:
mHeaderText.remove(pos);
ArrayList<Integer> temp = new ArrayList<Integer>();
for (int i = 1 ; mHeaderText.size() >= i ; i++) {
temp.add(i);
Log.d("ADDDING","ADDDING"+i);
}
mHeaderText.clear();
mHeaderText.addAll(temp);
notifyDataSetChanged();
Try this code ..
In adapter class in onBindView method when you perform click event then set edittext value as null.
edittext.setText("");
and if your edittext box in activity layout then make interface into adapter class and handling click event like this way...
OnItemClick onItemClick;
public void setOnItemClick(OnItemClick onItemClick) {
this.onItemClick = onItemClick;
}
public interface OnItemClick {
void getPosition(String data); //pass any data to shared it.
}
after that..
#Override
public void onBindViewHolder(ItemViewHolder holder, final int position) {
// below code handle click event on recycler view item.
final String data = mStringList.get(position);
holder.textView.setText(data);
holder.firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onItemClick.getPosition(data); // shared data.
}
});
}
after that in activity adapter bind into recycler view it means adapter not null then called below line..
adpater.setOnItemClick(new RecyclerViewAdpater.OnItemClick() {
#Override
public void getPosition(String data) {
// hear perform any operation.
editTextbox.setText("");
adpater.notifyDataSetChanged();
}
});
Use
edittext.setText("");
to clear text where edittext is reference to EditText. Since you haven;t posted complete code so i can guide to this only. If you provide more code, then I can help you more.
One thing is sure. You will need to use
edittext.setText("") to clear the value.
But you need to be careful. You need to keep this as the first line in your delete row function, i.e. before you are removing that edittext item from your list.
Here is my code: I have placed the "remove row" function inside onBindViewHolder() method of Recyclerview Adapter.
holder.delImgView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// You need to place it in the start
holder.editText.setText("");
// Remove the item on remove/button click
myArrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, myArrayList.size());
Toast.makeText(context, "Removed : " + position +" item", Toast.LENGTH_SHORT).show();
}
});

Android recycle view with swipe effect on click listener not working

I am implementing an app that display list of data on a RecycleView. After swipe a recycleview item it display a specific LinearLayout view. In that view I have implemented an onClickListner. But it's not calling on click.
You must create an OnItemClickListener() interface.
Then, put this in your onBindViewHolder method:
holder.bind(items.get(position), listener);
Next time, put the bind method inside of your Adapter:
itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
listener.onItemClick(item);
}
});
And finally, in your MainActivity inside of your adapter declaration open the listener like this:
recycler.setAdapter(new ContentAdapter(items, new ContentAdapter.OnItemClickListener() {
#Override public void onItemClick(ContentItem item) {
Toast.makeText(getContext(), "Item Clicked", Toast.LENGTH_LONG).show();
}
}));
If you want to read more check out this documentation

RecyclerView changes multiple rows although only one row was targeted

I got a RecyclerView and want to change the appearance of any clicked row. For that I have a callbackFunction in my Activity which I pass to the Adapter, which then is called inside the Adapter, as soon as I click on any row in the RecyclerView.
The clicked row is then changed, but it happens, that not only the clicked rows are changed but also other rows, that weren't clicked and were never clicked before. I checked the ArrayList that contains the data, but everything is fine there. Only the clicked elements contain the trigger to change the appearance of the row.
What is causing the other rows to change, although they have not been clicked?
Interface inside activity for callback
public interface onHeaderClickListener{
void onHeaderClicked(int index);
}
Inside RecyclerView Adapter
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ViewHolderHeader){
((ViewHolderHeader)holder).dateHeaderTextView.setText( Integer.toString(((objClass_offerDateHeader) arrayList.get(position)).getDate()));
if(((objClass_offerDateHeader) arrayList.get(position)).isSelected()){
((ViewHolderHeader)holder).dateHeaderTextView.setBackgroundColor(Color.parseColor("#b642f4"));
}
((ViewHolderHeader)holder).dateHeaderTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onHeaderClickListener.onHeaderClicked(position);
}
});
}
}
Adapter initialisation inside activity
customAdapterRecyclerViewAddOffersTo = new customAdapterRecyclerViewAddOffers(offerArrayList,"dragTo", new onHeaderClickListener() {
#Override
public void onHeaderClicked(int index) {
if (offerArrayList.get(index) instanceof objClass_offerDateHeader){
if(((objClass_offerDateHeader) offerArrayList.get(index)).isSelected()){
((objClass_offerDateHeader) offerArrayList.get(index)).setSelected(false);
}
else {
((objClass_offerDateHeader) offerArrayList.get(index)).setSelected(true);
}
customAdapterRecyclerViewAddOffersTo.notifyDataSetChanged();
}
}
});
In your onBindViewHolder method you have to set the background of the unselected cell, keep in mind the the cells are reused and you only set the background of selected cells so when it is reused the background is not returned to the normal color
So in code you will have to add an else condition
if(((objClass_offerDateHeader) arrayList.get(position)).isSelected()){
((ViewHolderHeader)holder).dateHeaderTextView.setBackgroundColor(Color.parseColor("#b642f4"));
} else {
((ViewHolderHeader)holder).dateHeaderTextView.setBackgroundColor(Color.parseColor("#FFFFFF")); // I assume you need it to be white you can change it to any other color
}
You need to add an else condition here:
if(((objClass_offerDateHeader) arrayList.get(position)).isSelected()){
((ViewHolderHeader)holder).dateHeaderTextView.setBackgroundColor(Color.parseColor("#b642f4"));
}
Viewholders get recycled, so you cannot be sure of the current state when onBindViewHolder is called.

How to get adapter notified when state changes?

In my main activity I have two fragments. One is song list and second is favorite. This both fragment contain ListView and custom adapter. There is a bottom sheet which contain a button . when that button will be clicked that song will be added to favorite(second fragment). I am able to add the song into fragment favorite but it do not show at a time when we add it. we have to restart the application to get it into the favorite. The button to add favorite is operated from main activity . Is there any way that i can notify my adapter that state have been changed.(songs added to favorite use database)
Is there any way i can refresh fragment when it is opened???
This is code of main activity in which favorite is button that adds song name in database when it is clicked.
favourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(x==0) {
favourite.setImageResource(R.drawable.ic_favorite);x=1;
boolean isinserted =
mydb.insertdata(collections.get(a).getsong().toString());
if (isinserted == true) {
Toast.makeText(MainActivity.this, "Added as favorite", Toast.LENGTH_LONG).show();
x=1;
}
else
{
Toast.makeText(MainActivity.this, "data not inserted", Toast.LENGTH_LONG).show();
}
}
else{favourite.setImageResource(R.drawable.ic_favorite_border);
int deletedrow = mydb.deleteData(collections.get(a).getsong());
if(deletedrow>0){Toast.makeText(MainActivity.this, "removed from favorite", Toast.LENGTH_LONG).show();
finish();
startActivity(getIntent());
x=0;
}
else {Toast.makeText(MainActivity.this, " not removed from favorite", Toast.LENGTH_LONG).show();
}
}
}
});
you should call notifyDataSetChanged() from your adapter like this:
yourAdapter.notifyDataSetChanged();
update the list with the newly added item and call
adapter.notifyDataSetChanged();
Use this into item click to add into list :
public Both[] boths;
public ArrayList<Both> both = new ArrayList<Both>();
onItemClick:
String itemValue = (String) listview.getItemAtPosition(i);
if (boths == null) {
boths = new Both[]{ \\Both is your Object Class name
new Both(itemValue)
};
}
both.addAll(Arrays.asList(boths));
adapters.notifyDataSetChanged(); \\adapters is your Adapter Name
adapters.setAdapter(both);
As I know, you have two fragments. Here is a solution as follow:
Step 1:
You can define two method to update your adapter in your two fragment!
SongFragment:
public void updateDataList(){
if(songAdapter != null){
songAdapter.notifyDataSetChanged();
}
}
FavoriteFragment:
public void updateDataList(){
if(favoriteAdapter != null){
favoriteAdapter.notifyDataSetChanged();
}
}
Step 2:
When your want to update your views, and your MainActivity must hold the SongFragment And FavoriteFragment Instance.
you can call the method updateDataList in MainActivity. eg:
songFragment.updateDataList();
favoriteFragment.updateDataList();
You also need to update your fragment if you are retrieving data from database,you need to update your cursor or list, you can check this on fragment in this method or you can make listener where you can listen your added fav event.
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
}else{
// fragment is no longer visible
}
}
I solved it if anybody wants it.
I have used broadcast when button gets clicked. Then that broadcast will be recieved in fragment. In reciever i have cleared the list and then again added the list . Now adapter is notified if state have been changed

Show / Hide buttons in RecyclerView adapter from Activity

I have some RecyclerView item with invisible button and I would like to change all button visibility from Activity. Like this:
Please help me.
Why don't you create a method inside your RecyclerAdapter which will activate the button when a certain action happens in the Activity. Let's say an activity named activateButtons like this:
public void activateButtons(boolean activate) {
this.activate = activate;
notifyDataSetChanged(); //need to call it for the child views to be re-created with buttons.
}
Now, inside your onBindViewHolder, do something like this:
if (activate) {
buttons.setVisibility(View.VISIBLE);
} else {
buttons.setVisibility(View.INVISIBLE);
}
and now, the final step, call the activateButtons method from Activity on an action:
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
adapter.activateButtons(true);
}
});

Categories

Resources