ListFragment不显示List中的数据

时间:2016-04-23 16:27:57

标签: java android android-listfragment

所以我之前问过这个问题,但是在我尝试打开导航抽屉并转到另一个标签然后再回到它之后它再次无法正常工作。它没有再次在列表中显示数据。先谢谢!

主要活动

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();
    }
}

1 个答案:

答案 0 :(得分:0)

您创建的活动实例不受Android生命周期的管理,因此您要使用的Arraylist为空,因为它从未在活动的onCreate方法中填充。

    MainActivity act = new MainActivity();

    universalListings = act.universalListings; // size = 0

除此之外,您还有一些异步代码可以获取数据,因此Fragment甚至可能无法获取FragmentTransaction完成时下载的列表。

您必须使用processFinish方法中的Fragment将Arraylist传递给适配器,或者将AsyncTask代码调用到Fragment内部并从{{1}内部填充适配器方法