我完全是 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