Recyclerview adapted from Firestore not appearing

2019-06-02 03:47发布

My Recyclerview (which is loaded from Firestore) works if I load it on the activity.

I just added a TabLayout and a ViewPager to my ConstraintLayout:

   <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        app:layout_constraintTop_toBottomOf="@id/storeIDLayout"
        >
        <android.support.design.widget.TabItem
            android:id="@+id/tabItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TEST 1" />
        <android.support.design.widget.TabItem
            android:id="@+id/tabItem2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test 2" />
    </android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/tabs"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

and moved out the RecyclerView to fragment.xml:

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ConstraintLayout"
>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_prodlist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    app:layout_constraintBottom_toBottomOf="@+id/ConstraintLayout"
    app:layout_constraintLeft_toLeftOf="@+id/ConstraintLayout"
    app:layout_constraintRight_toRightOf="@+id/ConstraintLayout"
    tools:listitem="@layout/item_addorders" />

</android.support.constraint.ConstraintLayout>

Question: In the activity, I have this:

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

    if (mAdapter != null) {
        mAdapter.startListening();
    }
}

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

Where do I insert this in my fragment?

2nd Question:

My fragment is appears empty. If I uncomment the Textview (R.id.section_label) - the textview works fine. But the recyclerview does not show up. Any idea why?

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }
}

public static class PlaceholderFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

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

        View rootView = inflater.inflate(R.layout.fragment_add_new_order, container, false);

        FirebaseFirestore mFirestore;
        Query mQuery;
        AddOrderAdapter mAdapter;

        RecyclerView mAddOrderRecycler = rootView.findViewById(R.id.recycler_prodlist);
        mAddOrderRecycler.setLayoutManager(new LinearLayoutManager(this.getActivity()));

        mFirestore = FirebaseFirestore.getInstance();

        mQuery = mFirestore
                .collection("PROD")
                .document("TEST")
                .collection("Items")
                .orderBy("Name", Query.Direction.ASCENDING);
        mAdapter = new AddOrderAdapter(mQuery, getActivity());

        mAddOrderRecycler.setAdapter(mAdapter);

        //TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        //textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));

        return rootView;
    }
}

Edited to include model:

import com.google.firebase.firestore.IgnoreExtraProperties;

@IgnoreExtraProperties
public class AddOrderProductList {
    private String Barcode;
    private String ImageURL;
    private String Name;
    private String Unit;

    public AddOrderProductList() {}

    public String getBarcode() {
        return Barcode;
    }

    public String getImageURL() {
        return ImageURL;
    }

    public String getName() {
        return Name;
    }

    public String getUnit() {
        return Unit;
    }

    public AddOrderProductList(String barcode, String imageURL, String name, String unit) {
        Barcode = barcode;
        ImageURL = imageURL;
        Name = name;
        Unit = unit;
    }
}

2nd Edit:

import com.google.firebase.firestore.IgnoreExtraProperties;

@IgnoreExtraProperties
public class AddOrderProductList {
    private String barcode;
    private String imageURL;
    private String name;
    private String unit;

    public AddOrderProductList() {}

    public String getBarcode() {
        return barcode;
    }

    public String getImageURL() {
        return imageURL;
    }

    public String getName() {
        return name;
    }

    public String getUnit() {
        return unit;
    }

    public AddOrderProductList(String barcode, String imageURL, String name, String unit) {
        this.barcode = barcode;
        this.imageURL = imageURL;
        this.name = name;
        this.unit = unit;
    }
}

Database Structure:

PROD -> TEST -> Items -> Document of items with fields:

"Barcode" - String
"Name" - String
"Unit" - String
"ImageURL" - String

1条回答
够拽才男人
2楼-- · 2019-06-02 04:21

The answer to the first question is very simple:

Where do I insert this in my fragment?

As you did it in your activity class and it worked, do it also in your fragment class. Override both onStart() and onStop() methods and start/stop listening for changes. Also, in the onStart() method, there is no need to check for nullity, just use mAdapter.startListening(); directly.

Regarding your second question, when you are commenting the view with the id section_label and it works, it most likely because your view is set to match_parent for both width and height, which in term means that it take all the total space of your view.

There is one more thing that you need to keep in mind regarding your model class. When the Firebase Realtime Database SDK deserializes objects coming from the database, is looking for fields that follow the principles of the JavaBeans and are named accordingly to Java Naming Conventions. So a field in your model class should be named barcode and not Barcode, with the corresponding getter getBarcode(). You model class should look like this:

public class AddOrderProductList {
    private String barcode;
    private String imageURL;
    private String name;
    private String unit;

    public AddOrderProductList() {}

    public AddOrderProductList(String barcode, String imageURL, String name, String unit) {
        this.barcode = barcode;
        this.imageURL = imageURL;
        this.name = name;
        this.unit = unit;
    }

    public String getBarcode() {return barcode;}
    public String getImageURL() {return imageURL;}
    public String getName() {return name;}
    public String getUnit() {return unit;}
}
查看更多
登录 后发表回答