'ListFragment Not displaying data from List

After I try to open the navigation drawer and go to a different tab and then go back to it. It is not showing data in a list once again.

Main Activity

package com.goldleaf.branden.goldleafcomics;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

import com.kosalgeek.android.json.JsonConverter;
import com.kosalgeek.genasync12.AsyncResponse;
import com.kosalgeek.genasync12.PostResponseAsyncTask;


import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
    ListView lvGlimpse;
    public List<UniverseListing>universalListings = new ArrayList<UniverseListing>();
    final GlimpseListFragment glimpse = new GlimpseListFragment();

    NavigationView navigationView;
    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String url = "http://goldleafcomics.com/application/UniverseGlimpse.JSON";
        PostResponseAsyncTask task = new PostResponseAsyncTask(MainActivity.this, new AsyncResponse() {
            @Override
            public void processFinish(String s) {
                universalListings = new JsonConverter<UniverseListing>().toArrayList(s, UniverseListing.class);
                Toast.makeText(MainActivity.this, "Application Data Refreshed", Toast.LENGTH_LONG).show();
            }
        });
        task.execute(url);

        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, glimpse);
        fragmentTransaction.commit();

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Glimpse");

        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) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_news) {
            getSupportActionBar().setTitle("G.C. News");
        } else if (id == R.id.nav_team) {
            TeamFeedFragment fragment = new TeamFeedFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            getSupportActionBar().setTitle("Team Feed");
            fragmentTransaction.commit();
        }else if(id == R.id.nav_notifications){
            getSupportActionBar().setTitle("Notifications");
        }else if(id == R.id.nav_comics){
            getSupportActionBar().setTitle("Comics");
        }else if(id == R.id.nav_glimpse){
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, glimpse);
            getSupportActionBar().setTitle("Glimpse");
            fragmentTransaction.commit();
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

UniverseListing

package com.goldleaf.branden.goldleafcomics;

import com.google.gson.annotations.SerializedName;

public class UniverseListing {
    @SerializedName("universe_id")
    public String universeId;

    @SerializedName("universe_glimpse")
    public String universeGlimpse;

    @SerializedName("universe_url")
    public String universeuRL;

}

Glimpse List Fragment

package com.goldleaf.branden.goldleafcomics;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kosalgeek.android.json.JsonConverter;
import com.kosalgeek.genasync12.*;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

public class GlimpseListFragment extends ListFragment {
    List<UniverseListing> universalListings = new ArrayList<UniverseListing>();
    List<String> glimpse = new ArrayList<String>();
    List<String> titles = new ArrayList<String>();

    public GlimpseListFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_glimpse_list, container, false);

        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MainActivity act = new MainActivity();

        universalListings = act.universalListings;

        ArrayList<String> glimpse = new ArrayList<String>();
        for(UniverseListing value: universalListings){
            glimpse.add(value.universeGlimpse);
        }

        ArrayList<String> titles = new ArrayList<String>();
        for(UniverseListing value: universalListings){
            titles.add(value.universeId);
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, titles);
        setListAdapter(adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        String value = (String)getListAdapter().getItem(position);
        Toast.makeText(getActivity(), value, Toast.LENGTH_LONG).show();
    }
}


Solution 1:[1]

You create an Activity instance that isn't managed by the Android life cycle, so the Arraylist that you want to use is empty because it was never populated in the onCreate method of the activity.

    MainActivity act = new MainActivity();

    universalListings = act.universalListings; // size = 0

Along with that, you have some asynchronous code to go fetch your data, so the Fragment may not even get the list that you download when the FragmentTransaction completes.

You'll have to pass the Arraylist to the adapter from the Fragment in the processFinish method you have, or call that AsyncTask code to inside the Fragment and populate the Adapter from inside the processFinish method

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1