Firebase RecyclerView: No Adapter Attached, Skippi

2019-01-28 12:31发布

I have an activity that hosts a TabLayout with 3 tabs. I am trying to load a recyclerview based on dynamic data in Firebase and the recyclerview simply is not populating any views. I am not sure if it has to do with the FragmentSectionPager adapter or what, but I am having no luck loading the actual RecyclerView.

Activity:

public class HomeActivity extends AppCompatActivity implements TrendingFragment.FragmentListener,FollowingFragment.OnFragmentInteractionListener, LiveFragment.OnFragmentInteractionListener {


private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private Toolbar toolbar;
private boolean isOpen;
private ProgressBar mProgressBar;
private TextView mProgressText;
private TabLayout mTabLayout;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    toolbar = (Toolbar) findViewById(R.id.action_tool_bar);
    toolbar.setTitleTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
    setSupportActionBar(toolbar);
    setTitle(R.string.app_name);
    Window window = getWindow();

    final android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    if(actionBar != null){
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
    }

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mProgressBar = (ProgressBar) findViewById(R.id.pbHeaderProgress);
    mProgressText = (TextView) findViewById(R.id.progress_text);
    mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
    mViewPager = (ViewPager) findViewById(R.id.tab_swipe_container);
    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            toolbar,  /* nav drawer icon to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description */
            R.string.drawer_close  /* "close drawer" description */
    ) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            isOpen = false;
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            isOpen = true;
        }
    };
    mDrawerLayout.addDrawerListener(mDrawerToggle);

    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    ArrayList<String> drawerTitleArray = new ArrayList<>();
    drawerTitleArray.add(0, "TEST");
    drawerTitleArray.add(1, "TEST 1");
    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, drawerTitleArray));


    mViewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));
    mTabLayout.setupWithViewPager(mViewPager);
    Bundle extras = getIntent().getExtras();
    int position = 0;
    if(extras != null) {
        position = extras.getInt("viewpager_position");
    }
    mViewPager.setCurrentItem(position);

}


@Override
public boolean onOptionsItemSelected(MenuItem item){
    return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}


@Override
public void onFragmentLoaded() {
    mProgressBar.setVisibility(View.INVISIBLE);
    mProgressText.setVisibility(View.INVISIBLE);
}

@Override
public void onFragmentInteraction(Uri uri) {

}

public class SectionPagerAdapter extends FragmentStatePagerAdapter {

    public SectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new TrendingFragment();
            case 1:
                return new FollowingFragment();
            case 2:
                return new LiveFragment();
            default:
                return new TrendingFragment();
        }
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return getResources().getString(R.string.trending_text);
            case 1:
                return getResources().getString(R.string.following_text);
            case 2:
                return getResources().getString(R.string.new_text);
            default:
                return getResources().getString(R.string.trending_text);
        }
       }
    }  
 }

Fragment Containing RecyclerView:

public class LiveFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

private RecyclerView mRecyclerview;
private DatabaseReference mBaseRef;
private DatabaseReference mPollsRef;
private LinearLayoutManager mLayoutManager;

private FirebaseRecyclerAdapter <Poll, PollHolder> mFireAdapter;


// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public LiveFragment() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment LiveFragment.
 */
// TODO: Rename and change types and number of parameters
public static LiveFragment newInstance(String param1, String param2) {
    LiveFragment fragment = new LiveFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBaseRef = FirebaseDatabase.getInstance().getReference();
    mPollsRef = mBaseRef.child("Polls");
    mFireAdapter = new FirebaseRecyclerAdapter<Poll, PollHolder>(Poll.class, R.layout.latest_item, PollHolder.class, mPollsRef) {
        @Override
        protected void populateViewHolder(PollHolder viewHolder, Poll model, int position) {
            viewHolder.mPollQuestion.setText(model.getQuestion());
            Picasso.with(getActivity().getApplicationContext())
                    .load(model.getImage_URL())
                    .fit()
                    .into(viewHolder.mPollImage);
            Log.v("QUESTION", model.getQuestion());
            Log.v("IMAGE", model.getImage_URL());
        }
    };
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);

    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View v = inflater.inflate(R.layout.fragment_new, container, false);
    Log.v("TAG", "ON CREATE CALLED FROM NEW");

    mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    mRecyclerview = (RecyclerView) v.findViewById(R.id.new_RecyclerView);

    if (mRecyclerview != null){
        mRecyclerview.setHasFixedSize(true);
    }

    mRecyclerview.setLayoutManager(mLayoutManager);
    Log.v("TAG", "ON START CALLED FROM NEW");
    mRecyclerview.setAdapter(mFireAdapter);
    return v;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onStart() {
    super.onStart();

}

@Override
public void onStop() {
    super.onStop();
    if (mFireAdapter != null){
        mFireAdapter.cleanup();
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}


public static class PollHolder extends RecyclerView.ViewHolder {

    TextView mPollQuestion;
    ImageView mPollImage;


    public PollHolder(View itemView) {
        super(itemView);

        mPollQuestion = (TextView) itemView.findViewById(R.id.latest_item_question);
        mPollImage = (ImageView) itemView.findViewById(R.id.pollThumbNailImage);

    }
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}
}

0条回答
登录 后发表回答