How to notify Intent that data in changed? - android

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)

Related

Empty list after orientation change

First off, I've already looked at this question. In my scenario, I have a fragment that extends support.v4.app.ListFragment. The fragment calls a custom adapter. I need to handle orientation changes on a device that will switch to a 2 pane layout. I believe I'm doing most things right as the view itself changes correctly and I am able to retrieve the data from savedInstanceState. However, my list is always empty. I tried the recommended answer in the linked question (calling setListShown(true)), but I get an exception, "Can't be used with a custom content view". The relevant code is below:
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
LayoutInflater inflater = LayoutInflater.from(getActivity());
ViewGroup group = ((ViewGroup) getView());
group.removeAllViews();
inflater.inflate(R.layout.activity_message_list, group);
if(!messages.isEmpty()){
mAdapter = new MessageListAdapter(getActivity().getApplicationContext(), messages);
}
setListAdapter(mAdapter);
}
The adapter's getView method is never invoked after the configuration change. What else do I need to do to re-hydrate the view? Let me know if you need to see any other code.
Since you are creating a new view then you have to redo all View's initialization that you do in the original piece of code (in onViewCreated or somewhere else). So, in order to initialize the ListView - you should do something like this:
View rootView = inflater.inflate(R.layout.activity_message_list, group);
if(!messages.isEmpty()){
mAdapter = new MessageListAdapter(getActivity().getApplicationContext(), messages);
}
ListView listView = (ListView) root.findViewById(R.id.<your_list_view_id>;
listView.setAdapter(mAdapter);
Just keep in mind that you also have to do all other initialization (creating references to Views and creating onClickListeners or whatever else you're doing)
There is a way to avoid app reload after device orientation changed, you just have to tell system that you'll handle screen orientation changed events on your own.
Have you tried to add parameter android:configChanges="orientation" inside activity tag in Manifest.xml?
With a ton of direction from Chaosit, I was able to achieve what I wanted. My app has a nav drawer activity and I swap the fragment (which is a master-detail type fragment) based on the selection. I added android:configChanges="orientation" so the fragment wouldn't revert back to the default selection I made in the onCreate method of the activity. So, I removed that line and made the adjustments to store the selection in the savedInstanceState bundle. Everything is working as I would have expected now.

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

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

Android: Dynamically Change Activity Title to Text of Selected List Item

I'm new to Android development.
I created a simple master-detail app that starts with a simple, vertical scrolling list of topics.
When the user selects a topic, a details screen appears, replacing the first screen, with a list of details that pertain to the selected topic.
I want the title for the details screen to show the topic the user has selected on the first page, but haven't been able to solve the problem after working for almost a week.
All I need to know is, Can this be done? Not looking for someone to solve this for me, but maybe a hint or a link to a tutorial that shows how this can be done.
Note: I'd post a drawing of what I want to do, but I'm new here and don't have 10 reputation yet.
Thanks,
SonCoder
Not exactly sure what you want but either way..
-You have a listview. Each view (the data) in the listview should be a represented by a model. (aka a separate class containing specific information that you want to represent for each listitem.
-Write a custom list adapter (extend from base adapter).
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
In the getView method of this class you load the the String field of the model that you want in the textview.
-Make sure to use the viewholder pattern in the adapter above. I noticed the example doesnt use one. This speeds up scrolling in the list because there are much fewer calls to findViewById.
-in the list activity set up a View onClick listener. This should create an intent (for launching an activity) or a fragment transaction (for fragments). Send the instance of your entire model (will get from
parent.getAdapter().getItem(position);
in the on click method) into the detail activity.
-if you want to set a textview title just get the textview and set it from the model. It will be the same filed you inflated in the getView method of the adapter.
-if you want to set the titile in the actionbar set:
this.getActionBar().setTitle(title)
This is simple. Just send extra data in the intent that starts the activity and then in the activity's onCreate read the data and then use the setTitle(myString) method from the activity.
setTitle(String title) can be called from anywhere using the activity by the way.
So, your in your listadapter, then you set a listener on your view right? A simple onClickListener on the whole "root" view is just fine.
In the listener you say something in the ways of this:
Intent intent = new Intent(myActivity, MySubActivity.class);
intent.putExtra(key, titleName);
myActivity.startActivity(intent);
Note that the activity reference should be set in the constructor of the adapter and that the "key" String is something you get from your strings.xml. Do not duplicate these in code since if you change one and forget to change the others you might get some wierd NPEs.
Continue in your MySubActivity's onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String key = getString(R.string.my_title_key);
String title = intent.getString(key);
setTitle(title);
}
NOTE: I'm not sure of all method names are correct and such but something like this.

Android refresh listview adapter from another activity

I Googled a lot, but can't find the proper answer.
I've got two activities. Each of that activities 'has' one listview.
Each of that listviews have a custom ArrayAdapter. So :
Activity 1 ----> Listview 1 ------> custom ArrayAdapter 1
Activity 2 ----> Listview 2 ------> custom ArrayAdapter 2
When I press a button in Activity 1, then it refreshes the Listview 1 (it's adapter).
The goal is : I have to do a refresh on Listview 2 too that time. How can I achieve this ? I've read something about broadcasting, but didn't understand it well.
Thank You !
If you are in Activity1, it's useless to try to refresh Activity2's listview as it's not visible. However, what you can do is, when the user is switched to Activity2, just call adapter's notifyDataSetChanged() in it's onResume() method and your Activity2 will be refreshed.
Add listView as global variable in each activity and make it public static.
Then, just call it when you need it.
e.g. in activity1, call the following to let the list in activity2 refresh
if(SecondActivity.listView != null)
((ArrayAdapter)SecondActivity.listView.getAdapter()).notifyDataSetInvalidated();

Categories

Resources