Android Fragments default select first item in ListView - android

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);
}

Related

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.

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

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.

How to Load only current pages of Fragment

I have involved with Fragments page-loads problem. I create a 10-Fragments and identify the current Fragment by onPageChanged(). I am updating the contents of each Fragment during swipe.
but if i am doing to update one Fragment and show values. it is displaying wrong index value at wrong pages.
I noted, Fragment is loading previous and next pages also. so if i display current fragment values by changing the resources, it changes previous and next resources but not current.
example:- if i am in pageno-3, if display the update values of pageno-3 at pageno-4 , it happens during left to right swipe
but i will do right to left swipe, it display the update values of pageno-3 at pageno-2
I think you should extend the Fragment class. Inside the new class define a method
public void refresh(NewValuesObject obj){
... do something with the new values
};
Inside your onPageChanged callback call this method with the desired params.
You may want to save your fragment instances and do the updates (call refresh) for the fragment at the previous or next index.
Hope it helps.
You can set control in visiblity like this:
#Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if((visible ) )
{
//set your adapter or listview
}
}
if you set this in your 2 and 4 , when you come 3. page the other ones does not triggered.

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

How to notify Intent that data in changed?

I am using LocalActivityManager activityManager = getLocalActivityManager();, I put all my activities to this by View view = activityManager.startActivity("123", myIntent).
There are many activities in this activityManger which are identical by their ID e.g(123).
1 activity hold favorite Items name FavoriteActivty. Another activity hold items. All I want to do is to notify FavoriteActivty that data has been changed so activity has to refresh itself...
How to do this? I dont want to create that activity again.
I'm not 100% sure if I got what you need.
For my understanding you need to set an adapter to this activity...
then you can just handle a list and give the adapter a "notification"
Example:
adapter = new ArrayAdapter<Module>(this,
android.R.layout.simple_list_item_1, moduleList);
setListAdapter(adapter);
and if you change something (I add here an entry to the list):
moduleList.add(m);
adapter.notifyDataSetChanged();
OK. I finally got my answer. I used the following method:
onAttachedToWindow();
It worked for me. As the window comes to front I check look for the changes.
onResume() ;
onWindowFocusChanged(boolean hasFocus)

Categories

Resources