我正在尝试使用firebase数据库和firebase存储构建一个简单的博客应用。数据已添加到firebase中,但未在我的再循环视图中显示。
以下是我的firebase数据库的快照:
我尝试在 PostActivity.java 文件中的firebase数据库中发布数据:
package naim.google.com.photographyblog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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 com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
public class PostActivity extends AppCompatActivity {
private ImageButton mImageButton;
private EditText mPostTitle;
private EditText mPostDescription;
private Button mSubmitButton;
private Uri imageUri = null;
private StorageReference mStorage;
private DatabaseReference mDatabase;
private ProgressDialog mProgress;
private static final int GALLERY_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
mStorage = FirebaseStorage.getInstance().getReference();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
mImageButton = findViewById(R.id.imageSelect);
mPostTitle = findViewById(R.id.titileField);
mPostDescription = findViewById(R.id.descriptionField);
mSubmitButton = findViewById(R.id.submitButton);
mProgress = new ProgressDialog(this);
mImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent , GALLERY_REQUEST);
}
});
mSubmitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startPosting();
}
});
}
private void startPosting()
{
final String title_val = mPostTitle.getText().toString().trim();
final String description_val = mPostDescription.getText().toString().trim();
mProgress.setMessage("Uploading image...");
if(!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(description_val) && imageUri!= null)
{
mProgress.show();
StorageReference filePath = mStorage.child("Blog_Images").child(imageUri.getLastPathSegment());
filePath.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
final Uri downloadUri = taskSnapshot.getDownloadUrl();
final DatabaseReference newPost = mDatabase.push();
newPost.child("title").setValue(title_val);
newPost.child("description").setValue(description_val);
newPost.child("image").setValue(downloadUri.toString());
mProgress.dismiss();
startActivity(new Intent(PostActivity.this , MainActivity.class));
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK)
{
imageUri = data.getData();
mImageButton.setImageURI(imageUri);
}
}
}
我尝试将firebase数据库中的数据添加到 MainActivity.java 文件中的回收站视图中:
package naim.google.com.photographyblog;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
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 com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
private RecyclerView mBlogList;
private DatabaseReference mDatabase;
private DatabaseReference mDatabaseUsers;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mAuthListner = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() == null)
{
Intent intent = new Intent(MainActivity.this , LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
};
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users");
mDatabase.keepSynced(true);
mDatabaseUsers.keepSynced(true);
mBlogList = findViewById(R.id.blog_list);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(layoutManager);
checkUserExist();
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListner);
FirebaseRecyclerAdapter<Blog , BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.blog_row,
BlogViewHolder.class,
mDatabase
) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDescription(model.getDescription());
viewHolder.setImage(getApplicationContext() , model.getImage());
}
};
mBlogList.setAdapter(firebaseRecyclerAdapter);
}
private void checkUserExist()
{
if(mAuth.getCurrentUser()!=null)
{
final String user_id = mAuth.getCurrentUser().getUid();
mDatabaseUsers.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.hasChild(user_id)){
Intent intent = new Intent(MainActivity.this , SetupActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setDescription(String description){
TextView post_description = mView.findViewById(R.id.post_description);
post_description.setText(description);
}
public void setImage(Context ctx ,String image) {
ImageView post_image = mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).into(post_image);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_add)
{
startActivity(new Intent(MainActivity.this,PostActivity.class));
}
if(item.getItemId() == R.id.action_logout)
{
logout();
}
return super.onOptionsItemSelected(item);
}
private void logout()
{
mAuth.signOut();
}
}
运行应用程序并添加数据后,它不会显示任何内容,以下是快照:
答案 0 :(得分:3)
我已经解决了这个问题。问题出在我的 MainActivity.java 文件中。我刚从 MainActivity.java 文件中删除了以下行:
mBlogList.setHasFixedSize(true);
删除后,一切正常。