Android : Dynamic ListView in custom PagerAdapter not initializing

on Wednesday, December 10, 2014


I'm trying to get a feature running but can't seem to get past a problem. The feature build is as follows: Tabs, lists in each tab, autosearch for each list, infinite scrolling for each list, and the search field will be a single unmoving view so if you type "fox" into the search field, if you are on tab 1, tab 1 will update with the new results, and if you switch to tab 2, it will get search results for the text in the unmoving search bar.


As it stands if I start the activity and initialize the first tab (it also initializes the second tab because when a tab is initialized it also initializes the closest tabs to it) it won't display the list, even though it grabs the data and sets the adapter for the list. If I switch to tab 2, which was initialized with tab 1, it will display the list with the appropriate data. If I switch to tab 3 it will initialize fine and destroy tab 1 to save memory. Once tab 1 has been destroyed, if I got back to tab 1 the list will be displayed appropriately. If I click on the now destroyed tab 3, even though tab 3 will initialize it will not display the list.


The kicker is that the tabs basically update fine themselves, they initialize when they should and are destroyed when they should, and I can even set the tab number in a textView for each tab, so that if I switch a tab it displays the tab I am currently on, and it does that correctly every time. I am only getting the issue with the lists not displaying ever though the adapters are set, the data is retrieved from the server, and the list/adapter is notified properly.


I've been working on this feature for days so any help would be unbelievably appreciated.



package com.solo.solo;



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class SlidingTabsBasicFragment extends Fragment {

DB DB = new DB(SlidingTabsBasicFragment.this.getActivity());

static final String LOG_TAG = "SlidingTabsBasicFragment";

/**
* A custom {@link ViewPager} title strip which looks much like Tabs present
* in Android v4.0 and above, but is designed to give continuous feedback to
* the user when scrolling.
*/
private SlidingTabLayout mSlidingTabLayout;

/**
* A {@link ViewPager} which will be used in conjunction with the
* {@link SlidingTabLayout} above.
*/
private ViewPager mViewPager;

/**
* Inflates the {@link View} which will be displayed by this
* {@link Fragment}, from the app's resources.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.f_notifications, container, false);
}

// BEGIN_INCLUDE (fragment_onviewcreated)
/**
* This is called after the
* {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} has finished.
* Here we can pick out the {@link View}s we need to configure from the
* content view.
*
* We set the {@link ViewPager}'s adapter to be an instance of
* {@link SamplePagerAdapter}. The {@link SlidingTabLayout} is then given
* the {@link ViewPager} so that it can populate itself.
*
* @param view
* View created in
* {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}
*/
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// BEGIN_INCLUDE (setup_viewpager)
// Get the ViewPager and set it's PagerAdapter so that it can display
// items
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(new SamplePagerAdapter());
// END_INCLUDE (setup_viewpager)

// BEGIN_INCLUDE (setup_slidingtablayout)
// Give the SlidingTabLayout the ViewPager, this must be done AFTER the
// ViewPager has had
// it's PagerAdapter set.
mSlidingTabLayout = (SlidingTabLayout) view
.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);
// END_INCLUDE (setup_slidingtablayout)
}

// END_INCLUDE (fragment_onviewcreated)

/**
* The {@link android.support.v4.view.PagerAdapter} used to display pages in
* this sample. The individual pages are simple and just display two lines
* of text. The important section of this class is the
* {@link #getPageTitle(int)} method which controls what is displayed in the
* {@link SlidingTabLayout}.
*/
class SamplePagerAdapter extends PagerAdapter {

/**
* @return the number of pages to display
*/
@Override
public int getCount() {
return 4;
}

/**
* @return true if the value returned from
* {@link #instantiateItem(ViewGroup, int)} is the same object
* as the {@link View} added to the {@link ViewPager}.
*/
@Override
public boolean isViewFromObject(View view, Object o) {
return o == view;
}

// BEGIN_INCLUDE (pageradapter_getpagetitle)
/**
* Return the title of the item at {@code position}. This is important
* as what this method returns is what is displayed in the
* {@link SlidingTabLayout}.
* <p>
* Here we construct one using the position value, but for real
* application the title should refer to the item's contents.
*/
@Override
public CharSequence getPageTitle(int position) {
String pageTitle = null;
if (position == 0) {
pageTitle = "Connections";
} else if (position == 1) {
pageTitle = "Forwards";
} else if (position == 2) {
pageTitle = "Profited";
} else if (position == 3) {
pageTitle = "Other";
}

return pageTitle;
}

// END_INCLUDE (pageradapter_getpagetitle)

/**
* Instantiate the {@link View} which should be displayed at
* {@code position}. Here we inflate a layout from the apps resources
* and then change the text view to signify the position.
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Inflate a new layout from our resources
View view = getActivity().getLayoutInflater().inflate(
R.layout.f_n_layout, container, false);
// Add the newly created View to the ViewPager

container.addView(view);
ListView listView = null;

if (position == 0) {
listView = (ListView) view.findViewById(R.id.listView1);
} else if (position == 1) {
listView = (ListView) view.findViewById(R.id.listView2);
} else if (position == 2) {
listView = (ListView) view.findViewById(R.id.listView3);
} else if (position == 3) {
listView = (ListView) view.findViewById(R.id.listView4);
}

final Trans_adapter adapter = new Trans_adapter(getActivity());

listView.setAdapter(adapter);
DB.getList("trans_history", "list", null, 0, null, getActivity(),
adapter, null, null, null);
// Assign adapter to ListView
// Retrieve a TextView from the inflated View, and update it's text
TextView title = (TextView) view.findViewById(R.id.item_title);
title.setText(String.valueOf(position + 1));
TextView title2 = (TextView) view.findViewById(R.id.item_subtitle);
title2.setText(String.valueOf(position + 1));

Log.i("Fail", "instantiateItem() [position: " + position + "]");

// Return the View
return view;
}

/**
* Destroy the item from the {@link ViewPager}. In our case this is
* simply removing the {@link View}.
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
Log.i("Fail", "destroyItem() [position: " + position + "]");

}

}


}


0 comments:

Post a Comment