where to call asynctask for listview in a tab layout containing many fragments

Course Queries Syllabus Queries 3 years ago

4.78K 1 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago


i am using material design tab layout. i have 5 tabs each a fragment containing a list view. i want to populate the each list with the json. i used asynctask to to so. question is where to call the asynctask? my code Main activity

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
private ProgressDialog pDialog;
private TabLayout tabLayout;
private ViewPager viewPager;
private static String url = "http://";


private static final String TAG_COLLEGEID = "collegeid";
private static final String TAG_COLLEGECODE = "collegecode";
private static final String TAG_COLLEGENAME = "college";
private static final String TAG_COLLEGEADDRESS = " ArrayList> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setNavigationContentDescription("MDU Rohtak");
   // contactList = new ArrayList>();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager=(ViewPager)findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    tabLayout=(TabLayout)findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(viewPager);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);



}
   private class getCollege extends AsyncTask {


    @Override
    protected void onPreExecute() {
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Void... params) {
        ServiceHandler sh = new ServiceHandler();

        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
        if (jsonStr !=null){
            try {
                JSONArray jArr = new JSONArray(jsonStr);
                for (int i=0;i contact = new HashMap();


                    contact.put(TAG_COLLEGEID, collegeid);
                    contact.put(TAG_COLLEGECODE, collegecode);
                    contact.put(TAG_COLLEGENAME,collegename);
                    contact.put(TAG_COLLEGEADDRESS,collegeaddress);
                    contactList.add(contact);
                }


            }catch (JSONException e){
                e.printStackTrace();
            }
        }
        return null;

    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        if (pDialog.isShowing())
            pDialog.dismiss();

        ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, contactList,
                R.layout.collegelistitem, new String[] { TAG_COLLEGEID, TAG_COLLEGECODE,
                TAG_COLLEGENAME,TAG_COLLEGEADDRESS }, new int[] { R.id.collegeid,
                R.id.collegecode, R.id.collegename,R.id.collegeaddress });

    }


}
public void setupViewPager(ViewPager viewPager){
    ViewPagerAdapter adapter=new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new Notice(),"Notice");
    adapter.addFrag(new Syllabus(),"Syllabus");
    adapter.addFrag(new CollegeList(),"CollegeList");
    adapter.addFrag(new Timetable(),"Timetable");
    adapter.addFrag(new ContactUs(),"ContactUs");
    viewPager.setAdapter(adapter);
}
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum