I just started learning android, and I was trying to learn swipe views with tab. I modified the code to sync data every time the tab is clicked, but it seems that my implementation does not invoke 'doInBackground' method on tab select. This is the changes that I made in existing sample:
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
Object[] object=new Object[2];
object[0]=rootView;
object[1]=this.getArguments().getInt(ARG_SECTION_NUMBER);
new AsyncRead().execute(object);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(
getString(R.string.dummy_section_text, args.getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
Async:
public static class AsyncRead extends AsyncTask<Object, Void, String[]> {
View view;
Integer section;
String[] headlines=new String[20];
@Override
protected String[] doInBackground(Object...object) {
this.view=(View)object[0];
this.section=(Integer)object[1];
for(int i=0; i<=19; i++){
headlines[i]="welcome "+section;
}
return headlines;
}
@Override
protected void onPostExecute(String[] result) {
System.out.println("Hello");
//TextView dummyTextView = (TextView) view.findViewById(R.id.section_label);
//dummyTextView.setText(result[1]);
ListView lv_headlines = (ListView) view.findViewById(R.id.action_bar);
ArrayAdapter<String> aa = new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_list_item_1, 0, headlines);
//lv_headlines.setAdapter(aa);
}
}
Looks like System.out.println("Hello"); is not invoked everytime the tab is selected and hence the view won't be refreshed.
What is the best approach to refresh(update) Listview in fragment on tab click?
No comments:
Post a Comment