i'm developing an app with viewpagerindicator and actionsherlock. I've implemented a tab screen but when i load data in a SherlockListFragment Async, fragments dissapear after few fragment changes.
I've four tabs A,B,C,D and when i load data async switching A to B is ok, but when i switch to C, A doesn't show fragment and when i switch to D no fragment is displayed any more.
No error is thrown.
My Fragment page adapter is like this:
class PaginationAdapter extends FragmentPagerAdapter {
public PaginationAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new CustomFragment();
}
#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase(Locale.getDefault());
}
#Override
public int getCount() {
return CONTENT.length;
}
}
This is the fragment that works:
public final class CustomFragment extends SherlockListFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new SimpleAdapter(getSherlockActivity(), getData(),
R.layout.descuentos_layout, new String[] { "title" },
new int[] { R.id.txtTitle }));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.custom_list_fragment, null);
return view;
}
protected List<Map<String, String>> getData() {
List<Map<String, String>> myData = new ArrayList<Map<String, String>>();
for (int i = 1; i < 20; i++){
Map<String, String> temp = new HashMap<String, String>();
temp.put("title", "Item " + i);
temp.put("ItemId", String.valueOf(i));
myData.add(temp);
}
return myData;
}
}
But this doesn't:
public final class DescuentosFragment extends SherlockListFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListViewLoaderTask llt = new ListViewLoaderTask();
llt.execute("");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.descuentos_list_fragment, null);
return view;
}
private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter> {
#Override
protected SimpleAdapter doInBackground(String... str) {
SimpleAdapter adapter = new SimpleAdapter(getSherlockActivity(), getData() , R.layout.descuentos_layout,new String[] { "title" },
new int[] { R.id.list_title });
return adapter;
}
#Override
protected void onPostExecute(SimpleAdapter adapter) {
ListView listView = getListView();
listView.setAdapter(adapter);
}
protected List<Map<String, String>> getData() {
List<Map<String, String>> myData = new ArrayList<Map<String, String>>();
for (int i = 1; i < 20; i++){
Map<String, String> temp = new HashMap<String, String>();
temp.put("title", "Item " + i);
temp.put("ItemId", String.valueOf(i));
myData.add(temp);
}
return myData;
}
}
}
What is wrong? thx
Related
I use FragmentPagerAdapter in my android project. I use too socket.io and I need to refresh a listview in fragment after received data by socket.
FragmentPagerAdapter
public class SectionsPagerAdapter extends FragmentPagerAdapter {
#StringRes
private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2};
private final Context mContext;
public SectionsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Collections collections = new Collections();
return collections;
case 1:
List list = new List();
return list;
default:
return null;
}
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return mContext.getResources().getString(TAB_TITLES[position]);
}
#Override
public int getCount() {
// Show 2 total pages.
return TAB_TITLES.length;
}
}
Below is my fragment code:
ListView Fragment
public class List extends Fragment {
JSONArray LIST_DATA = new JSONArray();
ListViewAdapter listAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list, container, false);
ListView listView = view.findViewById(R.id.listView);
FloatingActionButton addSong = view.findViewById(R.id.addButton);
GetterSetter getterSetter = new GetterSetter();
JSONObject jsonObject = getterSetter.getJsonObject();
try {
LIST_DATA = jsonObject.getJSONArray("list");
} catch (JSONException e) {
e.printStackTrace();
}
listAdapter = new ListViewAdapter(getActivity(), LIST_DATA);
listView.setAdapter(listAdapter);
addSong.setOnClickListener(new FloatingActionButton.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getActivity(), AddSong.class);
startActivity(intent);
}
});
return view;
}
and my socket.io
SocketIo receiver
private Ack createSong = new Ack() {
#Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
#Override
public void run() {
JSONArray GRID_DATA = new JSONArray();
try {
GRID_DATA = new JSONArray(""+args[1]);
} catch (JSONException e) {
e.printStackTrace();
}
GetterSetter getterSetter = new GetterSetter();
getterSetter.setJsonArray(GRID_DATA);
Log.d("name", String.valueOf(GRID_DATA));
finish();
}
});
}
};
How can I refresh my listview adapter after called finish() method?
i have a ViewPager that has a Fragment which contains a GridView into it
my code works fine only when i'm scrolling page with delay and each time retrieve's content's from internet again
all i want to do is just get each page content's once and use it when user scroll's a page
one of my issues is that JSON downloding simultaneously when several pages has been scrolled and i don't know how to store my gridview content's for next usage that a page called
is there a standard way to conquer this issue ?
here is PagerAdapter for my ViewPager :
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
String[] tabName;
public ViewPagerAdapter(FragmentManager fm, String[] tabArray) {
super(fm);
this.tabName = new String[tabArray.length];
this.tabName = tabArray;
}
#Override
public Fragment getItem(int position) {
TabPageFragment tabPageFragment = new TabPageFragment(position);
return tabPageFragment;
}
#Override
public int getCount() {
return tabName.length;
}
#Override
public CharSequence getPageTitle(int position) {
return tabName[position];
}
}
each page of ViewPager use's this class for populate Fragment :
public class TabPageFragment extends Fragment {
private GridView mgridview; // gridView inside Fragment
private int tabPosition;
private ArrayAdapterGridview arrayadapter; //adapter for my gridView
public TabPageFragment(int tabposition) {
this.tabPosition = tabposition;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mgridview = (GridView) rootView.findViewById(R.id.gridView2);
View rootView = inflater.inflate(R.layout.fragment_grid_view, container,false);
DownloadContentTab dct = new DownloadContentTab();
dct.execute();
return rootView;
}
// DownloadJSON AsyncTask
private class DownloadContentTab extends AsyncTask<Void, Void, Integer> {
#Override
protected Integer doInBackground(Void... params) {
JSONfunctions jsf = new JSONfunctions(); //this function retrieve's json for me
JSONArray jsonarray = jsf.getJSONfromURL(myUrl); // this url is differ for each page
String[] gridNameArray = new String[jsonarray.length()];
String[] gridPicArray = new String[jsonarray.length()];
for (int j = 0; j < jsonarray.length(); j++) {
try {
JSONObject jsonobject = jsonarray.getJSONObject(j);
gridNameArray[j] = jsonobject.getString("title");
gridPicArray[j] = jsonobject.getString("pic");
} catch (JSONException e) {
e.printStackTrace();
}
}
arrayadapter = new ArrayAdapterGridview(context, gridPicArray, gridNameArray);
return 0;
}
#Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
mgridview.setAdapter(arrayadapter);
}
}
}
Why does my pager adapter doesn't want to start from my first item. When I start ContainerIspiti activity, and show first fragment, the view pager dones't show the right element. Insted of first, view pager shows the second element, and I can't to swipe to first element of my array list. Does anybody have solution.
Here is my code for ConainterIspiti
public class ContainerIspiti extends FragmentActivity{
private Button next, previous, odgovori, informacije;
private TextView textPitanja, brojPitanja;
private ViewPager pager;
private PagerAdapter mPagerAdapter;
public static ArrayList<Pitanja> getAllPitanja;
public static ArrayList<Pitanje_has_Slika> getAllImages;
private Intent intent;
private DBTools db = new DBTools(this);
private ProgressDialog pDialog;
private int broj;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_container_pitanja);
Bundle bundle = new Bundle();
intent = getIntent();
Dohvati d = new Dohvati();
d.execute();
textPitanja = (TextView) findViewById(R.id.kategorijaTextViewPitanjeActivity);
brojPitanja = (TextView) findViewById(R.id.brojPitanjaTextViewPitanjeActivity);
pager = (ViewPager) findViewById(R.id.pagerPitanja);
pager.setCurrentItem(0, true);
bundle.putString("NAZIV_KATEGORIJE", intent.getStringExtra("NAZIV_KATEGORIJE"));
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Log.i("Position je sljedeci ", String.valueOf(position));
return PitanjaFragment.create(position);
}
#Override
public int getCount() {
Log.i("Velicina polja je ", String.valueOf(getAllPitanja.size()));
Log.i("Prvi eleemnt liste je", getAllPitanja.get(0).getTextPitanja());
return getAllPitanja.size();
}
}
private class Dohvati extends AsyncTask<String, String, String>{
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ContainerIspiti.this);
pDialog.setCancelable(false);
pDialog.setIndeterminate(false);
pDialog.show();
}
#Override
protected String doInBackground(String... params) {
getAllPitanja = db
.getAllPitanja(intent.getStringExtra("id_kategorije"));
Log.i("Ovoliki je get all pitanja", String.valueOf(getAllPitanja.size()));
getAllImages = db.getAllPitanjaImages();
Log.i("ovoliko je slika", String.valueOf(getAllImages.size()));
return null;
}
#Override
protected void onPostExecute(String result) {
pDialog.dismiss();
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
pager.setAdapter(mPagerAdapter);
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
#Override
public void onPageSelected(int position) {
invalidateOptionsMenu();
brojPitanja.setText(String.valueOf(position) + "/" + String.valueOf(getAllPitanja.size()));
broj = position;
}
});
textPitanja.setText(intent.getStringExtra("NAZIV_KATEGORIJE"));
brojPitanja.setText(String.valueOf(broj) + "/" + getAllPitanja.size());
}
}
}
and here is my fragment activity
public class PitanjaFragment extends Fragment implements API{
public static final String ARG_PAGE = "page";
private int broj;
private ImageView image;
private TextView textPitanja;
private String uri;
private View view;
private ListView listView;
private Typeface custom_font;
private boolean odgovoreno, tocno;
private ArrayList<Odgovor> odgovorList;
private PitanjaAdapter adapter;
private DBTools db;
private List<Integer> kliknuti;
private HashMap<Integer, List<Integer>> odgBrojPitanja;
public static PitanjaFragment create(int pageNumber) {
PitanjaFragment fragment = new PitanjaFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
public PitanjaFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
broj = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_pitanja, container, false);
image = (ImageView) view.findViewById(R.id.imageSlikaImageView);
image.setOnClickListener(this);
textPitanja = (TextView) view.findViewById(R.id.textPitanjaTextViewPitanjaActivity);
db = new DBTools(getActivity());
listView = (ListView) view.findViewById(R.id.listView);
updateDisplay((broj+1));
return view;
}
public void updateDisplay(int z) {
odgovoreno = false;
tocno = true;
textPitanja.setText(stripHtml(String.valueOf(ContainerIspiti.getAllPitanja.get(z).getTextPitanja())));
image.setImageBitmap(null);
//info = ContainerIspiti.getAllPitanja.get(z).getInfo();
Log.d("Postoji", ContainerIspiti.getAllImages.get(z).getNazivSlike() + ", ");
for (int j = 0; j < ContainerIspiti.getAllImages.size(); j++) {
if (ContainerIspiti.getAllImages.get(j).getIdPitanja() == ContainerIspiti.getAllPitanja.get(z)
.getIdPitanja()
&& ContainerIspiti.getAllImages.get(j).getNazivSlike() != null) {
Log.i("Id pitanja slike + idpitanja pitanja + idSlike",
ContainerIspiti.getAllImages.get(j).getIdPitanja()
+ ", "
+ String.valueOf(ContainerIspiti.getAllPitanja.get(z)
.getIdPitanja()
+ ", "
+ ContainerIspiti.getAllImages.get(j).getIdSlika()));
image.setVisibility(View.VISIBLE);
//povecalo.setImageResource(R.drawable.gumb_pretrazi);
//povecalo.setEnabled(true);
uri = PregledZnakova.PHOTOS_BASE_URL
+ ContainerIspiti.getAllImages.get(j).getNazivSlike();
int rounded_value = 40;
DisplayImageOptions options = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.placeholder).showStubImage(R.drawable.placeholder).cacheInMemory().cacheOnDisc().displayer(new RoundedBitmapDisplayer(rounded_value)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity().getApplicationContext()).defaultDisplayImageOptions(options).build();
ImageLoader.getInstance().init(config);
ImageLoader.getInstance().displayImage(uri, image,options);
break;
// }
} else {
image.setVisibility(View.GONE);
//povecalo.setImageResource(R.drawable.gumb_pretrazi_neaktivno);
//povecalo.setEnabled(false);
}
}
odgovorList = db.getAllOdgovore(Integer.toString(ContainerIspiti.getAllPitanja.get(z)
.getIdPitanja()));
adapter = new PitanjaAdapter(getActivity(),
R.layout.pitanja_entry, odgovorList);
listView.setAdapter(adapter);
for (int i=0;i<ContainerIspiti.getAllPitanja.size();i++){
if (ContainerIspiti.getAllPitanja.get(i).isTocno()){
Log.i("Ovo pitanje je tocno", ContainerIspiti.getAllPitanja.get(i).getTextPitanja());
}
}
}
#Override
public int getItemBroj() {
return broj;
}
#Override
public int getPosition() {
return broj;
}
#Override
public void setPosition(int position) {
this.broj = position;
}
#Override
public Fragment getFragment() {
return this;
}
}
Thanks for your time and your help.
In the onCreateView() method of your PitanjaFragment class, replace:
updateDisplay((broj+1));
with
updateDisplay(broj);
Try this. It should work.
I have created action bar and viewpager, I have three fragment. Each of the fragment I parsed json and can show each listview . My problem is : If I click second fragment and then I go back(click firs fragmet) when I go to the next fragment and then if I go to a front fragment at this situation Loading information from the server occurs twice, I use AsyncTask class to load info from server.
Below is a my code :
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new SendItemsFragment();
case 1:
return new RecivedItemsFragment();
case 2:
return new FavoriteItemsFragment();
}
return null;
}
#Override
public int getCount() {
return 3;
}
public class MainActivity extends FragmentActivity implements TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "test1", "test2", "test3" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setIcon(R.color.white);
actionBar.setDisplayShowTitleEnabled(true);
Drawable d = getResources().getDrawable(R.drawable.acttitle);
getActionBar().setBackgroundDrawable(d);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
viewPager = (ViewPager) findViewById(R.id.vp_main);
viewPager.setAdapter(mAdapter);
getActionBar().setCustomView(R.layout.menu_example);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.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) {
}
});
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// actionBar.setStackedBackgroundDrawable(getResources().getDrawable(
// R.drawable.background)); background viewpager
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
This is - one of the fragment's code. Similar to all three of them. The only difference is that the load of data from the server, And also use the AsyncTask class, each of which had a different.
public class SendItemsFragment extends Fragment {
private String URL = "*******************************************";
public static String KEY_title = "title";
public static String KEY_description = "description";
public static String KEY_image = "image";
public static String KEY_journal = "journal";
public static String KEY_JournalID = "JournalID";
public static String KEY_pubDate = "pubDate";
public static String KEY_statID = "statID";
public JSONArray jsonarray;
public ListView list;
public TransparentProgressDialog pd;
public JSONParser jsonparser;
static DealBoxAdapter adapter;
ProgressDialog pDialog, pDialog1;
static String fontPath2 = "font.ttf";
public static Typeface tf2;
ArrayList<HashMap<String, String>> itemList = new ArrayList<HashMap<String, String>>();
public ImageLoader imageLoader;
static final int DIALOG_ERROR_CONNECTION = 1;
private int screenSize;
private LoadDataAllChanelsToServer loader;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.send_items, container, false);
list = (ListView) rootView.findViewById(R.id.listView1);
pd = new TransparentProgressDialog(getActivity(), R.drawable.loader);
screenSize = getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK;
loader=new LoadDataAllChanelsToServer();
loader.execute();
return rootView;
}
private class LoadDataAllChanelsToServer extends
AsyncTask<String, Integer, String> {
#Override
protected void onPreExecute() {
pd.show();
}
#Override
protected String doInBackground(String... urls) {
jsonparser = new JSONParser();
JSONObject jsonobject = jsonparser.getJSONfromURL(URL);
try {
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("journal", jsonobject.getString(KEY_journal));
map.put("image", jsonobject.getString(KEY_image));
map.put("title", jsonobject.getString(KEY_title));
map.put("description",
jsonobject.getString(KEY_description));
map.put("JournalID", jsonobject.getString(KEY_JournalID));
map.put("pubDate", jsonobject.getString(KEY_pubDate));
map.put("statID", jsonobject.getString(KEY_statID));
itemList.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return itemList.toString();
}
#Override
protected void onPostExecute(String result) {
try {
if (pd != null) {
pd.dismiss();
}
} catch (Exception e) {
}
try {
adapter = new DealBoxAdapter(getActivity(), itemList,
screenSize);
list.setAdapter(adapter);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
}
that's all :)
if anyone knows solution please help me
thank you
You should cancel async task when your Fragment is destroyed.
private LoadDataAllChanelsToServer mLoader;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
mLoader = new LoadDataAllChanelsToServer();
mLoader.execute();
...
}
#Overide
public onDestroy() {
super.onDestroy();
if(mLoader != null) {
mLoader.cancel(true);
mLoader = null;
}
this way when fragment is destroyed AsyncTask will be canceled.
You can also set ViewPager.setOffscreenPageLimit(2). This way, since you have only 3 Fragments, no Fragments will be destroy and therefore your onViewCreated will only be called once.
I need to populate the listview from the loadListTask. You will see at the end of script, where it should happen. How to do that ?
I tried several methods, but always the same result, a crash.
Main activity here and the async task at the end :
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoadListTask myLoadListTask = new LoadListTask();
myLoadListTask.execute();
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new SectionFragment();
Bundle args = new Bundle();
args.putInt(SectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase();
case 1:
return getString(R.string.title_section2).toUpperCase();
case 2:
return getString(R.string.title_section3).toUpperCase();
}
return null;
}
}
public static class SectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public SectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayList<HashMap<String, String>> listItem;
HashMap<String, String> map;
SimpleAdapter mSchedule;
listItem = new ArrayList<HashMap<String, String>>();
map = new HashMap<String, String>();
ListView maListViewPerso = new ListView(getActivity());
mSchedule = new SimpleAdapter (getActivity(), listItem, R.layout.affichageitem,
new String[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description});
maListViewPerso.setAdapter(mSchedule);
switch (getArguments().getInt(
ARG_SECTION_NUMBER)) {
case 1:
map = new HashMap<String, String>();
map.put("titre", "Excel");
map.put("description", Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
map.put("img", String.valueOf(R.drawable.devoir));
listItem.add(map);
case 2:
//To do
case 3:
//To do
}
return maListViewPerso;
}
}
// I NEED TO POPULATE FROM HERE
private class LoadListTask extends AsyncTask<String, Void, Integer> {
protected void onPreExecute() {
}
protected Integer doInBackground(String... params) {
return 0;
}
protected void onPostExecute(Integer result) {
if (result == 0) {
}
}
}
}
You have to keep a reference to listItem and to maListViewPerso to use them in onPostExecute, you can do it by declaring them as members of your class.
After doing that,you have to create your adapter. (adapter) For simple data structures you can use one of the Android's pre-defined, such as ArrayAdapter or SimpleCursorAdapter but I suppose that what you want to do is to create a list which iterates through the ArrayList and then into the items of the map. To do so you have to implement your own adapter, extending BaseAdapter. Implementing getView(int pos) you have to return the item in the position pos and with getCount() the ListView would know how many items your list have.