简体   繁体   English

Recycler View 的项目未显示

[英]Items of Recycler View is not showing

The items of my recycler view is not showing.我的回收站视图中的项目未显示。 The items are retrieved from Firebase.从 Firebase 检索项目。 There are no any error message or warning.没有任何错误消息或警告。

I did search relevant problems but I could not found any solution for mine.我确实搜索了相关问题,但我找不到任何适合我的解决方案。 Hope someone able to correct my problem, thank you.希望有人能纠正我的问题,谢谢。

Here are the codes:以下是代码:

Layout of the recycler view data:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="5dp"
android:elevation="5dp"
android:layout_margin="5dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_margin="5dp">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="29 November 2020"
            android:textColor="@color/date_color"
            android:textAppearance="?android:textAppearanceSmall"
            android:textStyle="bold"
            android:id="@+id/date_txt_income"
            />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Type"
            android:maxLines="1"
            android:textColor="@android:color/black"
            android:textAppearance="?android:textAppearanceSmall"
            android:id="@+id/type_txt_income"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Note"
            android:maxLines="1"
            android:textColor="@android:color/black"
            android:textAppearance="?android:textAppearanceSmall"
            android:layout_below="@+id/type_txt_income"
            android:id="@+id/note_txt_income"
            />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2000"
            android:textColor="@color/date_color"
            android:textAppearance="?android:textAppearanceSmall"
            android:textStyle="bold"
            android:id="@+id/amount_txt_income"
            />

    </RelativeLayout>

</LinearLayout>

</android.support.v7.widget.CardView>


Layout that shows the recycler view items:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#FFF1F3FC"
tools:context=".IncomeFragment">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="5dp"
    android:elevation="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_margin="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:textAppearanceMedium"
                android:textColor="@android:color/black"
                android:text="Income"
                />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="center"
            android:layout_margin="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/income_txt_result"
                android:textAppearance="?android:textAppearanceMedium"
                android:textColor="@android:color/black"
                android:text="0.00"
                />

        </RelativeLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_id_income">



</android.support.v7.widget.RecyclerView>
The java file:

public class IncomeFragment extends Fragment {

//Firebase
private FirebaseAuth mAuth;
private DatabaseReference mIncomeDatabase;

//RecyclerView
private RecyclerView recyclerView;
private FirebaseRecyclerAdapter adapter;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View myview = inflater.inflate(R.layout.fragment_income, container, false);

    mAuth = FirebaseAuth.getInstance();

    FirebaseUser mUser = mAuth.getCurrentUser();
    String uid = mUser.getUid();

    mIncomeDatabase = FirebaseDatabase.getInstance().getReference().child("IncomeData").child(uid);

    recyclerView = myview.findViewById(R.id.recycler_id_income);

    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());

    layoutManager.setReverseLayout(true);
    layoutManager.setStackFromEnd(true);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(layoutManager);
    return myview;
}

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

    FirebaseRecyclerOptions<Data> options = new FirebaseRecyclerOptions.Builder<Data>()
            .setQuery(mIncomeDatabase, Data.class)
            .build();

    adapter = new FirebaseRecyclerAdapter<Data, MyViewHolder>(options) {

        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.income_recycler_data, parent, false));
        }

        protected void onBindViewHolder(MyViewHolder holder, int position, @NonNull Data model) {
            holder.setAmount(model.getAmount());
            holder.setType(model.getType());
            holder.setNote(model.getNote());
            holder.setDate(model.getDate());
        }
    };

    recyclerView.setAdapter(adapter);
}
}


class MyViewHolder extends RecyclerView.ViewHolder {
View mView;

public MyViewHolder(@NonNull View itemView) {
    super(itemView);
    mView = itemView;
}

void setType(String type) {
    TextView mType = mView.findViewById(R.id.type_txt_income);
    mType.setText(type);
}

void setNote(String note) {

    TextView mNote = mView.findViewById(R.id.note_txt_income);
    mNote.setText(note);
}

void setDate(String date) {
    TextView mDate = mView.findViewById(R.id.date_txt_income);
    mDate.setText(date);
}

void setAmount(double amount) {
    TextView mAmount = mView.findViewById(R.id.amount_txt_income);
    String strAmount = String.valueOf(amount);
    mAmount.setText(strAmount);
}

}

Data.java:

public class Data {

private double amount;
private String type;
private String note;
private String id;

public Data(double amount, String type, String note, String id, String date) {
    this.amount = amount;
    this.type = type;
    this.note = note;
    this.id = id;
    this.date = date;
}

public double getAmount() {
    return amount;
}

public void setAmount(double amount) {
    this.amount = amount;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getNote() {
    return note;
}

public void setNote(String note) {
    this.note = note;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

private String date;

public Data(){

}

} }

Sorry for inappropriate format.抱歉格式不合适。

@Ryx you are missing the call to methods adapter.startListening(); @Ryx 您缺少对方法的调用adapter.startListening(); and adapter.stopListening();adapter.stopListening(); . . I think that's why you are not getting the result.我认为这就是为什么你没有得到结果。 You can also look at the documentation In case you are still facing the issue then first create your own adapter and the try to run your code end to end and then use the Firebase-ui您还可以查看文档如果您仍然遇到问题,请首先创建自己的适配器并尝试端到端运行您的代码,然后使用 Firebase-ui

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM