Load data of a Fragment only if selected - android

In my Androidapp I use Swipe View as my navigation now.
If I start my App, this one loads all of my different fragments and not only this one whioch is selected. If I switch between the Fragments the view doesn't get reloaded.
How can I do this that only getting load the view which is selected and if I switch to a fragment this is getting reloaded again.
Thanks
MainActivity:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
assert actionBar != null;
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new EventCalendarFragment();
default:
return new VideoListFragment();
}
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return "Event";
} else {
return "Videos";
}
}
}
VideoListFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_videos_table, container, false);
//load customBanner
imageView = (ImageView) rootView.findViewById(R.id.video_imageView);
imageView.setVisibility(View.GONE);
new AsyncTaskParseJson().execute();
inputSearch = (EditText) rootView.findViewById(R.id.video_inputSearch);
new UpdateData().execute();
return rootView;
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser){
if (isVisibleToUser == true){
new AsyncTaskParseJson().execute();
new UpdateData().execute();
}
}
UpdateData:
class UpdateData extends AsyncTask<Void, Void, JSONArray> {
int error = 0;
InputStream is = null;
String result = "";
JSONArray jArray = null;
ProgressDialog pd;
#Override
protected void onPostExecute(JSONArray result) {
super.onPostExecute(result);
pd.dismiss();
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(getActivity(), "",
"Loading...", true);
}
#Override
protected JSONArray doInBackground(Void... arg0) {
error = 0;
url = "************";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
error = 1;
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
error = 1;
}
deptList.clear();
try {
JSONArray array = new JSONArray(result);
for (int i = 0; i < array.length(); i++) {
JSONObject j = array.getJSONObject(i);
EventCalendarStrings d = new EventCalendarStrings();
d.name = j.optString("name", "");
deptList.add(d);
}
} catch (JSONException e) {
Log.e("log_tag", "No connection " + e.toString());
error = 1;
}
return jArray;
}
}
Logcat:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException

If I understand your question correctly, you should override setUserVisibleHint(boolean isVisibleToUser) method in your fragments and use it as indicator that your fragment is currently visible (if isVisibleToUser is true). Then you should move there your logic (I suppose it's in onCreateView now) that should be invoked only if a fragment is currently on the screen.

Viewpager reads the loaded one...its clever
override this method.
public int getItemPosition (Object object)
{
return POSITION_NONE;
}
POSITION_NONE: data changed, remove the fragment.
POSITION_UNCHANGED: data unchanged.

I might be late for the party but here's my solution and it works as expected. This solution also prevents your users from re-sending requests again and again (if you're doing network operations).
In all of your child fragments create a boolean variable:
private boolean loadFragmentExecuted = false;
in the child fragments create a generic method called loadFragment and move all of the logic you added in onCreateView to that method:
public void loadFragment()
{
if(!loadFragmentExecuted)
{
//Add your logic to manipulate the UI or load data etc...
loadFragmentExecuted = true;
}
}
in your pageview logic create the fragments dynamically like:
//add the fragment
String fragmentName = "com.something." + fragmentId;
//check if the class exists
try
{
Class myFragmentClass = Class.forName(fragmentName);
Fragment myFragment = (Fragment) myFragmentClass.newInstance();
mFragments.add(myFragment);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InstantiationException e)
{
e.printStackTrace();
}
then set your pager adapter and attach a tablayout with it:
//set our pager adapter that contains different fragments
mPagerAdapter = new BasePagerAdapter(mFragmentManager, mFragments);
//link the adapter to the viewpager
mViewPager.setAdapter(mPagerAdapter);
//cache fragments
int limit = (mPagerAdapter.getCount() > 0 ? mPagerAdapter.getCount() : 1);
mViewPager.setOffscreenPageLimit(limit);
//add the page listner to the viewPager and link it to the tabLayout
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
//on tab selected select current viewpager item
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
#Override
public void onTabSelected(TabLayout.Tab tab)
{
mViewPager.setCurrentItem(tab.getPosition());
//get fragment for the selected tab
Fragment f = mPagerAdapter.getItem(tab.getPosition());
//load the content of the fragment
try
{
Class c = f.getClass();
Method loadFragment = c.getMethod("loadFragment");
loadFragment.invoke(f);
}
catch (IllegalAccessException e){}
catch (InvocationTargetException e){}
catch (NoSuchMethodException e){}
}
#Override
public void onTabUnselected(TabLayout.Tab tab)
{
}
#Override
public void onTabReselected(TabLayout.Tab tab)
{
}
});

Related

RecyclerView Adapter Conflict in a TabLayout

I have a TabLayout with 4 tabs in my Activity and I have made a Fragment per each Tab.
Below is my Activity code:
public class MyActivity extends AppCompatActivity {
private static final String TAG = "RecyclerView";
private Toolbar toolbar;
private static ViewPager viewPager;
private static TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Allow activity to show indeterminate progressbar */
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.student_regulations_list);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewPager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(onTabSelectedListener(viewPager));
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Fragment1(), getString(R.string.titleA));
adapter.addFragment(new Fragment2(), getString(R.string.titleB));
adapter.addFragment(new Fragment3(), getString(R.string.titleC));
adapter.addFragment(new Fragment4(), getString(R.string.titleD));
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(3);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
private TabLayout.OnTabSelectedListener onTabSelectedListener(final ViewPager viewPager) {
return new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
}
//View Pager fragments setting adapter class
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();//fragment arraylist
private final List<String> mFragmentTitleList = new ArrayList<>();//title arraylist
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
//adding fragments and title method
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
I have a RecyclerView in each fragment, and I fetch data from a Web Service using AsyncTask to show in the RecyclerView. Each item in RecyclerView has an ID and I want to When I switch between tabs and click on an item in RecyclerView, a Toast shows it's ID.
Below is a Fragment code:
public class Fragment1 extends Fragment implements ClickListener {
private static final String TAG = "RecyclerView";
private List<FeedItem> feedItemList;
private RecyclerView mRecyclerView;
private FeedsRecyclerAdapter mAdapter;
final String url = "a valid url";
private View rootView;
private ProgressBar progressBar;
public StuRegGeneralListFragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_student_regulations_list, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
progressBar = (ProgressBar) rootView.findViewById(R.id.progress_bar);
/* Initialize recyclerview */
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
/*Downloading data from below url*/
new AsyncHttpTask().execute(url);
}
#Override
public void itemClicked(View view, int position) {
FeedItem item = feedItemList.get(position);
Intent intent = new Intent(getActivity(), JsonRequestFeedActivity.class);
intent.putExtra("url", Const.URL_JSON_OBJECT_REGS + item.getID());
intent.putExtra("pid", item.getID());
//startActivity(intent);
Toast.makeText(getActivity(), "Item ID: " + item.getID(), Toast.LENGTH_SHORT).show();
}
public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {
#Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
feedItemList = new ArrayList<FeedItem>();
}
#Override
protected Integer doInBackground(String... params) {
InputStream inputStream = null;
Integer result = 0;
HttpURLConnection urlConnection = null;
try {
/* forming th java.net.URL object */
URL url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
/* for Get request */
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
/* 200 represents HTTP OK */
if (statusCode == 200) {
BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
response.append(line);
}
parseResult(response.toString());
result = 1; // Successful
} else {
result = 0; //"Failed to fetch data!";
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
return result; //"Failed to fetch data!";
}
#Override
protected void onPostExecute(Integer result) {
progressBar.setVisibility(View.GONE);
/* Download complete. Lets update UI */
if (result == 1) {
updateRecyclerView();
} else {
Log.e(TAG, "Failed to fetch data!");
}
}
}
private void parseResult(String result) {
try {
JSONObject response = new JSONObject(result);
JSONArray posts = response.optJSONArray("posts");
/*Initialize array if null*/
if (null == feedItemList) {
feedItemList = new ArrayList<FeedItem>();
}
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.optJSONObject(i);
FeedItem item = new FeedItem();
item.setID(post.optString("id"));
item.setDate(post.optString("date"));
item.setTitle(post.optString("title"));
item.setThumbnail(post.optString("thumbnail"));
feedItemList.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public void updateRecyclerView() {
mAdapter = new FeedsRecyclerAdapter(getActivity(), feedItemList);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setClickListener(this);
}
}
Assuming each fragment has 5 items in RecyclerView (from ID 1 to 5 in fragment1, from ID 6 to 10 in fragment2, ...) I expect when I enter to my Activity and click on an item in fragment1 (for example first item), ID=1 be shown in Toast, but ID=16 is shown!
In fact because of viewPager.setOffscreenPageLimit(3); in my Activity, fragment1 and fagment2 and fragment3 and fragment4 are created and at the end, mAdapter contains item IDs 16-20, although I am in fragment1.
I expect after creating specified fragments by viewPager.setOffscreenPageLimit(int); each fragment has it's own adapter, but it's not happened.
Any help is appreciated.
I found out my mistake!
My Activity and Fragments code are correct, my mistake was in ClickListener of RecyclerView. It's implementation was not suitable in my case.
I had used a static clickListener in it, that was the point!

GridView TabView not working in when i am json parsing

It's working fine in single GridView Json Object,when i am calling to tabView my tabs will displaying but my Gridview not set into my Tabs.
Please any one help me
Main Activity
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
MyAdapter
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new Kids();
case 1:
// Games fragment activity
return new Kids();
case 2:
// Movies fragment activity
return new Kids();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
this activity not displaying in my Tabview,it will working fine in my single gridview activity,when i am calling in tabview it will not diisplaed my gridview
kids.java
public class Kids extends Fragment {
ListAdapter adapter;
private ArrayList<Pojo> gridData;
GridView grd;
private ProgressBar mProgressBar;
private String Sam_URL = "http://example.in/rest/jsoMain/document?name=Rekapalli";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
System.out.println("Servce Called");
gridData=new ArrayList<Pojo>();
grd =(GridView)rootView.findViewById(R.id.gridview);
Async as=new Async(getActivity(),grd);
as.execute(Sam_URL);
mProgressBar = (ProgressBar)rootView.findViewById(R.id.progressBar);
// grd.setBackgroundColor(Color.CYAN);
grd.setVerticalSpacing(7);
grd.setHorizontalSpacing(7);
return super.onCreateView(inflater, container, savedInstanceState);
}
class Async extends AsyncTask<String, Void, Integer>{
Context context;
GridView gridView;
public Async(Context context,GridView gridView) {
// TODO Auto-generated constructor stub
this.context=context;
this.gridView=gridView;
}
#Override
protected Integer doInBackground(String... params) {
// TODO Auto-generated method stub
Integer result = 0;
try {
// Create Apache HttpClient
//HttpClient httpclient = new DefaultHttpClient();
URL url = new URL(Sam_URL);
URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(
urlConnection.getInputStream());
// int statusCode =
// httpResponse.getStatusLine().getStatusCode();
// 200 represents HTTP OK
if (true) {
String response = streamToString(in);
parseResult(response);
result = 1; // Successful
} else {
result = 0; // "Failed
}
} catch (Exception e) {
}
return result;
}
String streamToString(InputStream stream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(stream));
String line;
String result = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
// Close stream
if (null != stream) {
stream.close();
}
return result;
}
#Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (result == 1) {
gridView.setAdapter(new MyAdapter(context,gridData));
gridView.setVisibility(View.VISIBLE);
}mProgressBar.setVisibility(View.GONE);
}
private void parseResult(String result) {
try {
Log.d("MainActivity", "JSON Result : " + result);
JSONArray response = new JSONArray(result);
for (int i = 0; i < response.length(); i++)
{
JSONObject obj = response.getJSONObject(i);
String Doc_name = obj.getString("documentName");
Log.d("documentName",Doc_name);
String Doc_file = obj.getString("documentFile");
String Doc_content = obj.getString("documentContent");
String Doc_offer=obj.getString("offer");
String Doc_address=obj.getString("address");
//Log.d("documentName","JSON Result : " + result);
Pojo gd = new Pojo();
gd.setDocumentName(Doc_name);
gd.setDocumentFile(Doc_file);
gd.setOffer(Doc_offer);
gd.setDocumentContent(Doc_content);
gd.setAddress(Doc_address);
gridData.add(gd);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
Change from:
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new Kids();
case 1:
// Games fragment activity
return new Kids();
case 2:
// Movies fragment activity
return new Kids();
}
return null;
}
To:
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new Kids();
case 1:
// Games fragment activity
return new Games();
case 2:
// Movies fragment activity
return new Movies();
}
return null;
}
Hope that Helps!!!

How to Refresh Fragment from Action Bar Menu

I have an Action Bar with Fragment as follow. I would like to refresh current fragment using Refresh button Action Bar Menu. I saw a lot of example using getFragmentByTag() but my fragment is created dynamically. May I know how to get the current fragment and refresh the content.
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
RssFragmentPagerAdapter mRssFragmentPagerAdapter;
ViewPager mViewPager;
List<RssCategory> categoryList;
// Database Helper
private DatabaseHelper db;
private ActionBar actionBar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
db = DatabaseHelper.getInstance(getApplicationContext());
int categoryCount = db.getCategoriesCount();
// Create the adapter that will return a fragment for each of the three primary sections
// of the app.
mRssFragmentPagerAdapter = new RssFragmentPagerAdapter(getSupportFragmentManager(), categoryCount);
// Set up the action bar.
actionBar = getActionBar();
// Specify that the Home/Up button should not be enabled, since there is no hierarchical
// parent.
actionBar.setHomeButtonEnabled(false);
// Specify that we will be displaying tabs in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
// Set up the ViewPager, attaching the adapter and setting up a listener for when the
// user swipes between sections.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mRssFragmentPagerAdapter);
mViewPager.setOffscreenPageLimit(categoryCount - 1);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// When swiping between different app sections, select the corresponding tab.
// We can also use ActionBar.Tab#select() to do this if we have a reference to the
// Tab.
actionBar.setSelectedNavigationItem(position);
}
});
initialiseActionBar();
}catch(Exception e){
Log.e(getClass().getName(), e.getMessage());
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
case R.id.action_refresh:
//TO REFRESH CURRENT Fragment
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void initialiseActionBar() {
if(categoryList == null)
categoryList = db.getAllCategories();
// For each of the sections in the app, add a tab to the action bar.
for (RssCategory category : categoryList) {
// Create a tab with text corresponding to the page title defined by the adapter.
// Also specify this Activity object, which implements the TabListener interface, as the
// listener for when this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(category.getName())
.setTabListener(this));
}
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
* sections of the app.
*/
public static class RssFragmentPagerAdapter extends FragmentPagerAdapter {
private int pageCount;
public RssFragmentPagerAdapter(FragmentManager fm, int pageCount) {
super(fm);
this.pageCount = pageCount;
}
#Override
public Fragment getItem(int i) {
switch (i) {
default:
// The other sections of the app are dummy placeholders.
Fragment fragment = new RssFragment();
Bundle args = new Bundle();
args.putInt(RssFragment.ARG_CATEGORY_ID, i + 1);
fragment.setArguments(args);
return fragment;
}
}
#Override
public int getCount() {
return pageCount;
}
/*#Override
public CharSequence getPageTitle(int position) {
return "Section " + (position + 1);
}*/
}
/**
* A dummy fragment representing a section of the app, but that simply displays dummy text.
*/
public static class RssFragment extends Fragment {
public static final String ARG_CATEGORY_ID = "category_id";
View rootView;
private List<RssItem> resultList;
List<RssWebSite> websiteList;
ArrayList<String> urlList;
ProgressBar progressBar;
#SuppressWarnings("unchecked")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try{
rootView = inflater.inflate(R.layout.fragment_rss_items_list, container, false);
resultList = new ArrayList<RssItem>();
progressBar = (ProgressBar)rootView.findViewById(R.id.progressBar);
Bundle args = getArguments();
if(args != null){
DatabaseHelper db = DatabaseHelper.getInstance(rootView.getContext());
websiteList = db.getAllRssWebSiteByCategory(args.getInt(ARG_CATEGORY_ID));
urlList = new ArrayList<String>();
if(websiteList != null && websiteList.size() > 0){
for (RssWebSite website : websiteList) {
urlList.add(website.getRssUrl());
}
if(urlList.size() > 0) {
GetRSSDataTask task = new GetRSSDataTask();
task.execute(urlList);
}
}
}
}catch(Exception e){
Log.e(getClass().getName(), e.getMessage());
}
return rootView;
}
/**
* This class downloads and parses RSS Channel feed.
*
* #author clippertech
*
*/
private class GetRSSDataTask extends AsyncTask<ArrayList<String>, Void, List<RssItem> > {
#Override
protected List<RssItem> doInBackground(ArrayList<String>... urls) {
try {
for(String url : urls[0]) {
// Create RSS reader
RssReader rssReader = new RssReader(url);
Log.d(getClass().getName(), url);
// Parse RSS, get items
resultList.addAll(rssReader.getItems());
}
return resultList;
}catch (Exception e) {
Log.e(getClass().getName(), e.getMessage());
}
return null;
}
#Override
protected void onPostExecute(List<RssItem> result) {
try{
// Get a ListView from the RSS Channel view
ListView itcItems = (ListView) rootView.findViewById(R.id.rssChannelListView);
View emptyView = null;
if(result == null){
itcItems.setEmptyView(emptyView);
Log.d(getClass().getName(), "Empty View");
}
else {
//resultList.addAll(result);
Collections.sort(result, new Comparator<RssItem>() {
#Override
public int compare(RssItem lhs, RssItem rhs) {
SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
try {
Date date1 = formatter.parse(rhs.getPublishedDate());
Date date2 = formatter.parse(lhs.getPublishedDate());
return date1.compareTo(date2);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
});
// Create a list adapter
ListAdapter adapter = new ListAdapter(rootView.getContext(), resultList);
itcItems.setAdapter(adapter);
adapter.notifyDataSetChanged();
// Set list view item click listener
itcItems.setOnItemClickListener(new ListListener(resultList, getActivity()));
}
//dialog.dismiss();
progressBar.setVisibility(View.GONE);
}catch(Exception e){
Log.e(getClass().getName(), e.getMessage());
}
}
}
}
}
You would need to keep track of the current fragment index by using the PageChangeListener in your viewpager.
You can use the fragment index to retrieve the fragment from your adapter and call whatever methods you need on it.

AsyncTask not updating the TextView inside First fragment in Viewpager

I've started programming for Android (from iOS), and I working with the boiler plate code that ADT generates for me when I choose Activity+Scrollable tabs option in the new Project wizard. In the code below I've marked the same in comments.
The scenario is I enter text in an editText, press the button and fetch some data from a webservice. I wish to put this data in the appropriate fragment (there are 3 fragments) inside a view pager. I am getting the data and I am able to set it on Fragment 2 and Fragment 3 when I swipe, but the data that is relevant to Fragment 1 is not getting set once the AsycTask is complete. However, after swiping to fragment 3, when I come back to fragment 1, the data is there.
Here is my code:
Activity
public class XXXX extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
//My additions to the boiler plate source generated by ADT
public ArrayList<String> p1T,p2T,p3T,p4T;
private EditText editText;
private Button button;
private boolean flag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.konjugation);
mSectionsPagerAdapter = new SectionsPagerAdapter (getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
//My additions to the boiler plate source below
flag=false;
p1T = new ArrayList<String>(6);
p2T = new ArrayList<String>(6);
p3T = new ArrayList<String>(6);
p4T = new ArrayList<String>(6);
editText = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = editText.getText().toString();
if(!url.isEmpty()) {
url="http://.../?q="+editText.getText().toString();
PAsyncTask task = new PAsyncTask();
task.execute(url);
}
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int pos) {
if(flag==true) {
if(pos==0) DummySectionFragment.setText(p4T,p1T);
if(pos==1) DummySectionFragment.setText(p4T,p2T);
if(pos==2) DummySectionFragment.setText(p4T,p3T);
}
else {
Log.d("ts","data not arrived");
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.konjugation, menu);
return true;
}
PagerAdapter
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new DummySectionFragment();
return fragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_section1);
case 1:
return getString(R.string.title_section2);
case 2:
return getString(R.string.title_section3);
}
return null;
}
#Override
public void notifyDataSetChanged() {
int pos = mViewPager.getCurrentItem();
if(pos==0) DummySectionFragment.setText(p4T,p1T);
else if(pos==1) DummySectionFragment.setText(p4T,p2T);
else if(pos==2) DummySectionFragment.setText(p4T,p3T);
}
}
AsyncTask
private class PAsyncTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params)
{
String response = "";
for (String url : params) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
protected void onPostExecute(String result)
{
try {
p1T.clear();
p2T.clear();
p3T.clear();
//do something very important with the result here
flag=true;
//hoping that somehow the viewpager adapter will refresh the view
mSectionsPagerAdapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Viewpager's fragment
public static class DummySectionFragment extends Fragment {
private static TextView dummyTextView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_konjugation_dummy, container, false);
dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
return rootView;
}
public static void setText(ArrayList<String> s, ArrayList<String> t) {
try {
DummySectionFragment.dummyTextView.setText("");
for(int i=0;i<s.size();i++) {
DummySectionFragment.dummyTextView.append(s.get(i)+" "+t.get(i)+"\n\n");
}
} catch(NullPointerException e) {
e.printStackTrace();
}
}
}
}
I feel kinda lost, after reading some threads here. Please help :-(
you should update it inside of runOnUiThread() method, which takes a runnable object as argument, so you can pass your implementation in it.

Save the fragment state in tabs switch

it is one week I try to solve this problem whitout success. Please help me.
I use the tabs navigation with viewpager. This is the class where I put the tabs and the FragmentPagerAdapter class:
public class Detail extends SherlockFragmentActivity {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
mViewPager = (ViewPager)findViewById(R.id.pager);
// Add the tabs
mTabsAdapter = new TabsAdapter(this, bar, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText(R.string.filmtab),
FragmentFilm.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.cinematab),
FragmentCinema.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.dintornitab),
FragmentPdi.class, null);
if (savedInstanceState != null) {
bar.setSelectedNavigationItem(savedInstanceState.getInt("tab"));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
}
public static class TabsAdapter extends FragmentPagerAdapter
implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
private final Context mContext;
private final ActionBar mBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public TabsAdapter(Detail activity, ActionBar bar, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mBar = bar;
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<? extends Fragment> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mBar.addTab(tab);
notifyDataSetChanged();
}
#Override
public int getCount() {
return mTabs.size();
}
#Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
mBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i=0; i<mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
}
The 3 fragment classes are all the same I copy here just one;
In the fragment class I use async task for download the data I need to put in the view, I do this in the onActivityCreated method:
public class FragmentFilm extends SherlockFragment
{
private Detail act;
private DetailedRec detail_film;
private View view;
private String a;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
setRetainInstance(true);
view = inflater.inflate(R.layout.tab_film_info, container, false);
return view;
}
/*
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
act = (Detail) getActivity();
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new DownloadFilmDetailAsyncTask().execute();
}
private class DownloadFilmDetailAsyncTask extends AsyncTask<Void, DetailedRec, Void>
{
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ProgressBar prBar = (ProgressBar)getView().findViewById(R.id.progressbar_film);
prBar.setVisibility(View.GONE);
ScrollView lay = (ScrollView)getView().findViewById(R.id.tab_filmsummary);
lay.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(Void... params)
{
try
{
String locale = getResources().getConfiguration().locale.getDisplayName();
JSONObject objSend = new JSONObject();
objSend.put("idFilm", act.getIdFilm());
objSend.put("cinemaId",act.getIdCinema());
int ind = locale.indexOf("(");
String locale_send = locale.substring(0, ind-1);
objSend.put("locale", locale_send);
ArrayList<String> otherCin = new ArrayList<String>(Arrays.asList(act.getOtherCinemas()));
JSONArray othCin = new JSONArray(otherCin);
objSend.put("otherCinemas", othCin );
JSONObject jsonObject = sendAndGetJSONObject(JSON_SERVER+"JsonServer?op=getFilmbyId",objSend);
DetailedRec detail_rec = new DetailedRec();
//FILM
detail_rec.setFilmId(jsonObject.getString("filmId"));
detail_rec.setName(jsonObject.getString("name"));
detail_rec.setImageUrl(jsonObject.getString("imageUrl").replace("640", "80"));
detail_rec.setActors(jsonObject.getString("actors"));
detail_rec.setGenre(jsonObject.getString("genre"));
detail_rec.setDirector(jsonObject.getString("director"));
detail_rec.setPlot(jsonObject.getString("plot"));
detail_rec.setYear(jsonObject.getString("year"));
detail_rec.setDuration(jsonObject.getString("duration"));
detail_rec.setTrailer(jsonObject.getString("trailer"));
detail_rec.setRating(jsonObject.getString("rating"));
detail_film = detail_rec;
publishProgress(detail_rec);
}
catch (IOException ignored)
{
}
catch (JSONException ignored)
{
}
return null;
}
#Override
protected void onProgressUpdate(DetailedRec... values)
{
for (final DetailedRec detail_rec : values)
{
updateViews(detail_rec);
}
}
private JSONObject getJSONObject(String url) throws IOException, MalformedURLException, JSONException
{
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
InputStream in = conn.getInputStream();
try
{
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(new DoneHandlerInputStream(in),"WINDOWS_1252"));
for (String line = r.readLine(); line != null; line = r.readLine())
{
sb.append(line);
}
return new JSONObject(sb.toString());
}
finally
{
in.close();
}
}
private JSONObject sendAndGetJSONObject(String url,JSONObject request) throws IOException, MalformedURLException, JSONException
{
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
InputStream in=null;
try{
HttpPost post = new HttpPost(url);
StringEntity se = new StringEntity(request.toString());
se.setContentType((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
in = entity.getContent();
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(new DoneHandlerInputStream(in),"WINDOWS_1252"));
for (String line = r.readLine(); line != null; line = r.readLine())
{
sb.append(line);
}
return new JSONObject(sb.toString());
}catch(Exception e){
}
finally
{
in.close();
}
return null;
}
}
private void updateViews(final DetailedRec detail_rec){
//FILM
TextView filmName = (TextView) getView().findViewById(R.id.movieTitle);
filmName.setText(detail_rec.getName().trim());
TextView actors = (TextView) getView().findViewById(R.id.movieActor);
actors.setText(detail_rec.getActors().trim());
TextView genre = (TextView) getView().findViewById(R.id.movieGenre);
genre.setText(detail_rec.getGenre().trim());
TextView director = (TextView) getView().findViewById(R.id.movieDirector);
director.setText(detail_rec.getDirector().trim());
TextView plot = (TextView) getView().findViewById(R.id.moviePlot);
plot.setText(detail_rec.getPlot().trim());
TextView year = (TextView) getView().findViewById(R.id.movieYear);
year.setText(detail_rec.getYear().trim());
TextView duration = (TextView) getView().findViewById(R.id.movieDuration);
duration.setText(detail_rec.getDuration().trim());
ImageView image = (ImageView) getView().findViewById(R.id.moviePoster);
new DownloadImagesTask(detail_rec.getImageUrl().trim().replace("80", "100")).execute(image);
//image.setImageBitmap(downloadBitmap(detail_rec.getImageUrl().trim().replace("80", "100")));
//Rating
if(detail_rec.getRating().compareTo("N/A")!=0){
RatingBar rateBar = (RatingBar)getView().findViewById(R.id.MovieRatingBar);
rateBar.setRating(Float.parseFloat(detail_rec.getRating()));
}
//Trailer
Button trailer = (Button)getView().findViewById(R.id.trailer);
if(detail_rec.getTrailer().compareTo("")!=0){
trailer.setVisibility(View.VISIBLE);
trailer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int index = detail_rec.getTrailer().indexOf("v=");
String videoId="";
if(index!=-1){
videoId = detail_rec.getTrailer().substring(index+2); //"Fee5vbFLYM4";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+videoId));
intent.putExtra("VIDEO_ID", videoId);
startActivity(intent);
}
}
});
}
}
}
Ok, in my application I want the fragment is load one time. I explain so good:
When I start the Detail class the fragment classes are istantiate and the AsyncTask in all the 3 fragment start, now when the user switch from one tab to other I want the fragment in tab unselected don't lost the data and the view because now when I switch from one tab to another and then return the first tab this is recreate and the onActivityCreated method is called again!
Please help me, I search in all place but I dont find solution!!
P.S. I use the SherlockActionBar, I don't know id it is relevant. Sorry for my bad English
Thank you
Just after instantiating the ViewPager, add this
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(3);
That should do it!

Categories

Resources