ListFragment 不显示列表中的数据

ListFragment Not displaying data from List

在我尝试打开导航抽屉并转到另一个选项卡然后返回它之后。它不再在列表中显示数据。

主要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;

}

一瞥列表片段

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

您创建了一个不受 Android 生命周期管理的 Activity 实例,因此您要使用的 Arraylist 是空的,因为它从未在activity。

    MainActivity act = new MainActivity();

    universalListings = act.universalListings; // size = 0

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

您必须在您拥有的 processFinish 方法中将 Arraylist 从 Fragment 传递到适配器,或者将 AsyncTask 代码调用到 Fragment 内部并从 [=11= 内部填充适配器] 方法