0

我完全是 android 的初学者,所以在我的情况下可能会有一些愚蠢。

我正在尝试制作 Instagram 克隆应用程序。但我被困在显示用户提要上。我从 Parse Server 获取我的数据然后它存储在两个 ArrayList 1 中存储所有位图和其他所有字符串。通过我的日志,我可以看到我的数据正在正确获取,但在活动的任何地方都没有显示。

我正在提供活动代码和所有必要的信息,任何帮助将不胜感激。

用户时间线类:

package com.example.avail.instagramclone;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.GetDataCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

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

public class userTimeLine extends AppCompatActivity {
    TextView userName;
    String currLoggedInUser;
    ArrayList<String> postsByUser;
    ArrayList<Bitmap> imagesByUser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_time_line);

        postsByUser = new ArrayList<String>();
        imagesByUser = new ArrayList<Bitmap>();
        String selectedUser = getIntent().getStringExtra("SelectedUser");
        Log.i("Selected User Is " ,selectedUser);
        userName = findViewById(R.id.username);
        userName.setText(selectedUser.toUpperCase());
        final RecyclerView recyclerView = findViewById(R.id.recView);
        recyclerView.setHasFixedSize(true);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        final RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(postsByUser,imagesByUser);
        recyclerView.setAdapter(recyclerViewAdapter);



        ParseQuery<ParseObject> query = ParseQuery.getQuery("Image");
        query.whereEqualTo("username",selectedUser);
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if (e==null){
                    Log.i("Loading Posts","True");
                    Log.i("No Of Posts", String.valueOf(objects.size()));
                    if (objects.size()>0){


                        for(ParseObject object : objects){
                            ParseFile file = (ParseFile) object.get("image");
                            file.getDataInBackground(new GetDataCallback() {
                                @Override
                                public void done(byte[] data, ParseException e) {
                                    if(e==null){
                                        Bitmap bitmapImage = BitmapFactory.decodeByteArray(data, 0, data.length);
                                        imagesByUser.add(bitmapImage);
                                        Log.i("Status","Bitmap Fetched!");
                                        Log.i("Content In Images", String.valueOf(imagesByUser.size()));
                                        recyclerViewAdapter.notifyDataSetChanged();

                                    }
                                    else{
                                        Log.i("info", e.getMessage());
                                    }

                                }
                            });

                            String status = (String) object.get("poststatus");


                            postsByUser.add(status);


                        }
                        Log.i("Content In Status", String.valueOf(postsByUser.size()));
                        Log.i("Content In Images", String.valueOf(imagesByUser.size()));












                    }else {
                        Toast.makeText(getApplicationContext(),"NO POSTS BY USER",Toast.LENGTH_SHORT).show();


                    }
                }else{
                    Toast.makeText(getApplicationContext(),"NETWORK ERROR!",Toast.LENGTH_SHORT).show();
                    Log.i("Network Error ",e.getMessage());


                }
            }
        });







    }
}

回收器视图的适配器类命名为: RecyclerViewAdapter :

**

package com.example.avail.instagramclone;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private ArrayList<String> postStatuses ;
    private ArrayList<Bitmap> postImages;
    ImageView postImage;
    TextView postStatus;
    public RecyclerViewAdapter(ArrayList<String> postStatuses, ArrayList<Bitmap> postImages){
        this.postStatuses = postStatuses;
        this.postImages = postImages;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View customViewForDisplayingFeed = inflater.inflate(R.layout.recycler_list_users_feed,parent,false);
        return new ViewHolder(customViewForDisplayingFeed);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String status = postStatuses.get(position);
        Bitmap image = postImages.get(position);
        postImage.setImageBitmap(image);
        postStatus.setText(status);
    }
    @Override
    public int getItemCount() {
        return postStatuses.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        public ViewHolder(View itemView) {
            super(itemView);
            postImage = itemView.findViewById(R.id.userFeedImageListItem);
            postStatus = itemView.findViewById(R.id.userFeedPostListItem);
        }
    }
}

**

RecyclerView 项目的布局文件命名为 recycler_list_users_feed.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/userFeedImageListItem"
        android:layout_width="match_parent"
        android:layout_height="261dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo" />

    <TextView
        android:id="@+id/userFeedPostListItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Here comes the post status"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/userFeedImageListItem" />
</LinearLayout>

另外,我的活动文件名为 activity_user_time_line.xml :

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".userTimeLine">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recView"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="15dp"
        android:layout_marginStart="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view4"
        app:layout_constraintStart_toEndOf="@+id/view4"
        app:layout_constraintTop_toBottomOf="@+id/username" />

    <View
        android:id="@+id/view4"
        style="@style/Divider"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        app:layout_constraintBottom_toTopOf="@+id/imageView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/usersList" />

    <View
        android:id="@+id/view3"
        style="@style/Divider"
        android:layout_width="wrap_content"
        android:layout_height="38dp"
        android:background="#f0f1f1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="12dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view3"
        app:srcCompat="@drawable/usericon" />

    <TextView
        android:id="@+id/TIMELINE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="TIMELINE"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="@+id/view3"
        app:layout_constraintEnd_toStartOf="@+id/view3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view3"
        app:layout_constraintTop_toTopOf="@+id/view3" />

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="USERNAME"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@+id/view4"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view4"
        app:layout_constraintTop_toBottomOf="@+id/imageView3" />

</android.support.constraint.ConstraintLayout>

最后但并非最不重要的是我的日志猫:)。注意:顺便说一句,我的应用程序没有崩溃。

09-15 03:35:31.559 21397-21397/com.example.avail.instagramclone I/Selected User Is: abuzar
09-15 03:35:31.583 21397-21726/com.example.avail.instagramclone I/QCNEA   : |NIMS|: getaddrinfo: hostname 18.222.87.194 servname NULL numeric 4 appname /system/bin/app_process
09-15 03:35:31.586 21397-21726/com.example.avail.instagramclone I/QCNEA   : |NIMS|: getaddrinfo: hostname 18.222.87.194 servname NULL numeric 4 appname /system/bin/app_process
09-15 03:35:32.065 21397-21397/com.example.avail.instagramclone I/Loading Posts: True
09-15 03:35:32.065 21397-21397/com.example.avail.instagramclone I/No Of Posts: 1
09-15 03:35:32.070 21397-21397/com.example.avail.instagramclone I/Content In Status: 1
09-15 03:35:32.070 21397-21397/com.example.avail.instagramclone I/Content In Images: 0
09-15 03:35:32.110 21397-21731/com.example.avail.instagramclone D/dalvikvm: GC_FOR_ALLOC freed 568K, 13% free 26178K/30084K, paused 14ms, total 14ms
09-15 03:35:32.130 21397-21397/com.example.avail.instagramclone D/dalvikvm: GC_FOR_ALLOC freed 1531K, 13% free 26212K/30084K, paused 14ms, total 14ms
09-15 03:35:32.177 21397-21397/com.example.avail.instagramclone I/dalvikvm-heap: Grow heap (frag case) to 43.627MB for 16777232-byte allocation
09-15 03:35:32.298 21397-21397/com.example.avail.instagramclone I/Status: Bitmap Fetched!
09-15 03:35:32.298 21397-21397/com.example.avail.instagramclone I/Content In Images: 1
4

4 回答 4

0

我认为你错的是postImagepostStatusAdapter.

postImage并且postStatus应该是ViewHolder类的属性。

ViewHolder您将在扩展' 布局后初始化它们。并给他们显示的价值onBindViewholder()

附注:保留一个ArrayListofBitmap可能不是一个好主意,因为它们会填满您的可用内存。

也许最好将 Bitmas 缓存在某个文件夹中并保留一个ArrayList路径。
然后在需要显示它们时从该文件夹中读取它们。

于 2018-09-14T23:11:36.873 回答
0

让我们尝试另一件事:

我们用这个改变你的 RecyclerViewAdapter

import java.util.ArrayList;
public class RecyclerViewAdapter extends 
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private ArrayList<String> postStatuses ;
private ArrayList<Bitmap> postImages;

public RecyclerViewAdapter(ArrayList<String> postStatuses, ArrayList<Bitmap> postImages){
    this.postStatuses = postStatuses;
    this.postImages = postImages;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View customViewForDisplayingFeed = inflater.inflate(R.layout.recycler_list_users_feed,parent,false);
    return new ViewHolder(customViewForDisplayingFeed);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    String status = postStatuses.get(position);
    Bitmap image = postImages.get(position);
    holder.postImage.setImageBitmap(image);
    holder.postStatus.setText(status);
}
@Override
public int getItemCount() {
    return postStatuses.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{

ImageView postImage; TextView postStatus public ViewHolder(View itemView) { super(itemView); postImage = itemView.findViewById(R.id.userFeedImageListItem); postStatus = itemView.findViewById(R.id.userFeedPostListItem); } } }

对不起,我在手机中编辑了这个

于 2018-09-14T23:19:51.627 回答
0

也许您的问题发生在适配器的 ViewHolder 中。您可以像这样交换您的适配器代码,然后尝试再次编译您的应用程序。

package com.example.avail.instagramclone;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class RecyclerViewAdapter extends 
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private ArrayList<String> postStatuses ;
private ArrayList<Bitmap> postImages;

public RecyclerViewAdapter(ArrayList<String> postStatuses, ArrayList<Bitmap> postImages){
    this.postStatuses = postStatuses;
    this.postImages = postImages;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View customViewForDisplayingFeed = inflater.inflate(R.layout.recycler_list_users_feed,parent,false);
    return new ViewHolder(customViewForDisplayingFeed);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    String status = postStatuses.get(position);
    Bitmap image = postImages.get(position);
    viewHolder.postImage.setImageBitmap(image);
    viewHolder.postStatus.setText(status);
}
@Override
public int getItemCount() {
    return postStatuses.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
    ImageView postImage;
    TextView postStatus;
    public ViewHolder(View itemView) {
        super(itemView);
        postImage = itemView.findViewById(R.id.userFeedImageListItem);
        postStatus = itemView.findViewById(R.id.userFeedPostListItem);
    }
}
}
于 2018-09-15T00:52:53.170 回答
0

我能够通过更改我的主要活动主页来解决我的问题

    <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"
    tools:context=".userTimeLine">

    <View
        android:id="@+id/view3"
        style="@style/Divider"
        android:layout_width="wrap_content"
        android:layout_height="38dp"
        android:background="#f0f1f1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view3"
        app:srcCompat="@drawable/usericon" />

    <TextView
        android:id="@+id/TIMELINE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="TIMELINE"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="@+id/view3"
        app:layout_constraintEnd_toStartOf="@+id/view3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view3"
        app:layout_constraintTop_toTopOf="@+id/view3" />

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="USERNAME"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView3" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="170dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/username" />

</android.support.constraint.ConstraintLayout>
于 2018-09-18T20:57:07.250 回答