Как отобразить изображение в режиме просмотра? - PullRequest
0 голосов
/ 25 марта 2020

Я разрабатываю приложение android с использованием java и Firebase. В этом я использую представление переработчика для отображения изображений и другого содержимого, которое хранится в базе данных Firebase. Когда я запускаю программу, другое содержимое отображается правильно, но изображение не отображается. Я впервые занимаюсь разработкой приложения android, поэтому я в замешательстве.

Любая помощь, которую вы можете оказать мне, чтобы заставить это работать, была бы великолепна.

Вот мой код,

Класс адаптера

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyMessageHolder> {

    private StorageReference storage;

    private Context context;
    private ArrayList<profile> Messages;

    public MessageAdapter(Context c , ArrayList<profile> p)
    {
        context = c;
        Messages =p;
    }



    @NonNull
    @Override
    public MyMessageHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyMessageHolder(LayoutInflater.from(context).inflate(R.layout.activity_card_view,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyMessageHolder holder, int position) {
        profile prof = Messages.get(position);
        holder.name.setText(Messages.get(position).getTitle());
        holder.description.setText(Messages.get(position).getDescription());
        holder.date.setText(Messages.get(position).getDate());
        //Picasso.get().load(Messages.get(position).getImage()).into(holder.image);
        Picasso.get()
                .load(prof.getImage())
                .fit()
                .centerCrop()
                .into(holder.image);
    }

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

    class  MyMessageHolder extends RecyclerView.ViewHolder
    {
        TextView name,description,date,time;
        ImageView image;
        public MyMessageHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.hTitle);
            description = itemView.findViewById(R.id.hDescription);
            date = itemView.findViewById(R.id.hDate);
            time = itemView.findViewById(R.id.hTime);
            image = itemView.findViewById(R.id.hImage);

        }
    }
}

Основной класс активности

 class MainActivityHomePage extends AppCompatActivity {
    private DatabaseReference reference;
    RecyclerView recyclerView;
    ArrayList<profile> list;
    MessageAdapter adapter;

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

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        list = new ArrayList<>();

        reference = FirebaseDatabase.getInstance().getReference().child("Messages");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
                    profile p = dataSnapshot1.getValue(profile.class);
                    list.add(p);
                }
                adapter = new MessageAdapter( MainActivityHomePage.this,list);
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(MainActivityHomePage.this, "something wrong", Toast.LENGTH_SHORT).show();
            }
        });

    }
}

Класс модели

package com.eNotification.getnotify;

public class profile {
    private String title;
    private String description;
    private String date;
    private String image;

    public profile() {
    }

    public profile(String title, String description, String date, String image) {
        this.title = title;
        this.description = description;
        this.date = date;
        this.image = image;
    }

    public String getTitle() {
        return title;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDate() {
        return date;
    }

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

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }
}

макет представления карты xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    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="170dp"
    app:cardBackgroundColor="@android:color/white"
    android:layout_marginTop="10dp"
    app:cardCornerRadius="8dp"
    app:cardElevation="2dp"
    app:cardPreventCornerOverlap="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/silver"
        android:padding="5dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="260dp"
            android:layout_height="match_parent"
            android:padding="5dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/hTitle"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_marginTop="8dp"
                android:background="@color/lightSilver"
                android:hint="Title"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/hTime" />

            <TextView
                android:id="@+id/hDescription"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginTop="8dp"
                android:background="@color/lightSilver"
                android:text="Description"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/hTitle" />

            <TextView
                android:id="@+id/hDate"
                android:layout_width="136dp"
                android:layout_height="30dp"
                android:layout_marginEnd="4dp"
                android:background="@color/lightSilver"
                android:text="Date"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toStartOf="@id/hTime"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/hTime"
                android:layout_width="116dp"
                android:layout_height="30dp"
                android:layout_marginStart="4dp"
                android:background="@color/lightSilver"
                android:text="Time"
                app:layout_constraintStart_toEndOf="@id/hDate"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <ImageView
            android:id="@+id/hImage"
            android:layout_width="160dp"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.834" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

Представление Recycler xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        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:padding="8dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </RelativeLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...