ListFragment不显示数据

时间:2016-04-23 13:56:08

标签: java android

我是Android开发的新手,无法弄清楚数据未在列表视图中显示的原因。它将它存储到Array列表中,因为当我检查它是否为null时,它返回false(意味着它有信息)任何帮助都会很棒!谢谢你提前!

主要活动

Village

未显示列表的片段

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

    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) {
                Toast.makeText(MainActivity.this, "Application Data Refreshed", Toast.LENGTH_LONG).show();
                GlimpseListFragment glimpse = new GlimpseListFragment();
                glimpse.makeList(s);
            }
        });
        task.execute(url);

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Goldleaf Comics!");

        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){
            GlimpseListFragment fragment = new GlimpseListFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            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 android.os.Bundle;
import android.support.v4.app.Fragment;
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.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kosalgeek.android.json.JsonConverter;

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

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

    public GlimpseListFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ArrayList<String> titles = new ArrayList<String>();
        for(UniverseListing value: universalListings){
            titles.add(value.universeId);
        }

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

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

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, glimpse);
        setListAdapter(adapter);
        setRetainInstance(true);

        return rootView;
    }

    public void onListItemClick(ListView l, View view, int position, long id){
        String item = ((TextView)view).getText().toString();
        Toast.makeText(getActivity(), glimpse.indexOf(id), Toast.LENGTH_SHORT).show();
    }

    public void makeList(String r){
        ArrayList<UniverseListing> universalListings = new JsonConverter<UniverseListing>().toArrayList(r, UniverseListing.class);
        if(universalListings == null){
            Log.w("Glimpse", "no information");
        }else{
            Log.w("Glimpse", "information stored");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我猜两个问题, 1. makelist调用是在没有附加到UI的片段的新实例上进行的。 2.一旦数据被修改,notifydatasetchagned就不会在makelist中调用。

您可以尝试以下更改: 1.在Activity中,使用相同的片段实例,而不是创建新的Fragment实例

index.php

不要创建新的,在提交片段后更好地放置此代码。

  1. 在makeList()函数中调用adapter.notifyDataSetChanged()。