How to swap items from two recyclerview items that are in two fragments in one activity? - android

I have two fragments (a and b) inside an Activity according to the picture below.
I am able to delete from first one but how to add that item to favorites fragment RecyclerView?
Deleting actress name and adding to favorites
My Viewholder code for RecyclerView Fragment one class:
addToFavoriteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mArrayList.remove(getAdapterPosition());
notifyDataSetChanged();
}
});
How to add this deleted item inside adapter of Favorites Recyclerview?

What I would do is maintain a list of actresses (either locally or on the server) with each one containing an isFavorite boolean attribute.
Then, while you have one global list, each recyclerview is only showing a subset:
On the left, you show all actresses where isFavorite is set to false.
On the right, you show all actresses where isFavorite is set to true.
How you update it could be done a few different ways, but here is what I recommend at a high level:
Have an onClick listener for each one that bubbles up to the activity, so the activity is aware any time an actresses's favorite state changes. Every time the state changes for an actress, tell your adapters in each fragment to update.
If you don't want to refresh the entire list every time, you could integrate a remove and add method like Mauker's Answer.

You can create a method inside your adapter that removes an item from the RecyclerView, and returns the given item.
Then, you can use this item reference to add it to the second RecyclerView.
Pseudocode example
public myItem removeAndGetItem(int position) {
myItem item = mArrayList.get(position);
mArrayList.remove(position);
notifyDataSetChanged();
return item;
}
Then you could call something like (also, pseudocode):
myItem item = adapter1.removeAndGetItem(position);
adapter2.add(item);
Adjust the examples to your code, and it should do the trick.
Edit
I misread the part about the RecyclerViews being on different Fragments.
So, you can still do what I said on the example above, you'll just have to pass that item to the second Fragment, using Fragment callbacks, or Broadcasting the item, or even through an EventBus.
Instead of using notifyDataSetChanged() which can be very costly, try to use notifyItemRemoved(int position) instead. As you can see on the docs:
If you are writing an adapter it will always be more efficient to use the more specific change events if you can. Rely on notifyDataSetChanged() as a last resort.

Related

Is there way to acces every RecyclerViewHolder?

I have a recyclerview with data that changes during the lifecycle of an app, lets say that in my recycler view holders i have an edittext field that comes with populated data but I want user to have access to change that data, is there a way that when the button is pressed i can somehow access all of those textfields at once?
Hmm you could keep 2 parallel lists in your recycle view adapter? one that has the main data, the other one is updated whenever the user changes something on an edit text.
Once the user presses the update button, you can just copy the data from list2(with modified data) to list1 (original) and update the recycler view.
for example
inside onBindviewholder()
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// find your edittext to set a listener for text change
// get the current item from list2 (list2 has same item originally as the original list)
keep updated the list2 data here.
}
on the main activity once the user presses the button you can do
adapter.mainList = new ArrayList<>(adapter.list2)
adapter.notifyDataSetChanged()

RecyclerView Data passing to same Activity

I have a cart program, in activity is simple, there was a recyclerview and a button. In recycler view I can edit its stock.
Now, I want to get that product_name and product_stock from that I've ever clicked and make changes.
Now, when every stock has been clicked, I want click button on activity, so I want that data I've ever clicked on recyclerview stored to array, so that button can do their action / function. Can you guys lead me, how to import data from recyclerview to its activity itself.
It was Android program to do shopping cart, so I click the data in recycler view it stored to activity array.
I don't have any idea how to store that from recyclerview to activity's array.
My expected result, should be, when I click buy button on activity, every changed stock on recyclerview is shown.
You can simply implement an interface in your adapter like this for getting the values from clicked position in recycleview.
public interface FetchRecyclerViewItems{
void getItems(String product_name,String product_stock);
}
And simply create a setter method for this interface in adapter like this,
private FetchRecyclerViewItems fetchRecyclerViewItems;
public void setFetchRecyclerViewItems(FetchRecyclerViewItems fetchRecyclerViewItems){
this.fetchRecyclerViewItems = fetchRecyclerViewItems;
}
Then set the values like this on your click like this in OnBindViewHolder,
your_view.setOnClickListener(new View.SetOnClickListener)....{
....
fetchRecyclerViewItems.getItems(product_nameFromPosition,product_stockFromPosition);
}
And implement this interface in your activity and you will get the product_name,product_stock values there.
Make sure to initialize the FetchRecyclerViewItems in your activity.

Android Fragments default select first item in ListView

In Android Studio I have selected a new project based on master / detail flow. The project works as it should be. Now I want to extend it. As of now the content of the items in the detail fragment are only shown when I click an item on the list. I want that the top item is selected automaticaly when the app starts. I thought I put in ItemListFragment just a method call
#Override
public void onStart()
{
mCallbacks.onItemSelected(DummyContent.ITEMS.get(0).id);
}
that a click is simulated in the lifecycle once all objects are initialized. That fails. What is the best way of doing this?
You can select the first element by the following code
yourListView.setSelection(0);
yourListView.getSelectedView().setSelected(true);
Hope this helps. Cheers mate! :)
You can use SharedPreferences. You can save first item of listview as preference when create listview, then you can use it later.
Have you try it in onResume()? I always put it in onResume()
UPDATE #1
You can try my way to implement the selection. Normally i don't use the select of list view.
1.In your data model, create a boolean field called "selection".
2.When you apply data to your adapter, set the first data "selection" to true.
3.In your adapter getView callback, try to handles your data "selection" such as:
if (!dummyData.selection){
do something when not select....
}else{
do something when selected...
}
UPDATE #2
put this code in ItemListFragment
#Override
public void onResume() {
super.onResume();
mCallbacks.onItemSelected(DummyContent.ITEMS.get(0).id);
}

Individual IDs for listeners using a custom ListView

I'm rather new to the Android SDK, and I recently started developping a small application. The application centers around a menu, which is implemented using a custom ListView. I used this guide as a reference in order to build my model classes and my adapter. The author also published the code he used on GitHub, here. My result is rather close from the original one, the screen below is from the author's blog post:
I successfully set up my menu, and now comes the time when I need to connect every item of the list to the listener so that I can handle clicks. In order to keep it simple, I had my activity implement OnClickListener:
public class MenuActivity extends ListActivity implements View.OnClickListener
{
#Override
public void onClick(View v) {
// ...
}
}
I was also able to connect my menu items to this method through my menu items' XML files:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClick"
android:id="#+id/menu_item_layout">
Now, whenver I click on one of my list items, I enter the onClick method. However, once I'm there, I can't find a way to distinguish an item from other. As you can see, I applied my click event to their layouts, so that it is triggered when I click an item in it (my texts).
However, since all my items share the same XML layout, they also seem to share the same ID: #id/menu_item_layout. Because of that, I can't do the usual...
public void onClick(View v) {
switch(v.getId()){
case R.id.myId: /* ... */ break;
case R.id.myId2: /* ... */ break;
// ...
}
}
Given this "custom list" implementation, is there a way I could distinguish a click on Menu item1 from a click on Menu item 2?
An OnClickListener will not function as you expect with a ListView. However, the ListActivity class provides the onListItemClick() method, that is implemented as follows:
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
}
The position parameter will hold the index in the underlying data list corresponding to the item clicked.
When you create a View for a list item in your ListAdapter you can set tag on that view using View#setTag. Put there something which can distinguish one list item from another i.e. position in adapter. Then use View.getTag to get this unique identifier in onClick listener.
What i think is, there are two ways to do this:
First:
Add clicklistener to the view inside the getview method, that way you would also be having the position(remember to initialize a final int to the position inside getView and use that int inside your clickListener)
Second:(not guaranteed if it will work)
Add a tag to every textView inside getView and store the position in that textView. ex:
textView.setTag(new Integer(int position));
and then fetch that position in the onClick method of yours

need to reload mainActivity controller on click of button located in customized listview

To make my question more understandable let me start with an image of my view.
I have an xml file named Menu, that has customized list view in it. I have created another xmlview named MenuCell as below.
Now tapping on add button I'm adding Item to the cart. which is working perfectly fine except not updating value of a cart (top right corner) on click event. But If I navigate to different view and come back to this view at this point I'm getting number of items added in the cart reflected properly. So How Can I reload my controllerview when I tap in my arradepter view's ImageButton.
this is my adapter code
holder.imageButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addItem(position);
notifyDataSetChanged();
}
});
void addItem(int position) {
count++;
}
Where count is item added count.
If anyone can tell How am I able to reflect this count of my arrayadpter class to my other controller class that holds actual list view.
Any Help will be appreciated
Thanks in advance.
You need to use callback feature to get notified to your activity , hence add button is part of list component so you can update your list view.
Here are few links for understanding of call back using interface
http://cleancodedevelopment-qualityseal.blogspot.in/2012/10/understanding-callbacks-with-java.html
http://stackoverflow.com/questions/11577695/what-is-a-call-back-interface-in-java
After :
notifyDataSetChanged();
just add:
YourList.invalidateViews();
YourList.scrollBy(0, 0);
Send the reference of your controller to your list adapter class. In the controller I guess you have a method that computes some data and after that call update the view that holds cart data summary. Just make sure that update is on UI thread.
Your List is not refreshing at the instant because you have to refresh data of your adapter and then call notifyDataSetChanged();
Suppose You have the data in an array which is visible in textview.Then In your function addItem add the data into ur array and then call notifyDataSetChanged();
This will immediately tell the list view that watever data it is containing is changed so time to refresh.
If you can paste more of ur adapter code i can be helpful

Categories

Resources