从Firestore获取空值到RecyclerView。

huangapple go评论84阅读模式
英文:

getting null Value from firestore to recycleview

问题

NotificationActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notifications);

    getSupportActionBar().setTitle(R.string.menu_notifications);
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    initRecyclerView();
    getNotifications();
    Adview();
}

private void initRecyclerView() {
    Log.d(TAG, "initRecyclerView: 初始化 RecyclerView。");
    mRecyclerView = findViewById(R.id.recyclerv_view);
    if (mNotificationsAdapter == null) {
        mNotificationsAdapter = new NotificationsAdapter(this, mNotifications);
    }
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setAdapter(mNotificationsAdapter);
}

private void getNotifications() {
    SharedPreferences prefs = getSharedPreferences("TOKEN_PREF", MODE_PRIVATE);
    String token = prefs.getString("token", "");

    FirebaseFirestore db = FirebaseFirestore.getInstance();

    CollectionReference notificationsCollectionRef = db.collection("notifications").document(token).collection("messages");
    Query query = notificationsCollectionRef
            .orderBy("Date", Query.Direction.DESCENDING);
    query.get().addOnCompleteListener(task -> {

        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                Notifications notifications = document.toObject(Notifications.class);
                mNotifications.add(notifications);
            }
            mNotificationsAdapter.notifyDataSetChanged();
        } else {
            Log.d(TAG,"查询失败。请检查日志。");
        }
    });
}

NotificationsAdapter.java:

class NotificationsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "NotificationsAdapter";
    private ArrayList<Notifications> mNotifications;
    private Context mContext;

    public NotificationsAdapter(Context context, ArrayList<Notifications> notifications) {
        mNotifications = notifications;
        mContext = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.notifications_listitem, parent, false);

        holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        Log.d(TAG, "onBindViewHolder: 调用。");

        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).title.setText(mNotifications.get(position).getTitle());
            ((ViewHolder) holder).message.setText(mNotifications.get(position).getMessage());
            ((ViewHolder) holder).date_time.setText(mNotifications.get(position).getDate() + "\n" + mNotifications.get(position).getTime());
            ((ViewHolder) holder).parentLayout.setOnClickListener(view -> {
                Log.d(TAG, "点击事件:点击了 " + mNotifications.get(position).getTitle());

                Intent intent = new Intent(mContext, NotificationDetails.class);
                //  intent.putExtra("image_url", mImages.get(position));
                intent.putExtra("shop_name", mNotifications.get(position).getTitle());
                intent.putExtra("message", mNotifications.get(position).getMessage());
                mContext.startActivity(intent);
            });
        }
    }

    @Override
    public int getItemCount() {
        return mNotifications.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        CircleImageView image;
        TextView title, message, date_time;
        RelativeLayout parentLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.shop_image);
            title = itemView.findViewById(R.id.title);
            message = itemView.findViewById(R.id.message);
            date_time = itemView.findViewById(R.id.date_time);
            parentLayout = itemView.findViewById(R.id.parent_layout);
        }
    }
}

Notifications.java:

@IgnoreExtraProperties
public class Notifications {
    private String title;
    private String message;
    private String date;
    private String time;

    public Notifications() {
    }

    public Notifications(String title, String message, String date, String time) {
        this.title = title;
        this.message = message;
        this.date = date;
        this.time = time;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getDate() {
        return date;
    }

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

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}
英文:

I have made Notificatiion Activity that getting the data from Cloud Firestore. After I test the app. I got always null from database, I checked in the debug. I found the data is read from database, but not setting it in Notification.java. Here is my code:

NotificationActivity.java:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notifications);

        getSupportActionBar().setTitle(R.string.menu_notifications);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        initRecyclerView();
        getNotifications();
        Adview();
    }

    private void initRecyclerView() {
        Log.d(TAG, &quot;initRecyclerView: init recyclerview.&quot;);
        mRecyclerView = findViewById(R.id.recyclerv_view);
        if (mNotificationsAdapter == null) {
            mNotificationsAdapter = new NotificationsAdapter(this, mNotifications);
        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mNotificationsAdapter);
    }

    private void getNotifications() {
        SharedPreferences prefs = getSharedPreferences(&quot;TOKEN_PREF&quot;, MODE_PRIVATE);
        String token = prefs.getString(&quot;token&quot;, &quot;&quot;);

        FirebaseFirestore db = FirebaseFirestore.getInstance();

        CollectionReference notificationsCollectionRef = db.collection(&quot;notifications&quot;).document(token).collection(&quot;messages&quot;);
        Query query = notificationsCollectionRef
                .orderBy(&quot;Date&quot;, Query.Direction.DESCENDING);
        query.get().addOnCompleteListener(task -&gt; {

            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    Notifications notifications = document.toObject(Notifications.class);
                    mNotifications.add(notifications);
                }
                mNotificationsAdapter.notifyDataSetChanged();
            } else {
                Log.d(TAG,&quot;Query Failed. Check Logs.&quot;);
            }
        });
    }

NotificationsAdapter.java:

class NotificationsAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; {

    private static final String TAG = &quot;NotificationsAdapter&quot;;
    private ArrayList&lt;Notifications&gt; mNotifications;
    private Context mContext;

    public NotificationsAdapter(Context context, ArrayList&lt;Notifications&gt; notifications) {
        mNotifications = notifications;
        mContext = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.notifications_listitem, parent, false);

        holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        Log.d(TAG, &quot;onBindViewHolder: called.&quot;);

        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).title.setText(mNotifications.get(position).getTitle());
            ((ViewHolder) holder).message.setText(mNotifications.get(position).getMessage());
            ((ViewHolder) holder).date_time.setText(mNotifications.get(position).getDate() + &quot;\n&quot; + mNotifications.get(position).getTime());
            ((ViewHolder) holder).parentLayout.setOnClickListener(view -&gt; {
                Log.d(TAG, &quot;onClick: clicked on: &quot; + mNotifications.get(position).getTitle());

                Intent intent = new Intent(mContext, NotificationDetails.class);
                //  intent.putExtra(&quot;image_url&quot;, mImages.get(position));
                intent.putExtra(&quot;shop_name&quot;, mNotifications.get(position).getTitle());
                intent.putExtra(&quot;message&quot;, mNotifications.get(position).getMessage());
                mContext.startActivity(intent);
            });
        }
    }

    @Override
    public int getItemCount() {
        return mNotifications.size();
    }

       public class ViewHolder extends RecyclerView.ViewHolder {

        CircleImageView image;
        TextView title, message, date_time;
        RelativeLayout parentLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.shop_image);
            title = itemView.findViewById(R.id.title);
            message = itemView.findViewById(R.id.message);
            date_time = itemView.findViewById(R.id.date_time);
            parentLayout = itemView.findViewById(R.id.parent_layout);
        }
    }

Notifications.java

@IgnoreExtraProperties
public class Notifications {
    private String title;
    private String message;
    private String date;
    private String time;

    public Notifications() {
    }

    public Notifications(String title, String message, String date, String time) {
        this.title = title;
        this.message = message;
        this.date = date;
        this.time = time;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getDate() {
        return date;
    }

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

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

答案1

得分: 1

请确保您的Firestore / Firebase 属性与您的 Java/Kotlin 数据类属性相同。我的意思是大写字母和其他所有内容。

英文:

Make sure your Firestore / firebase attributes and you java/Kotlin data class attributes are the same. I mean caps letters and everything.

答案2

得分: 0

调用getNotification()方法首先。

英文:

Call the getNotification() method first.

答案3

得分: 0

我通过将模型类中的变量改为以大写字母开头来解决了这个问题。

英文:

I solved the issue by changing the variables in the model class to be starting with capital letter.

huangapple
  • 本文由 发表于 2020年7月26日 01:48:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63091604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定