Delay in drawing - android

I have a list view with two text view and a image view and items I populate it with are 114 , I have created the list view with my custom adapter but the problem is that it takes a while like approx. 1 sec to 2 secs to load up and show the activity where I am displaying the list and whenever I open the activity it take time to show , so how can I minimize this delay time?
Here is my code:
public class ListViewFragment extends ListFragment {
int suraPicNum;
ArrayList<ListItem> imageArry = new ArrayList<ListItem>();
String[] names;
ImageTextListAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.listviewtest, container, false);
suraPicNum = R.drawable.j415;
for (int x = 1; x <= 30; x++) {
imageArry.add(new ListItem(suraPicNum, R.drawable.j414, "Juz " + x,
String.valueOf(x)));
suraPicNum++;
}
adapter = new ImageTextListAdapter(getActivity(),
R.layout.list_item_image_text, imageArry);
setListAdapter(adapter);
return view;
}
}
And activity class:
public class ListFragActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.fragactivity);
}

use this...Endless Adapter
it will help you...:)

use AsyncTask to load all the images and then attach them to adapter.

Related

Best way to fill a list with an adapter in a fragment

I developed an app which fills a list. It works fine in the way I did it but I'm not conviced that I solved the problem in a recommended way. I read that you should override onActivityCreated in a Fragment and fill the list there instead of doing this in onCreateView. onCreateView should only be used to inflate static views. Is this true? If yes, how should these two methods look like in the end?
This is my Fragment class:
public class FragmentMain extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
List<MyItem> items = createListItems();
ListView listView = (ListView) view.findViewById(R.id.list);
MyListAdapter adapter = new MyListAdapter(view.getContext(), items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(view.getContext(),
"Clicked " + position, Toast.LENGTH_LONG)
.show();
}
});
return view;
}
.
.
.
}
My MainActivity just adds the fragment:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentMain fm = new FragmentMain();
getFragmentManager().beginTransaction()
.add(R.id.fragment_main_container, fm).commit();
}
.
.
.
}
That is true to a certain extend only because onCreateView happens on the UI thread and you don't want anything slowing that down otherwise your UI will be slow and choppy. For example, in your fragment class you have a call to a method "createListItems()". I don't know how many items you're making but if it's a lot it could slow down your UI (especially if youre accessing a database and querying objects and so on). So you could do it in onActivityCreated but you could also use an AsyncTask. So your code would become something like this:
public class LoadListObjectsTask extend AsyncTask<Void, List<MyItem>, Void> {
private MyListAdapter myListAdapter;
private Context mContext;
public LoadListObjectsTask(Context context) {
this.mContext = context;
}
#Override
public void doInBackground(Void...params) {
//create your list objects here instead of on UI thread. This will run on a separate thread.
myListAdapter = new MyListAdapter(mContext, items);
return items; //return list of MyItems
}
//This is called when doInBackground is done. THIS WILL RUN ON THE UI THREAD So don't do
//anything slow here
#Override
public void onPostExecute(List<MyItem>...params //don't really need the list here//) {
listView.setAdapter(myListAdapter);
}
}
then in your fragment
public class FragmentMain extends Fragment {
private ListView listView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
List<MyItem> items = new ArrayList<MyItem>();
listView = (ListView) view.findViewById(R.id.list);
//new code
new LoadListObjectsTask(getActivity()).execute();
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(view.getContext(),
"Clicked " + position, Toast.LENGTH_LONG)
.show();
}
});
return view;
}
public void onResume()... {
//also add the task here so your content is reloaded on resume..
new LoadListObjectsTask(getActivity()).execute();
}
.
.
.
}
If you don't want to do this just make your List of MyItems a private field and move
List<MyItem> items = createListItems();
to onActivityCreated().
Hope that helps!

Create listview in fragment android

As the title I want to create a listview with custom row in Fragment. My code below.
Fragment class
public class PhotosFragment extends Fragment{
public PhotosFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_photos, container, false);
ArrayList<ListviewContactItem> listContact = GetlistContact();
ListView lv = (ListView)getActivity().findViewById(R.id.lv_contact);
lv.setAdapter(new ListviewContactAdapter(getActivity(), listContact));
return rootView;
}
private ArrayList<ListviewContactItem> GetlistContact(){
ArrayList<ListviewContactItem> contactlist = new ArrayList<ListviewContactItem>();
ListviewContactItem contact = new ListviewContactItem();
contact.SetName("Topher");
contact.SetPhone("01213113568");
contactlist.add(contact);
contact = new ListviewContactItem();
contact.SetName("Jean");
contact.SetPhone("01213869102");
contactlist.add(contact);
contact = new ListviewContactItem();
contact.SetName("Andrew");
contact.SetPhone("01213123985");
contactlist.add(contact);
return contactlist;
}
}
Adapter class
public class ListviewContactAdapter extends BaseAdapter{
private static ArrayList<ListviewContactItem> listContact;
private LayoutInflater mInflater;
public ListviewContactAdapter(Context photosFragment, ArrayList<ListviewContactItem> results){
listContact = results;
mInflater = LayoutInflater.from(photosFragment);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return listContact.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return listContact.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(R.layout.contact_item, null);
holder = new ViewHolder();
holder.txtname = (TextView) convertView.findViewById(R.id.lv_contact_item_name);
holder.txtphone = (TextView) convertView.findViewById(R.id.lv_contact_item_phone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtname.setText(listContact.get(position).GetName());
holder.txtphone.setText(listContact.get(position).GetPhone());
return convertView;
}
static class ViewHolder{
TextView txtname, txtphone;
}
}
But when I run the app that display no thing. Could anyone tell me what wrong here and how can I fix it?
I guess your app crashes because of NullPointerException.
Change this
ListView lv = (ListView)getActivity().findViewById(R.id.lv_contact);
to
ListView lv = (ListView)rootView.findViewById(R.id.lv_contact);
assuming listview belongs to the fragment layout.
The rest of the code looks alright
Edit:
Well since you said it is not working i tried it myself
Please use ListFragment. Otherwise, it won't work.
EDIT 1:
Then you'll only need setListAdapter() and getListView().
you need to give:
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
inside fragment.
The inflate() method takes three parameters:
The id of a layout XML file (inside R.layout),
A parent ViewGroup into which the fragment's View is to be inserted,
A third boolean telling whether the fragment's View as inflated from
the layout XML file should be inserted into the parent ViewGroup.
In this case we pass false because the View will be attached to the
parent ViewGroup elsewhere, by some of the Android code we call (in
other words, behind our backs). When you pass false as last parameter
to inflate(), the parent ViewGroup is still used for layout
calculations of the inflated View, so you cannot pass null as parent
ViewGroup .
View rootView = inflater.inflate(R.layout.fragment_photos, container, false);
So, You need to call rootView in here
ListView lv = (ListView)rootView.findViewById(R.id.lv_contact);
Instead:
public class PhotosFragment extends Fragment
You can use:
public class PhotosFragment extends ListFragment
It change the methods
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<ListviewContactItem> listContact = GetlistContact();
setAdapter(new ListviewContactAdapter(getActivity(), listContact));
}
onActivityCreated is void and you didn't need to return a view like in onCreateView
You can see an example here

How to eliminate white frame while flipping half-way through the next view?

iam using Aphid-FlipView-Library to display static html pages using webview.
but while flipping ,(moving) from one page to another iam getting some white frame ,after that second page is normally loading..
can any one help me , how to remove that flash appearance..
public class FlipTextViewFragment extends Fragment {
private FlipViewController flipView;
String[] urls = {"file:///android_asset/Introduction_print.html","file:///android_asset/introduction2.html"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
flipView = new FlipViewController(inflater.getContext(), FlipViewController.HORIZONTAL);
flipView.setAdapter(new adp(getActivity().getApplicationContext()));
return flipView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void setText(String item) {
flipView.setAdapter(new adp(getActivity().getApplicationContext()));
}}
then my adapter class is here:
package com.example.flipwebview;
public class adp extends BaseAdapter {
String[] urls = {"file:///android_asset/Introduction_print.html","file///android_asset/introduction2.html"};
Context con;
public adp(Context applicationContext) {
// TODO Auto-generated constructor stub
con=applicationContext;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return urls.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
WebView wv=new WebView(con);
wv.loadUrl(urls[position]);
return wv;
}}
By using my code , i can flip across multiple webviews,but the problem i am getting is when i am halfway flipping the page i cant see my next view,it seems to be a blank page,it gets loaded after i flip my page completely..here is the screen regarding that
loadUrl isn't fastest method to render local HTML files. Since they are included in your app you should load their content into String and use loadData method.
http://developer.android.com/reference/android/webkit/WebView.html#loadData%28java.lang.String,%20java.lang.String,%20java.lang.String%29
EDIT:
One more thing. You should use convertView instead of creating new WebView every time you flip.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
WebView webView = null;
if (convertView == null)
{
webView = new WebView(con);
} else {
webView = (WebView) convertView;
}
webView.loadUrl(urls[position]);
return webView;
}}

Fragment does not refresh its content

In the following code I have set up a fragment that shows 2 ListViews.
When the main activity call the Fragment methods:
prendiListaBest
and
prendiListaWorst
they call notifyDataSetChanged() on the adapters,
therefore the lists should update with the new contents of the ArrayLists.
But this does not happen.
Please any help and suggestion more than welcome
public class WorstBest extends Fragment {
public ListView lvBest;
public ListView lvWorst;
public MyAdapter myAdap;
public MyAdapter myAdap2;
public ArrayList<Stock> best;
public ArrayList<Stock> worst;
public View v;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v= inflater.inflate(R.layout.listaworstbest, container, false);
lvBest= (ListView)v.findViewById(R.id.listbest);
lvWorst=(ListView)v.findViewById(R.id.listworst);;
myAdap= new MyAdapter(best, getActivity());
myAdap2= new MyAdapter(worst, getActivity());
lvBest.setAdapter(myAdap);
lvWorst.setAdapter(myAdap2);
return v;
}
public void prendiListaBest(ArrayList<Stock> al){
Log.e("", "passo per prendilitsa del fragment worstbest");
best=al;
Log.e("", "nel fragment best è" + Integer.toString(best.size()));
myAdap.notifyDataSetChanged();
}
public void prendiListaWorst(ArrayList<Stock> al){
Log.e("", "passo per prendilitsa del fragment worstbest");
worst=al;
Log.e("", "nel fragment worst è" + Integer.toString(worst.size()));
myAdap2.notifyDataSetChanged();
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
}
This is the code of my custom adapter.
I posted as requested but I apologize because it is really messy:
public class MyAdapter extends BaseAdapter {
ArrayList<Stock> l;
Activity a;
public MyAdapter(ArrayList<Stock> li, Activity ac) {
// TODO Auto-generated constructor stub
l = li;
a=ac;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
if (row==null) {
LayoutInflater inflater=a.getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}
TextView tw=(TextView)row.findViewById(R.id.textView1);
String st=l.get(position).simbolo;
String st1=st;
String st2=st;
String st3=st;
String st4=st;
String st5=st;
String st6=st;
String st7=st;
String st8=st;
st8=st;
if(st.length()<=7){st1=st.concat(" ");st2=st1;st3=st1;st4=st1;st5=st1;st6=st1;st7=st1;st8=st1;}
if(st1.length()<=7){st2=st1.concat(" ");st3=st2;st4=st2;st5=st2;st6=st2;st7=st2;st8=st2;}
if(st2.length()<=7){st3=st2.concat(" ");st4=st3;st5=st3;st6=st3;st7=st3;st8=st3;}
if(st3.length()<=7){st4=st3.concat(" ");st5=st4;st6=st4;st7=st4;st8=st4;}
if(st4.length()<=7){st5=st4.concat(" ");st6=st5;st7=st5;st8=st5;}
if(st5.length()<=7){st6=st5.concat(" ");st7=st6;st8=st6;}
if(st6.length()<=7){st7=st6.concat(" ");st8=st7;}
if(st7.length()<=7){st8=st7.concat(" ");}
tw.setText(st8);
String allora=(String) tw.getText();
TextView tw2=(TextView)row.findViewById(R.id.nome);
///aggiungiamo numero minimo caratteri
tw2.setText((l.get(position).nome));
///
TextView tw3=(TextView)row.findViewById(R.id.prezzo);
tw3.setText((l.get(position).prezzo));
TextView tw4=(TextView)row.findViewById(R.id.cambiamento);
tw4.setText((l.get(position).cambiamento).concat(" %"));
try {
double value = Double.parseDouble(l.get(position).cambiamento);
if(value<0)tw4.setTextColor(Color.RED);
if(value>0)tw4.setTextColor(Color.GREEN);
TextView tw5=(TextView)row.findViewById(R.id.freccia);
if(value<0)tw5.setText("▼");
if(value>0)tw5.setText("▲");
if(value<0)tw5.setTextColor(Color.RED);
if(value>0)tw5.setTextColor(Color.GREEN);
} catch (Exception exc) {}
return row;
}
public int getCount() {
// TODO Auto-generated method stub
return l .size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return l .get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT *EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT *
Thank you very much, the answer makes perfectly sense.**
But I do not understand why the same Adapter works fine with the following fragment; I explain:
the following "listafragment" fragment has a similar logic, it uses the
prendiLista
method to retrieve the latest version of the
ListArray lt
exactly the same way it is done in the previous "WorstBest" fragment.
But in this case when
public void prendiLista(ArrayList<Stock> al){
lt=al;
myAdap.notifyDataSetChanged();
}
the view is updated !!!
Please what is the difference??? :-)
IN MY UNDERSTANDING THE CALL TO notifyDataSetChanged() WAS ENOUGH TO UPDATE THE VIEW !!
Thanks a lot anyway! (I will accept answer tomorrow morning).
public class listafragment extends Fragment {
public ListView lv;
public MyAdapter myAdap;
public ArrayList<Stock> lt;
public View v;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.lista, container, false);
lv= (ListView) v.findViewById(android.R.id.list);
if(lv==null){Log.e("","nel fragment la lista è nulla");}
if(lv!=null){Log.e("","nel fragment la lista NON è nulla");}
myAdap= new MyAdapter(lt, getActivity());
lv.setAdapter(myAdap);
return v;
}
public void prendiLista(ArrayList<Stock> al){
Log.e("", "passo per prendilitsa del fragment");
lt=al;
if(myAdap==null){Log.e("","nel prendilista del fragment myadapt è nullo");};
myAdap.notifyDataSetChanged();
}
public void addToFavorites(int pos){
Quotes.addToFavorites(pos);
}
#Override
public void onCreate(Bundle savedInstanceState){
lt=Quotes.dammi();
super.onCreate(savedInstanceState);
}
}
therefore the lists should update with the new contents of the ArrayLists.
Unfortunately no. When your app first runs l and best both reference the same data. You have this set up.
Fragment:
best ---\
|
|--- <the initial data>
Adapter: |
l ------/
But after you call best=al, you have:
Fragment:
best ------- <the new data>
Adapter:
l ---------- <the initial data>
So you never update the reference in your Adapter. Either add a new method to your custom Adapter to change l or use l itself since you didn't declare it as private:
public void prendiListaBest(ArrayList<Stock> al){
myAdap.l=al;
myAdap.notifyDataSetChanged();
}

ListFragments Wrong Data Weird Pager

I hope someone can assist please. I have a Fragment hosting multiple list fragments using support library. The list fragments are supposed to display data that i retrieve form an async task in the parent fragment. I have been trying to figure out exactly how the data is being loaded because it is not loading correctly.
Each time the list display fragment is launched it preforms an async task to get and parse Json into an ArrayList <ArrayList <HashMap <String, String> > >
Each List fragment queries the parent fragment for data at its position in the ArrayList.
eg. For the 3rd page in it should retrieve arrList[2] which contains an `ArrayList <HashMap <String, String> > to display as a list.
The pager is acting weird. Maybe i am not understanding the lifecycle of the fragments or how the pager uses them. I have 7 Fragments. If i start on frag3 the pager will show fragment 3 with no data on it. It also loads fragment 2 and 4 with no data. If i go left to frag 1 it will display fragment 1 correctly and load fragment 0. I can properly switch to frag 0 but if i switch to frag 2 it loads data from frag 0 and loads frag 0's data into all of the rest of the views. If i go back and forth enough it will replace all data in every fragment with data from frag 0. I believe that it does not load data immediately because it does not have the data when the viewpager launches. I have not made it wait for the async task yet.
I thought that each fragment gets its view redrawn each time it is taken far enough from view. So i put Update in the onCreateView() of the fragment. I feel like this is a small thing that i have just misplaced or i am overlooking it. I tried to implement FragmentStatePagerAdapter but i do not think that i did it right.
Any Help is much Appreciated And i am very open to discussion if i am just doing things horribly wrong. I usually do. It never fails. Create something to find out i need to rewrite everything.
public class ListFragmentDisplay extends SherlockFragment {
public static final String TAG = "listFragmentDisplay";
Calendar calendar = Calendar.getInstance();
private int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
// listbyday is a list of hash maps each list of hash maps represents a day
// of the week with items for that Object
private ArrayList<ArrayList<HashMap<String, String>>> listByDay = null;
private String objectName = null;
private ViewPager pager;
private FragAdapter adapter;
public ArrayList<HashMap<String, String>> getList(int day) {
return listByDay.get(day);
}
private void getObjectName() {
barName = ((MainFragActivity) getActivity()).getobjectSelected();
}
public static ListFragmentDisplay newInstance() {
return new ListFragmentDisplay();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the ListView layout file.
initArrList();
getObjectName();
fillList();
return inflater.inflate(R.layout.list_view, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
pager = (ViewPager) view.findViewById(R.id.viewPager);
adapter =new FragAdapter(getChildFragmentManager());
if (pager.getAdapter() == null)
pager.setAdapter(adapter);
reload();
pager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrollStateChanged(int arg0) {}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {reload();}
#Override
public void onPageSelected(int arg0) {
}
});
pager.setCurrentItem(dayOfWeek-1);
}
private void initArrList() {
if (listByDay == null) {
listByDay = new ArrayList<ArrayList<HashMap<String, String>>>();
} else {
listByDay.clear();
}
for (int i = 0; i < 7; i++) {
ArrayList<HashMap<String, String>> hm = new ArrayList<HashMap<String, String>>();
listByDay.add(hm);
}
}
synchronized private void fillList() {
LoadWebTask lWT = new LoadWebTask();
executeAsyncTask(lWT, getSherlockActivity().getApplicationContext());
}
FragmentPager
public class FragAdapter extends FragmentPagerAdapter {
private static final String[] CONTENT = new String[] { "frag0", "frag1",
"frag2", "frag3", "frag4", "frag5", "frag6" };
public FragAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
return MyListFragment.newInstance(arg0);
}
#Override
public int getCount() {
return CONTENT.length;
}
#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length];
}
}
ListFragment
public class MyListFragment extends SherlockListFragment {
public static final String NAME_TAG = "name";
public static final String DESCRIPTION_TAG = "description";
private static int dow;
public static final String TAG = "listFragment";
// Keys used in Hashmap that will be mapped to the rows
String[] dFrom = { NAME_TAG, DESCRIPTION_TAG };
private ArrayList<HashMap<String, String>> list;
int[] dTo = { R.id.name, R.id.description };
public void upDateList() {
//**************************Not really sure if this is how things are supposed
//** to be done. For my small data- set i feel like it will work but i would
//** be interested in knowing how else this might be done.
ListFragmentDisplay lFD = (ListFragmentDisplay) this
.getParentFragment();
dList = lFD.getList(dow);
}
public static MyListFragment newInstance(int pos) {
MyListFragment frag = new MyListFragment();
dow = pos;
return (frag);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
upDateList();
View results = inflater.inflate(R.layout.list_fragment, container,
false);
SimpleAdapter adapter = new SimpleAdapter(getParentFragment()
.getActivity(), list, R.layout.listrow, dFrom, dTo);
setListAdapter(adapter);
return results;
}
}
Edit. Solved Code: In List Fragment
The Initial Question has been solved. I am only in the process of implementing the onPostExecute callback to the ListFragmentDisplay. Much Thanks to Luksprog for solving my very noobish mistake. I made dow static without knowing its affect. I think it was actually something that Eclipse offered to solve a conflict. I should have read it closer.
public class MyListFragment extends SherlockListFragment {
public static final String NAME_TAG = "name";
public static final String DESCRIPTION_TAG = "description";
public static final String TAG = "listFragment";
// Keys used in Hashmap that will be mapped to the rows
String[] dFrom = { NAME_TAG, DESCRIPTION_TAG };
private ArrayList<HashMap<String, String>> list;
int[] dTo = { R.id.name, R.id.description };
SimpleAdapter adapter = null; **NEW**
public void upDateList() {
ListFragmentDisplay lFD = (ListFragmentDisplay) this
.getParentFragment();
dList = lFD.getList(getArguments().getInt(TAG)); **NEW**
if(adapter != null) **NEW**
adapter.notifyDataSetChanged(); **NEW**
}
public static MyListFragment newInstance(int pos) {
MyListFragment frag = new MyListFragment();
Bundle args = new Bundle(); **NEW**
args.putInt(TAG, pos); **NEW**
frag.setArguments(args); **NEW**
return (frag);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
upDateList();
View results = inflater.inflate(R.layout.list_fragment, container,
false);
adapter = new SimpleAdapter(getParentFragment()
.getActivity(), list, R.layout.listrow, dFrom, dTo);
setListAdapter(adapter);
return results;
}
}
Is there any reason why you made the dow variable from MyListFragment as static? With the static keyword your fragments from the ViewPager will share their position so you'll call the lFD.getList(dow); method with the wrong position most of the cases. Make dow a private instance field: private int dow;
About the rest of the code, it looks ok, see if the change above solves the problem. To update your data in the inner fragments you could follow this scenario:
start with an empty list of data in ListFragmentDisplay and start the task
initially, your inner ListFragmnents will see that the data list is empty so you'll initialize them with an empty list(the getList(int day) method should just return an empty list if there is no data in the listByDay field)
your task now finishes. Suppose you have a callback from the onPostExecute method of the AsyncTask. In that callback which the ListFragmentDisplay will implement you'll update every Fragment from the ViewPager which is either currently visible to the user or it's in the FragmentPagerAdapter alive(so each Fragment which is not null and its getView() method doesn't return null from the ViewPager will be updated). The other Fragments will self update because the onCreateView method will need to be called for them and you have the updateList call in there.
For the point above keep in mind that calling the updateList method will not update a visible Fragment because in that method you just update the list of the Fragment you don't call notifyDataSetChanged on the adapter to let it know that the data has changed.

Categories

Resources