I have a ViewPager
and it is using a FragmentAdapter
in order to display several fragments of the same kind. Although these Fragments are basically instantiated from the same class, they are using a ListView
to display different information. (Obviously the ListView
is being poulated by an ArrayAdapter
.)
A background service is also running and is constantly receiving data from the Internet. I want to be able to update a specific Fragment in the ViewPager
when my background service has notified me of a specific event.
How can I do that?
A code snippet would be hugely appreciated!
(By the way, I have saw this similar question but I have no idea how to use their suggestion!)
To make it all more simple:
My activity with the ViewPager:
[Fragment 0] [Fragment 1] [Fragment 2]
The background service tells me (via a broadcast) to update the ListView
in Fragment 1.
EDIT: Here are sample codes:
public class ChatWindowPager extends FragmentActivity
{
private ViewPager mViewPager = null;
private ChatFragmentAdapter mAdapter = null;
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_window_pager);
this.mViewPager = (ViewPager) findViewById(R.id.chatPager);
this.mAdapter = new ChatFragmentAdapter(getSupportFragmentManager());
this.mViewPager.setAdapter(this.mAdapter);
.
.
.
}
class ChatFragmentAdapter extends FragmentPagerAdapter implements ViewProvider
{
public ChatFragmentAdapter(final FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(final int arg0)
{
String friendId = ..... // Some initializations
ChatWindowFragment f = ChatWindowFragment.newInstance(friendId);
return f;
}
@Override
public int getCount()
{
...
}
@Override
public View getView(final int position)
{
View v = getLayoutInflater().inflate(R.layout.tab_holder, null);
.
.
.
return v;
}
}
}
Now the fragments is defined like this:
public class ChatWindowFragment extends Fragment
{
public String friendId;
private ListView lv;
public static ChatWindowFragment newInstance(final String friendId)
{
ChatWindowFragment chatWindowFragment = new ChatWindowFragment();
Bundle bundle = new Bundle();
bundle.putString("friendId", friendId);
chatWindowFragment.setArguments(bundle);
return chatWindowFragment;
}
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.friendId = getArguments().getString("friendId");
}
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.chat_window, container, false);
this.friendId = getArguments().getString("friendId");
.
.
.
return v;
}
//The rest of the class
}
As I am using a FragmentPagerAdapter
I don't see how I can set the tag of each fragment!
(Obviously, I am not using transactions to add the Fragments!)
EDIT 2: I would like to know whether what I'm doing, is the correct way to handle what I want to do... Any other solution is also welcome!
Have you tried
FragmentManager.findFragmentByTag()
EDIT: I read carefully! The
instanceOf
will cast aFragment
into your Fragment class. It was you, who suggestedFragment1
as a name for simpicity. Also, you didn't provide any source to help us. It is true, that you cannot set aFragment
's tag, but why do you think you are able to get its tag? Usually aFragment
is added throughFragmentManagers
likeEDIT2: it's very easy though. according to your code you could just call
You should consider reading the docs (i.e about FragmenPagerAdapter) and post your source code so we don't have to guess what you need.
Just look at this answer https://stackoverflow.com/a/16388650
He has used
yourAdapter.notifyDataSetChanged()
which is a predefined method. Check the link to see how its doneEdit: When your AsyncTask is done, you should do something like this onPostExecute method:
And in your ResultFragment you need to have refreshData method, which is something like this:
I had the same issue but fixed it with a localBroadcastReceiver like this:
Create a receiver in your activity and register it:
Remember to remove LocalBroadCast
Send your broadcast from anywhere you want Adapters, services, etc.
Hope it helps others.
I don't know enough of what you are doing, but it sounds like you need to use an Observer pattern, Callbacks, or Listeners. Can't your fragment just do somthing like:
and then you can be "notified of a specific event."
Try this,
Register a broadcast receiver in all your fragments... like this
create a class which extends a broadcast receiver in all the classes, for eg:
and register this receiver in you fragment's onCreate ...
for eg.
getActivity().registerReceiver(new FragmentReceiver1(), new IntentFilter("fragmentupdater"));
Now assign a unique id to each of you fragment like 1 for Fragment1, 2 for Fragment2 and likewise
now whenever you want to pass any data and update any of the fragment just send a broadcast with the data in intent and "fragmentupdater" as the intent-filter...
For eg:
Now each of your fragment will receive the data but you can verify if the data if for the same fragment by the unique id we passed in it in the
onReceive
function..., the intent which you get, is the intent we passed above