Using viewpager in my application - android

I want to use viewpager in my application.I'm tried to do this everyday in one month but i can't achieve the solution.I want to create pages with same listview concept but different datas.Here is my code:
public final class TestFragment extends ListFragment {
private static final String KEY_CONTENT = "TestFragment:Content";
ArrayList <HashMap<String, Object>> imageliste = new ArrayList<HashMap<String, Object>>();
public class MyCustomAdapter extends ArrayAdapter<HashMap<String, Object>> {
//Bitmap bm;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<HashMap<String,Object>> imageliste) {
super(context, textViewResourceId,imageliste);
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
View row = convertView;
if(row==null){
LayoutInflater inflater=LayoutInflater.from(getActivity());
row=inflater.inflate(R.layout.list, parent, false);
}
TextView label=(TextView)row.findViewById(R.id.text1);
label.setText((CharSequence) imageliste.get(position).get("Baslik"));
TextView label2=(TextView)row.findViewById(R.id.text2);
int boyut =imageliste.get(position).get("Desc").toString().length();
label2.setText((CharSequence) imageliste.get(position).get("Desc").toString().substring(0, (boyut/3)*2)+"...");
ImageView icon=(ImageView)row.findViewById(R.id.img);
icon.setImageDrawable((Drawable) imageliste.get(position).get("Resim"));
return row;
}
}
public String getURLContent(String url)
{
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
ResponseHandler<String> resHandler = new BasicResponseHandler();
String page = httpClient.execute(httpGet, resHandler);
return page;
} catch (ClientProtocolException e) {
return "";
} catch (IOException e) {
return "";
}
}
public ArrayList<HashMap<String, Object>> getImageLinks(String strng){
ArrayList<HashMap<String, Object>> myBooks2 = new ArrayList<HashMap<String, Object>>();
String html = getURLContent(strng);
Document doc = Jsoup.parse(html);
Elements divs = doc.getElementsByClass("postBox");
for (Element div : divs) {
Element masthead = div.select("img[src].attachment-post-thumbnail").first();
String linkHref = masthead.attr("src");
Element masthead2 = div.select("h1").first().select("a").first();
String baslik = masthead2.text();
Element masthead3 = div.select("div.textPreview").first().select("p").first();
String desc = masthead3.text();
//Drawable drawable = LoadImageFromWebOperations();
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put("Resim", LoadImageFromWebOperations(linkHref));
hm.put("Baslik", baslik);
hm.put("Desc", desc);
myBooks2.add(hm);
}
return myBooks2;
}
private Drawable LoadImageFromWebOperations(String url){
try{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
public class backgroundLoadListView extends AsyncTask<String, Void, Void> {
private ProgressDialog dialog = new ProgressDialog(getActivity());
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
// adapter = new MyCustomAdapter( getActivity().getApplicationContext(), R.layout.list, imageliste);
//adapter.notifyDataSetChanged();
dialog.dismiss();
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
dialog.setMessage("Yükleniyor...");
dialog.show();
}
#Override
protected Void doInBackground(String... arg) {
// TODO Auto-generated method stub
imageliste=getImageLinks(arg[0]);
return null;
}
}
public class backgroundLoadListView2 extends AsyncTask<Void, Void, Void> {
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout.customslidingtabhost, null);
ListView listView1=(ListView)view.findViewById(R.id.list);
//MyCustomAdapter adapter = new MyCustomAdapter(getActivity(), R.layout.list, imageliste);
//listView1.setAdapter(adapter);
int[] colors = {0xFFFFFFFF, 0xFF87CEEB, 0xFFFFFFFF}; // red for the example
listView1.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
listView1.setDividerHeight(2);
listView1.setBackgroundColor(Color.WHITE);
((PullToRefreshListView) listView1).onRefreshComplete();
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
imageliste=getImageLinks("http://www.teknoinfo.net/kategoriler/haberler/teknoloji-haberleri");
return null;
}
}
public static TestFragment newInstance(String content) {
TestFragment fragment = new TestFragment();
return fragment;
}
private String mContent = "???";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
//new backgroundLoadListView().execute("http://www.teknoinfo.net/haberler");
MyCustomAdapter adapter = new MyCustomAdapter( getActivity().getApplicationContext(), R.layout.list, imageliste);
View view = inflater.inflate(R.layout.customslidingtabhost, null);
final ListView v=(ListView)view.findViewById(R.id.list);
/*((PullToRefreshListView) v).setOnRefreshListener(new OnRefreshListener() {
public void onRefresh() {
// Do work to refresh the list here.
new backgroundLoadListView2().execute();
}
});*/
int[] colors = {0xFFFFFFFF, 0xFF87CEEB, 0xFFFFFFFF}; // red for the example
v.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
v.setDividerHeight(2);
v.setBackgroundColor(Color.WHITE);
v.setAdapter(adapter);
((PullToRefreshListView) v).setOnRefreshListener(new OnRefreshListener() {
public void onRefresh() {
// Do work to refresh the list here.
//new backgroundLoadListView2().execute();
// new backgroundLoadListView().execute("http://www.teknoinfo.net/haberler");
}
});
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
}

It is hard to work through all that code you posted (most of which has not to do with your question), but as far as I can tell you have not even set up a ViewPager from your code or it is hidden in some XML file that you didn't post.
What you need to do is create a ViewPager instance. For example in your fragment's XML layout file, like:
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
Now, in your Fragment you create an adapter that specifies the pages that you want (similarly to how a ListAdapter specifies items to a ListView). For that, create a class (you can do that inline in the Fragment code) that inherits from PagerAdapter. For example, something like:
private class MyPagerAdapter extends PagerAdapter implements TitleProvider {
private ListView pagerListView1;
private ListView pagerListView2;
public MyPagerAdapter() {
LayoutInflater inflater = getActivity().getLayoutInflater();
pagerListView1 = (ListView) inflater.inflate(R.layout.fragment_pagerlist, null);
pagerListView2 = (ListView) inflater.inflate(R.layout.fragment_pagerlist, null);
}
#Override
public int getCount() {
return 2;
}
#Override
public Object instantiateItem(View container, int position) {
switch (position) {
case 0:
((ViewPager) container).addView(pagerListView1, 0);
return pagerListView1;
case 1:
((ViewPager) container).addView(pagerListView2, 0);
return pagerListView2;
}
return null;
}
#Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == (View) object;
}
#Override
public void finishUpdate(View container) {
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void startUpdate(View container) {
}
#Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
}
Note that I hard-coded 2 pages/lists in this pager. You can fill those as you would with any other ListView. The layout of the pages is just a simple ListView that is inflated from the fragment_pagerlist XML file, which looks something like:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:cacheColorHint="#color/BackgroundLight" />
Finally, you bind the ViewPager's adapter somewhere in your onActivityCreated method:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ViewPager pager = (ViewPager) getView().findViewById(R.id.pager);
pager.setAdapter(new MyPagerAdapter());
}
Note that this does not yet give you a ViewPagerIndicator. Check Jake Wharton's excellent library for that.
If you want the code to a fully implemented and working version, with multiple lists (and other views) in a ViewPager and a ViewPagerIndicator, take a look at the open source RateBeer for Android project; specifically http://code.google.com/p/ratebeerforandroid/source/browse/RateBeerForAndroid/src/com/ratebeer/android/gui/fragments/SearchFragment.java#486 for a real-world PagerAdapter.

Related

How to pass a row in listview in a fragment to listview in another fragment in same activity when click button in row?

I have two fragments in one activity and there is a ViewPager element in main_activity.xml. I have created two different classes that exteds Fragment class for each fragments and overrided onCreateView methods. The first fragment is a channels list. So it's a listview that its each row consist of a textview and a button. I want when the user click the button, get passed the row to listView in second fragment. I created a interface in ChannelsFragment named FavButtonClickedListener and overrided this interface in MainActivity.java. Then I added onAttach() method into ChannelsFragment in order to reference MainActivity. In getView method of Adapter of ChannelsFragment class I have set the clicklistener to imageButtons in each row and I called whenFavButtonClicked.onFavButtonClicked(); My code is working for SharedPreferences hovewer I need to restart the application to see changes in favorites list. I want to add the rows dynamically. I don't know where I wrong please help me
ChannelsFragment :
public class ChannelsFragment extends Fragment {
ViewGroup rootVg;
Context context;
FavButtonClickedListener whenFavButtonClicked;
private final List<String> favValues = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.channels_list_layout, container, false);
final ListView channelsList = (ListView) rootView.findViewById(R.id.channelsListView);
String[] chNames = new String[]{"ChA","ChB","ChC"};
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < chNames.length; ++i) {
list.add(chNames[i]);
}
//set adapter to listview
MyListAdapter listAdapter = new MyListAdapter(getActivity(),chNames);
channelsList.setAdapter(listAdapter);
return rootView;
}
public interface FavButtonClickedListener {
public void onFavButtonClicked(int position,List<String>favs);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
context = getActivity();
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
whenFavButtonClicked = (FavButtonClickedListener)context;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
}
ChannelsFragment Adapter class:
public class MyListAdapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
URL channelUrl;
public MyListAdapter(Context context, String[] values) {
super(context, R.layout.list_item_complex, values);
this.context = context;
this.values = values;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public long getCount(int position) {
// TODO Auto-generated method stub
return values.length;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item_complex, parent, false);
TextView chNameText = (TextView) rowView.findViewById(R.id.chName);
ImageView imageView = (ImageView) rowView.findViewById(R.id.chImage);
final ImageButton imageButton = (ImageButton)rowView.findViewById(R.id.starButon);
Button chButton = (Button)rowView.findViewById(R.id.chButon);
chNameText.setText(values[position]);
//Set the channels' logo appropriately
if(values[position].equals("CHa")){
imageView.setImageResource(R.drawable.CHaImage);
imageButton.setId(0);
}else if(values[position].equals("CHb")){
imageView.setImageResource(R.drawable.CHbImage);
imageButton.setId(1);
}else if(values[position].equals("Chc")){
imageView.setImageResource(R.drawable.CHcImage);
imageButton.setId(2);
}else{
imageView.setImageResource(R.drawable.defult_bacground_logo);
}
}
//Determine the target url for each channel correctly
chButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent("android.intent.action.Activity2");
String chName = null;
if(values[position].equals("CHa")){
i.putExtra("CHa", "http://www.cha.com/");
chName = "CHa";
i.putExtra("nameofCh", ChName);
startActivity(i);
}
if(values[position].equals("CHb")){
i.putExtra("CHb", "http://www.chb.com/");
chName = "CHb";
i.putExtra("nameofCh",ChName);
startActivity(i);
}
if(values[position].equals("CHc")){
i.putExtra("CHc", "http://www.chc.com/");
chName = "CHc";
i.putExtra("nameofCh",ChName);
startActivity(i);
}
});
final Context konteks;
konteks = this.getContext();
//set button click for favorite channels list
// I use this imageButton for create a favorites list in listview in FavoritesChannels
//fragment. I want when the user click this button, the row that has been clicked, get
//passed to FavoritesChannels fragment
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageButton.setImageResource(R.drawable.button_states_star);
SharedPreferences prefs = konteks.getSharedPreferences("favorites",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
if(imageButton.getId()==0){
editor.putBoolean("CHaChecked", true);
editor.commit();
favValues.add("CHa");
whenFavButtonClicked.onFavButtonClicked(position,favValues);
Toast.makeText(getActivity(), "CHa have been added to favlist", Toast.LENGTH_SHORT).show();
}
else if(imageButton.getId()==1){
editor.putBoolean("CHbChecked", true);
favValues.add("CHb");
editor.commit();
whenFavButtonClicked.onFavButtonClicked(position,favValues);
Toast.makeText(getActivity(), "CHb have been added to favlist", Toast.LENGTH_SHORT).show();
}
else if(imageButton.getId()==2){
editor.putBoolean("CHcChecked", true);
favValues.add("CHc");
editor.commit();
whenFavButtonClicked.onFavButtonClicked(position,favValues);
Toast.makeText(getActivity(), "CHc have been added to favlist", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(), "CH have been added to favlist", Toast.LENGTH_SHORT).show();
}
}
});
return rowView;
}
}
MainActivity.java:
onFavButtonClicked() implementation is at end of file
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener,KanallarFragment.FavButtonClickedListener {
private static final int NUM_PAGES = 2;
public SharedPreferences prefs;
FragmentTransaction fragmentTransaction;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
//Initiate Actionbar instance and define its tab names
private ActionBar actionBar;
private String[] tabs = { "Channels", "Favorites"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragMgr = getSupportFragmentManager();
fragmentTransaction = fragMgr.beginTransaction();
fragmentTransaction.add(new FavoriteFragment(), "favsFragment");
fragmentTransaction.add(new ChannelsFragment(), "channelsFragment");
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
//ViewPager's PageChangeListener
mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
//Retrieve actionbar and set some properties
actionBar = getSupportActionBar();
//actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setStackedBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_gradient));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
// Adding Tabs to actionbar
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
for(int i = 0; i<actionBar.getTabCount(); i++){
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.tab_layout, null);
TextView titleTV = (TextView) customView.findViewById(R.id.action_custom_title);
titleTV.setText(tabs[i]);
ImageView tabImage = (ImageView)customView.findViewById(R.id.tab_icon);
if(i==0){
tabImage.setImageResource(R.drawable.television_icon_64);
}else{
tabImage.setImageResource(R.drawable.star_icon);
}
actionBar.getTabAt(i).setCustomView(customView);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/*public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
mPager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}*/
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new ChannelsFragment();
case 1:
return new FavoritesFragment();
}
return null;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
#Override
public void onTabReselected(Tab arg0,FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab arg0,FragmentTransaction arg1) {
// TODO Auto-generated method stub
mPager.setCurrentItem(arg0.getPosition());
}
#Override
public void onTabUnselected(Tab arg0,FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
public void onFavButtonClicked(int position,List<String>favs) {
// TODO Auto-generated method stub
FavoritesFragment favFrag = (FavoritesFragment)getSupportFragmentManager().findFragmentByTag("favsFragment");
if(favFrag !=null){
favFrag.updateList(position,favs);
}
}
And FavoritesFragment class that contain updateList() method :
public class FavorilerFragment extends Fragment {
Context context;
private final List<String> values = new ArrayList<String>();
private final List<String> favValues = new ArrayList<String>();
ListView listView;
TextView textView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Context context = getActivity();
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.favorites_list_layout, container, false);
listView = (ListView)rootView.findViewById(R.id.f_channels_ListView);
textView = (TextView)rootView.findViewById(R.id.favListMessage);
SharedPreferences prefs = context.getSharedPreferences("favorites",getActivity().MODE_PRIVATE);
//-----SharedPreferences values for adapter----
if(prefs.getBoolean("CHaChecked", false)){
values.add("Cha");
}
if(prefs.getBoolean("CHbChecked", false)){
values.add("Chb");
}
if(prefs.getBoolean("ChcChecked", false)){
values.add("Chc");
}
//-----------------------------------------------
if(values.size() == 0){
textView.setText("Favorites List is empty");
}else{
textView.setText("");
}
MyListAdapter adapter = new MyListAdapter(getActivity(),values);
listView.setAdapter(adapter);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
context = getActivity();
}
public void updateList(int position,List<String> argfavValues){
for(int a= 0;a<argFavValues.size();a++){
favValues.add(argFavValues.get(a));
}
MyListAdapter = new MyListAdapter(getActivity(),favValues);
adaptor.notifyDataSetChanged();
listView.setAdapter(adapter);
}
public static class MyListAdapter extends ArrayAdapter<String>{
private final Context context;
private final List<String> values = new ArrayList<String>();
URL ChUrl;
public MyListAdapter(Context context, List<String> values) {
super(context, R.layout.list_item_complex_fav, values);
this.context = context;
for(int i=0;i<values.size();i++){
this.values.add(values.get(i));
}
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public long getCount(int position) {
// TODO Auto-generated method stub
return values.size();
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item_complex_fav, parent, false);
TextView chNameText = (TextView) rowView.findViewById(R.id.ChName);
ImageView imageView = (ImageView) rowView.findViewById(R.id.ChannelsImage);
Button chButton = (Button)rowView.findViewById(R.id.chButon);
chNameText.setText(values.get(position));
//Set the channels' logo appropriately
if(values.get(position).equals("CHa")){
imageView.setImageResource(R.drawable.CHaImage);
}else if(values.get(position).equals("CHb")){
imageView.setImageResource(R.drawable.CHbImage);
}else if(values.get(position).equals("CHc")){
imageView.setImageResource(R.drawable.CHcImage);
else{
imageView.setImageResource(R.drawable.default_logo);
}
return rowView;
}
}
Per the official documentation, fragment to fragment communication should be routed through the activity.
You need to create an interface in your channels fragment with some callback method defined, like onSelected. When a list item is selected, call that interface method using the activity context:
public class ChannelsFragment extends ListFragment {
OnItemSelectedListener mCallback;
public interface OnItemSelectedListener {
public void onSelected(int position);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnItemSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnItemSelectedListener");
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
mCallback.onSelected(position);
}
}
Then, your activity needs to implement ChannelFragment.OnItemSelectedListener and define the callback method.
public void onSelected(int position) {
Fragment2 fragment = (Fragment2)
getSupportFragmentManager().findFragmentById(R.id.fragment2);
if (fragment != null) {
fragment.updateValue(position);
}
}
You can pass data via parameters. To send the data to the other fragment, create a public method in the fragment which can be called directly from the activity.

How to Implement on scroll load ListView in Android?

I have implemented listview in fragment. In that listview i have used custom adapter to display images with their description. Data is coming from web server. There are many list items with image so I'd like to implement on scroll load ListView for the same.
Which approach should i follow to do the same? I have tried to google it and found some sample but it does not fit into my requirement.
Here is the code of my Fragment containing listview. Thanks in advance.
FragmentTab1.class
public class FragmentTab1 extends Fragment implements
AdapterView.OnItemClickListener {
DisplayImageOptions options;
protected ImageLoader imageLoader = ImageLoader.getInstance();
JSONArray AdsArray = null;
String TAG_IMAGE = "image";
String TAG_DISCRIPTION = "description";
ListView lv;
final Context context = getActivity();
static ArrayList<HashMap<String, String>> adList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab1, container,false);
options = new DisplayImageOptions.Builder().showStubImage(R.drawable.no_image).cacheInMemory().cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565).build();
lv = (ListView) rootView.findViewById(R.id.listView1);
lv.setOnItemClickListener(this);
adList = new ArrayList<HashMap<String, String>>();
new LoadMyAds().execute();
return rootView;
}
class LoadMyAds extends AsyncTask<Void, Void, String> {
#Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
UserFunctions userFunction = new UserFunctions();
String type = "all";
JSONObject json = userFunction.myAds(userid, type);
try {
if (json.getString("status") != null) {
String res = json.getString("status");
if (Integer.parseInt(res) == 1) {
adList.clear();
AdsArray = json.getJSONArray("myadslistsArray");
for (int i = 0; i < AdsArray.length(); i++) {
JSONObject c = AdsArray.getJSONObject(i);
String description = c.getString(TAG_DISCRIPTION);
String image = c.getString(TAG_IMAGE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_IMAGE, image);
map.put(TAG_DISCRIPTION, description); adList.add(0, map);
}
} else {
Toast.makeText(getActivity().getApplicationContext(),
"parsing error", Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
// image parameter need to be added
lv.setAdapter(new CustomAdapter(adList));
}
}
class CustomAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> locallist;
public CustomAdapter(ArrayList<HashMap<String, String>> list) {
// TODO Auto-generated constructor stub
locallist = list;
imageLoader.init(ImageLoaderConfiguration
.createDefault(getActivity()));
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return locallist.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
// return null;
return getItem(getCount() - position - 1);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
if (row == null) {
row = getActivity().getLayoutInflater().inflate(
R.layout.ad_list_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.desc= (TextView) row.findViewById(R.id.title);
viewHolder.img = (ImageView) row.findViewById(R.id.thumbImage);
row.setTag(viewHolder);
}
final ViewHolder holder = (ViewHolder) row.getTag();
holder.desc.setText(""+locallist.get(position).get(TAG_DISCRIPTION));
imageLoader.displayImage(locallist.get(position).get(TAG_IMAGE),
holder.img, options);
return row;
}
}
class ViewHolder {
protected TextView desc;
protected ImageView img;
}
}
Attach scroll listener to the listView
lv.setOnScrollListener(new EndlessScrollListener());
Here is the class implementation:
class EndlessScrollListener implements OnScrollListener {
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
new LoadMyAds().execute();
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
}

Replacing Fragment in ViewPager shows blank screen?

I'm having a lot of trouble getting Fragments to work with Tabs/Swiping. I have one activity and two Fragments, one which has a ListView. What I want is when I swipe from fragment 1 to the fragment with the ListView, to refresh the ListView or if possible recreate the second fragment....
What I am trying right now is to use FragmentTransaction.replace() like this:
public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
Fragment frag = new FragmentTwo();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
if (arg0.getPosition() == 1) {
trans.replace(R.id.pager, frag);
trans.addToBackStack(null);
trans.commit();
}
}
So I thought that when I swipe to tab 2, which is the one with the ListView, it would replace it with a new one, which it creates. What is actually happening is that when I swipe to tab 2, it recreates the fragment with the ListView (I know because I have a dialog in the onCreateView which shows to the screen) but then the fragment/tab is blank/black. What am I doing wrong? Where does this fragment which is created go?
I don't know if the code of the fragments is relevant but please let me know if I should post it and I can do that. This is my PagerAdapter class.
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
Thanks for any help! I'm really pulling my hair out over this one :(
Edit:
When you swipe from tab 1 to tab 2, tab 2 shows my dialog and then loads the tab, but if I go back to tab 1 its all black...and if I then go to tab 2 again, it creates again and shows my dialog and then goes black as well...
Edit: (adding listview fragment code)
public class FragmentTwo extends Fragment {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser_Helpers2 jParser = new JSONParser_Helpers2();
ArrayList<HashMap<String, String>> tripList;
// url to get all products list
private static String url_all_trips = "http://10.0.2.2/android_connect/get_all_trips.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_TRIPS = "trips";
private static final String TAG_TRIPID = "tripid";
private static final String TAG_TRIPNAME = "tripName";
private static final String TAG_UID = "uid32";
// products JSONArray
JSONArray trips = null;
View rootView;
Button btnRefresh;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//View rootView = inflater.inflate(R.layout.fragment_one, container, false);
rootView = inflater.inflate(R.layout.fragment_two, container, false);
btnRefresh = (Button) rootView.findViewById(R.id.refresh);
// Hashmap for ListView
tripList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = (ListView) rootView.findViewById(R.id.list);
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//getting values from selected ListItem
String tripid = ((TextView) view.findViewById(R.id.pid)).getText().toString();
//starting new intent
Intent in = new Intent(getActivity(), EditTrip_Activity.class);
//sending pid to next activity
in.putExtra(TAG_TRIPID, tripid);
//starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
return rootView;
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
ListView lv = (ListView) rootView.findViewById(R.id.list);
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading your trips. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_trips, "GET", params);
// Check your log cat for JSON response
Log.d("All Trips: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
trips = json.getJSONArray(TAG_TRIPS);
// looping through All Products
for (int i = 0; i < trips.length(); i++) {
JSONObject c = trips.getJSONObject(i);
// Storing each json item in variable
String tripid = c.getString(TAG_TRIPID);
String tripname = c.getString(TAG_TRIPNAME);
String userId = c.getString("uid");
// creating new HashMap
DatabaseHandler_Helpers db = new DatabaseHandler_Helpers(getActivity());
HashMap<String, String> map = new HashMap<String, String>();
if (userId.equals(db.getUserDetails().get("uid"))) {
// adding each child node to HashMap key => value
map.put(TAG_TRIPID, tripid);
map.put(TAG_TRIPNAME, tripname);
// adding HashList to ArrayList
tripList.add(map);
} //else {
//map.put(TAG_TRIPID, "");
//map.put(TAG_TRIPNAME, "You have no tracked trips.");
//tripList.add(map);
//}
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getActivity(),
NewTrip_Activity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
((Activity) getActivity()).runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
getActivity(), tripList,
R.layout.list_item, new String[] { TAG_TRIPID,
TAG_TRIPNAME},
new int[] { R.id.pid, R.id.name });
// updating listview
((ListView) lv.findViewById(R.id.list)).setAdapter(adapter);
}
});
}
}
}
Try to extend FragmentStatePagerAdapter instead of FragmentPagerAdapter.
Here's my code for fragments and it works fine for me:
//Adapter
public class AllPagesAdapter extends FragmentStatePagerAdapter {
public AllPagesAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Android();
case 1:
return new CoreJava();
case 2:
return new J2EE();
case 3:
return new Database();
case 4:
return new WebServices();
}
return null;
}
#Override
public int getCount() {
return 5;
}
}
//For example, this is one fragment:
public class Database extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View databaseview = inflater.inflate(R.layout.database, container, false);
return databaseview;
}
}
//My MainActivity
public class AllActivities extends FragmentActivity implements ActionBar.TabListener {
public ViewPager viewPager;
private AllPagesAdapter mAdapter;
private ActionBar actionBar;
private String [] tabs = {"Android","CoreJava","J2EE","Database","Web Services"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing all stuff
viewPager = (ViewPager)findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new AllPagesAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Add the tabs here
for(String tab_name:tabs){
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int position){
//on Page change, that particular page should be selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0,float arg1,int arg2){
}
#Override
public void onPageScrollStateChanged(int position){
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
}
Let me know if you also want the implementation of the listview in any one fragment.
Hope this helps..:)
My children fragments are in a ViewPager. I just replace the ViewPage adapter by FragmentStatePagerAdapter instead of FragmentPagerAdapter
Child Fragment
....
mViewPager.setAdapter(new SearchOriginTabsPagerAdapter(getFragmentManager()));
Adapter...
public class SearchOriginTabsPagerAdapter extends FragmentStatePagerAdapter {
public SearchOriginTabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new NearbyFragment();
case 1:
return new RecentOriginFragment();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}
}
Alright.. Here's the thing..
I didn't really use your code but used similar concept. Its gonna be a complete solution.
//This is the starting point of the app. It has many fragments and I am using my own json to get the data.
public class AllActivities extends FragmentActivity implements ActionBar.TabListener {
public ViewPager viewPager;
private AllPagesAdapter mAdapter;
private ActionBar actionBar;
private String [] tabs = {"Android","CoreJava","J2EE","Database","Web Services"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing all stuff
viewPager = (ViewPager)findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new AllPagesAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Add the tabs here
for(String tab_name:tabs){
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int position){
//on Page change, that particular page should be selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0,float arg1,int arg2){
}
#Override
public void onPageScrollStateChanged(int position){
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
}
//This is one of the fragment and assume that Expandablelistview is your list view.. In both the cases, i am setting the adapter in the onActivityCreated() method.
public class Android extends android.support.v4.app.Fragment {
ExpandableListAdapter listAdapter;
// private ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
ListView lv1;
private static final String QUESTION = "question";
private static final String ANSWERS = "answer";
// ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mikeview = inflater.inflate(R.layout.androidlayout, container, false);
return mikeview;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ExpandableListView expListView = null;
try{
expListView = (ExpandableListView) getActivity().findViewById(R.id.androidExpandableList);
}
catch (Exception e){
e.printStackTrace();
}
new Thread(){
#Override
public void run(){
}
}.start();
try {
setParent();
prepareChild();
} catch (Exception e) {
e.printStackTrace();
}
ExpandableListAdapter listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
/*Toast.makeText(
getActivity(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();*/
return true;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
/* Toast.makeText(getActivity(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();*/
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
/*Toast.makeText(getActivity(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();*/
}
});
}
public void setParent(){
listDataHeader = new ArrayList<String>();
try{
JSONObject json = new JSONObject(loadJSONFromAsset());
JSONArray array = json.getJSONArray("androidquestion");
for(int my =0;my<array.length();my++){
JSONObject c = array.getJSONObject(my);
String topics = c.getString(QUESTION);
listDataHeader.add(topics);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
public void prepareChild() throws JSONException {
listDataChild = new HashMap<String, List<String>>();
try{
JSONObject json = new JSONObject(loadJSONFromAsset());
JSONArray array = json.getJSONArray("androidquestion");
for(int mz = 0;mz<array.length();mz++){
ArrayList<String> child = new ArrayList<String>();
JSONObject d = array.getJSONObject(mz);
String ans = d.getString(ANSWERS);
child = new ArrayList<String>();
child.add(ans);
int position = mz ;
listDataChild.put(listDataHeader.get( position),child);
}
}
catch(JSONException e) {
e.printStackTrace();
}
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("android.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
//This is the adapter.
public class AllPagesAdapter extends FragmentStatePagerAdapter {
public AllPagesAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Android();
case 1:
return new CoreJava();
case 2:
return new J2EE();
case 3:
return new Database();
case 4:
return new WebServices();
}
return null;
}
#Override
public int getCount() {
return 5;
}
}
//This is the adapter for my expandable list view.. in your case, your can just use the listadapter or any other adapter you want to use. This is just an optional part.
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.newlistitems,null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.newlistviewitems);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater myInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = myInflater.inflate(R.layout.newlistgroup, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.newlistviewgroup);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
If anyone using viewpager2 then use the below constructor.
FragmentStateAdapter(fragmentManager, lifecycle)

Android AsyncTask: Skipped X frames - too much work inside main thread

I am making some RSS reader for some website, so I wanted to implement actionbar and viewpager on lower versions of Androd that 4.0, so I am using ActionBarSherlock and ViewPagerIndicator from Jake Wharton, so I am working with fragments.
And I want to read some RSS feed from URL, and I have AsyncTask class for that called LoadXMLData, and here is code of that class.
LoadXMLData class:
public class LoadXMLData extends AsyncTask<String, RSSFeed, RSSFeed>{
private ProgressDialog mProgressDialog;
private Context context;
RSSFeed feed;
private String RSSFEEDURL = "http://balkanandroid.com/feed/";
public LoadXMLData(Context context) {
this.context = context;
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage("Molimo Vas, sačekajte. Podaci se učitavaju.");
}
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
Log.d("OVDE SAM:", "onPreExecute()");
}
#Override
protected RSSFeed doInBackground(String... urls) {
// Obtain feed
DOMParser myParser = new DOMParser();
feed = myParser.parseXml(urls[0]);
Log.d("OVDE SAM:", "PARSIRAM XML");
return feed;
}
#Override
protected void onPostExecute(RSSFeed result) {
mProgressDialog.dismiss();
super.onPostExecute(result);
}
}
Also I have class MainActivity, which extends SherlockFragmentActivity.
public class MainActivity extends SherlockFragmentActivity {
BAFragmentAdapter mAdapter;
RSSFeed feed;
Application myApp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("OVDE SAM:", "MAIN ACTIVITY");
myApp = getApplication();
mAdapter = new BAFragmentAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(mAdapter);
TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(pager);
}
public RSSFeed getFeed() {
return feed;
}
public void setFeed(RSSFeed feed) {
this.feed = feed;
}
And the most imporatnt I have few fragment classes (LatestFragments, PhonesFragments, TabletFragments, ApplicationFragments and so on), and on each fragments I have almost same code as shown below.
Here is the full code of LatestFragment:
public class LatestFragment extends Fragment {
GridView lv;
RSSFeed feed;
CustomListAdapter adapter;
private String RSSFEEDURL = "http://balkanandroid.com/feed/";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_najnovije, container,
false);
AsyncTask<String, RSSFeed, RSSFeed> xml = new LoadXMLData(getActivity())
.execute(RSSFEEDURL);
// AsyncTask<String, RSSFeed, RSSFeed> load = new
// LoadXMLData(getActivity()).execute(RSSFEEDURL);
try {
feed = xml.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lv = (GridView) view.findViewById(R.id.GridView1);
// Set an Adapter to the ListView
adapter = new CustomListAdapter();
lv.setAdapter(adapter);
// Set on item click listener to the ListView
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// actions to be performed when a list item clicked
int pos = arg2;
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
Intent intent = new Intent(getActivity(), DetailsActivity.class);
intent.putExtras(bundle);
intent.putExtra("pos", pos);
startActivity(intent);
}
});
return view;
}
#Override
public void onDestroy() {
super.onDestroy();
adapter.imageLoader.clearCache();
adapter.notifyDataSetChanged();
}
class CustomListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public ImageLoader imageLoader;
public CustomListAdapter() {
layoutInflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(getActivity().getApplicationContext());
}
public int getCount() {
// TODO Auto-generated method stub
// Set the total list item count
return feed.getItemCount();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the item layout and set the views
View listItem = convertView;
int pos = position;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.list_item, null);
}
// Initialize the views in the layout
ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);
TextView tvTitle = (TextView) listItem.findViewById(R.id.title);
TextView tvDate = (TextView) listItem.findViewById(R.id.tvDate);
// Set the views in the layout
imageLoader.DisplayImage(feed.getItem(pos).getImage(), iv);
tvTitle.setText(feed.getItem(pos).getTitle());
tvDate.setText(feed.getItem(pos).getDate());
return listItem;
}
}
}
As you can see I use AsyncTask class LoadXMLData to read data in doInBackground() method, and then I call that AsyncTask class under all fragment classes with this code, because I need result of RSSFeed, because I need that data to show it to user.
AsyncTask<String, RSSFeed, RSSFeed> xml = new LoadXMLData(getActivity())
.execute(RSSFEEDURL);
try {
feed = xml.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And it works, but when I click to open other view from view pager its slow, almost freezes my app, and I get this message with Logcat.
07-10 00:22:40.598: I/Choreographer(623): Skipped 316 frames! The application may be doing too much work on its main thread.
That is because you are calling get() on your AsyncTask, which blocks. Do not do this.
Instead, use your feed in onPostExecute() of the AsyncTask.

Getting AsyncTask result data from Fragment

I have AsyncTask class called LoadXMLData, and as you can see I parse XML data in doInBackground() method.
public class LoadXMLData extends AsyncTask<String, RSSFeed, RSSFeed>{
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
private Context context;
RSSFeed feed;
public LoadXMLData(Context context) {
this.context = context;
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage("Molimo Vas, sačekajte. Podaci se učitavaju.");
}
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
Log.d("OVDE SAM:", "onPreExecute()");
}
#Override
protected RSSFeed doInBackground(String... urls) {
// Obtain feed
DOMParser myParser = new DOMParser();
feed = myParser.parseXml(urls[0]);
Log.d("OVDE SAM:", "PARSIRAM XML");
return feed;
}
#Override
protected void onPostExecute(RSSFeed result) {
mProgressDialog.dismiss();
super.onPostExecute(result);
}
}
And I have few fragments, where I need to get data from that AsyncTask. How I could do that?
Here is the code of an fragment called NajnovijeFragment.
public class NajnovijeFragment extends Fragment{
GridView lv;
RSSFeed feed;
CustomListAdapter adapter;
private String RSSFEEDURL = "http://balkanandroid.com/feed/";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_najnovije, container,
false);
lv = (GridView) view.findViewById(R.id.GridView1);
// Set an Adapter to the ListView
adapter = new CustomListAdapter();
lv.setAdapter(adapter);
// Set on item click listener to the ListView
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// actions to be performed when a list item clicked
int pos = arg2;
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
Intent intent = new Intent(getActivity(), DetailsActivity.class);
intent.putExtras(bundle);
intent.putExtra("pos", pos);
startActivity(intent);
}
});
return view;
}
#Override
public void onDestroy() {
super.onDestroy();
adapter.imageLoader.clearCache();
adapter.notifyDataSetChanged();
}
class CustomListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public ImageLoader imageLoader;
public CustomListAdapter() {
layoutInflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(getActivity().getApplicationContext());
}
public int getCount() {
// TODO Auto-generated method stub
// Set the total list item count
return feed.getItemCount();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the item layout and set the views
View listItem = convertView;
int pos = position;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.list_item, null);
}
// Initialize the views in the layout
ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);
TextView tvTitle = (TextView) listItem.findViewById(R.id.title);
TextView tvDate = (TextView) listItem.findViewById(R.id.tvDate);
// Set the views in the layout
imageLoader.DisplayImage(feed.getItem(pos).getImage(), iv);
tvTitle.setText(feed.getItem(pos).getTitle());
tvDate.setText(feed.getItem(pos).getDate());
return listItem;
}
}
}
The easiest way to get data from an ASyncTask is by implementing a callback.
Create an Interface:
public interface OnXMLLoadFinishedListener {
public void onXMLDataReady(RSSFeed results);
}
In you LoadXMLData:
private OnXMLLoadFinishedListener listener;
public void setOnXMLLoadFinishedListener(OnXMLLoadFinishedListener listener){
this.listener = listener;
}
#Override
protected void onPostExecute(RSSFeed result) {
super.onPostExecute(result);
listener.onXMLDataReady(RSSFeed results);
}
In your Fragment:
public class NajnovijeFragment extends Fragment implements OnXMLLoadFinishedListener{
and override onXMLDataReady:
#override
public void onXMLDataReady(RSSFeed results){
//display your data.
}
Make sure that when you create your AsyncTask instance you set the listener otherwise this will not work:
LoadXMLData xmlLoader = new LoadXMLData();
xmlLoader.setOnXMLLoadFinishedListener(this);
Your AsyncTask already knows context, so you could call back into your activity (called ActivityMain for illustrative purposes) in onPostExecute. e.g.
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
((ActivityMain) context).loadCompleteHandler(param1,param2,...)
}
It's then up to you how you want to implement loadCompleteHandler in your activity. Now your activity might not exist, so you must be careful to cancel the AsyncTask when the activity is removed. Fragments belonging to an activity can also access the activity.
AsyncTask is a Class that is very related to the UI, if you need to update the UI with this XML parsing you should take this consideration:
Make the asynctask an inner class in your fragment or
Pass the fragment to your asynctask
Update the fragment's view in onPostExecute()
In any case you should check if your activity is null, if so... avoid updating views, something like that:
onPostExecute(Object xml) {
if(getActivity != null) {
// update Views like...
textViewLabel.setText(parsedXml.getTitle);
}
}
I would suggest you to use SafeAsyncTask, which is a java class from the Roboguice Project, only one file, and it is related to java.util.concurrent.Callable, just copy and paste the source:
SafeAsyncTask.java
How to use it!

Categories

Resources