只有图像而不是文本被检索到片段中的recyclerview

时间:2018-05-12 19:30:52

标签: android android-fragments firebase-realtime-database android-recyclerview

你好,我有一个非常悸动的问题,它已经吃了好几天了。我是Android App Dev't的新手,所以请保持温和。我有一个带有片段的Android应用程序。我已经能够实现recyclelerview,但是在从firebase检索数据的过程中,只显示图像而不显示文本。我只是不明白。我到处研究但无济于事。以下是我的代码;

数据模型

public class News {

public String news_topic_list;
public String news_body_list;
public String news_thumb_image;

public News(){

}

public News(String news_topic_list, String news_body_list, String news_thumb_image) {
    this.news_topic_list = news_topic_list;
    this.news_body_list = news_body_list;
    this.news_thumb_image = news_thumb_image;
}

public String getNews_topic_list() {
    return news_topic_list;
}

public void setNews_topic_list(String news_topic_list) {
    this.news_topic_list = news_topic_list;
}

public String getNews_body_list() {
    return news_body_list;
}

public void setNews_body_list(String news_body_list) {
    this.news_body_list = news_body_list;
}

public String getNews_thumb_image() {
    return news_thumb_image;
}

public void setNews_thumb_image(String news_thumb_image) {
    this.news_thumb_image = news_thumb_image;
}
}

我的RecylcerView适配器类

package com.dreamlazerstudios.gtuconline;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
* Created by Gabriel Hagan on 12/05/2018 at 16:36.
*/
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

Context context;
List<News> MainImageUploadInfoList;

public RecyclerViewAdapter(Context context, List<News> TempList) {
    this.MainImageUploadInfoList = TempList;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.news_list_layout,
parent, false);
    ViewHolder viewHolder = new ViewHolder(view);

    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    News news = MainImageUploadInfoList.get(position);
    holder.news_topic.setText(news.getNews_topic_list());
    holder.news_body.setText(news.getNews_body_list());
Picasso.with(holder.news_image.getContext()).load(news.getNews_thumb_image()).into(holder.news_image);

}

@Override
public int getItemCount() {

    return MainImageUploadInfoList.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

    public TextView news_topic;
    public TextView news_body;
    public CircleImageView news_image;

    public ViewHolder(View itemView) {

        super(itemView);

        news_topic = itemView.findViewById(R.id.news_topic_list);
        news_body = itemView.findViewById(R.id.news_body_list);
        news_image = itemView.findViewById(R.id.news_image_list);
    }
}
}

My Fragment Class

package com.dreamlazerstudios.gtuconline;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

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

public class Home extends Fragment {

DatabaseReference databaseReference;
ProgressDialog progressDialog;
List<News> list = new ArrayList<>();
RecyclerView recyclerView;
RecyclerView.Adapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

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

}

@Override
public void onViewCreated(final View view, @Nullable Bundle    savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    recyclerView = view.findViewById(R.id.news_list);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Loading Data...");
    progressDialog.show();

    databaseReference = FirebaseDatabase.getInstance().getReference().child("News");
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                News news = dataSnapshot.getValue(News.class);
                list.add(news);
            }

            adapter = new RecyclerViewAdapter(getActivity(), list);
            recyclerView.setAdapter(adapter);
            progressDialog.dismiss();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

            progressDialog.dismiss();

        }
    });

}
}

这是片段的布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:background="#fff"
tools:context=".Home">

<android.support.v7.widget.CardView
   android:layout_margin="5dp"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical">

       <TextView
           android:layout_marginStart="10dp"
           android:paddingStart="2dp"
           android:paddingEnd="2dp"
           android:textColor="@color/colorPrimary"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="20sp"
           android:text="News"/>

       <TextView
           android:layout_marginStart="10dp"
           android:id="@+id/time_home"
           android:paddingEnd="2dp"
           android:paddingStart="2dp"
           android:textColor="@color/darkgrey"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="15sp"
           android:text="Time"/>

       <android.support.v7.widget.RecyclerView
           android:layout_marginTop="5dp"
           android:layout_marginStart="10dp"
           android:paddingEnd="2dp"
           android:paddingStart="2dp"
           android:id="@+id/news_list"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent">

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

   </LinearLayout>

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

   </FrameLayout>

我的应用程序的gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
defaultConfig {
    applicationId "com.dreamlazerstudios.gtuconline"
    minSdkVersion 18
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.firebase:firebase-auth:11.0.4'
implementation 'com.google.firebase:firebase-database:11.0.4'
implementation 'com.google.firebase:firebase-storage:11.0.4'
implementation 'com.google.firebase:firebase-messaging:11.0.4'
implementation 'com.google.firebase:firebase-core:11.0.4'
testImplementation 'junit:junit:4.12'
compile 'com.theartofdev.edmodo:android-image-cropper:2.4.7'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'id.zelory:compressor:2.0.0'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.android.support:cardview-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
compile 'com.android.support:support-vector-drawable:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
compile 'com.android.support:support-v4:26.1.0'}










apply plugin: 'com.google.gms.google-services'

我的news_list_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_margin="2dp"
    android:background="@color/lightgrey" />

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:weightSum="100">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="70"
        android:layout_gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/news_topic_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:paddingEnd="2dp"
            android:paddingStart="2dp"
            android:text="News Topic"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="17sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/news_body_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:paddingEnd="2dp"
            android:paddingStart="2dp"
            android:text="News Body"
            android:textColor="@color/darkgrey" />

     </LinearLayout>

     <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="30">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/news_image_list"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_gravity="center"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:src="@drawable/student_icon_17870" />

    </LinearLayout>

   </LinearLayout>


 </LinearLayout>

我的FirebaseDatabase快照 Look Here

0 个答案:

没有答案