简体   繁体   English

从Firebase Realtime数据库中获取值,并将其放入ArrayList中

[英]Grab values from the Firebase Realtime database and put them in an ArrayList

In my application, I am attempting to create a live feed (such as the Instagram feed, or the Facebook feed) consisting of posts that contain an Image, a caption, the username of the user, and the like count of the image. 在我的应用程序中,我试图创建一个实时供稿(例如Instagram供稿或Facebook供稿),该供稿由包含图像,标题,用户名和图像计数的帖子组成。 I have created a fragment that takes a picture, adds a caption, and converts it to a string from a Bitmap. 我创建了一个片段,用于拍摄图片,添加标题并将其转换为位图中的字符串。 I can successful post these Posts(object) to firebase database, but getting the values of each post from the database and adding them to an arraylist is not working CameraFragment(adds post values to database for each post) 我可以成功地将这些Posts(object)发布到Firebase数据库,但是从数据库获取每个Post的值并将它们添加到arraylist都不起作用CameraFragment(将每个Posts的值添加到数据库中)

 package com.example.a10012032.dreamapplicationv2.Main;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a10012032.dreamapplicationv2.R;
import com.example.a10012032.dreamapplicationv2.UserAuth.Login;
import com.example.a10012032.dreamapplicationv2.UserAuth.Profile;
import com.example.a10012032.dreamapplicationv2.UserAuth.signUp;
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 java.io.ByteArrayOutputStream;

import static android.app.Activity.RESULT_OK;


public class cameraFragment extends Fragment {
    private static final String Tab = "cameraFragment";
    private static final int CAMERA_REQUEST_CODE = 102;

    private Button take;
    private ImageView holder;
    private EditText editText;
    private TextView caption;
    private Button post;
    Bitmap bitmap;
    FirebaseAuth mAuth;
    DatabaseReference mDatabaseRef;
    DatabaseReference mUserData;
    String keyUser;
    String UsernameStr;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.camera_fragment, container, false);
        keyUser = signUp.USER_KEY;
        UsernameStr= Login.usernameValue;
        bitmap = null;
        mAuth = FirebaseAuth.getInstance();
        mDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Posts");
        mUserData = FirebaseDatabase.getInstance().getReference().child("Users").child(keyUser);
        take = view.findViewById(R.id.btnCapture);
        post = view.findViewById(R.id.post);
        holder = view.findViewById(R.id.imageView2);
        editText = view.findViewById(R.id.editText2);
        editText.setVisibility(View.INVISIBLE);
        caption = view.findViewById(R.id.cap);
        caption.setVisibility(View.INVISIBLE);
        post.setVisibility(View.INVISIBLE);


        take.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                takePictureFromCamera();
            }
        });
        post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String caption = editText.getText().toString();
                DatabaseReference mChildDatabase = mDatabaseRef.child("Posts").push();
                mChildDatabase.child("imageString").setValue(bitmapToString(bitmap)); //converts bitmap to string
                mChildDatabase.child("likeCount").setValue(0);
                Log.d("TAGUSERNAME", UsernameStr+" is null");
                mChildDatabase.child("Username").setValue(UsernameStr);
                mChildDatabase.child("Message").setValue(caption);
                holder.setImageResource(0);
                editText.setVisibility(View.INVISIBLE);
                post.setVisibility(View.INVISIBLE);
            }
        });


        return view;
    }
    public void takePictureFromCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_REQUEST_CODE);
    }
    public String bitmapToString(Bitmap bm){
        ByteArrayOutputStream bYtE = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, bYtE);
        bm.recycle();
        byte[] byteArray = bYtE.toByteArray();
        return Base64.encodeToString(byteArray, Base64.DEFAULT);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
            bitmap = (Bitmap) data.getExtras().get("data");
            holder.setImageBitmap(bitmap);
            editText.setVisibility(View.VISIBLE);
            caption.setVisibility(View.VISIBLE);
            post.setVisibility(View.VISIBLE);
            editText.setText("");
        }
    }
}

FeedFragment(trying to get each post and adding it to the ListView CustomAdapter) FeedFragment(尝试获取每个帖子并将其添加到ListView CustomAdapter)

package com.example.a10012032.dreamapplicationv2.Main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.a10012032.dreamapplicationv2.UserAuth.Login;
import com.example.a10012032.dreamapplicationv2.UserAuth.Profile;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.example.a10012032.dreamapplicationv2.R;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class feedFragment extends Fragment {
    private static final String Tab = "feedFragment";
    ListView list;
    DatabaseReference mDatabaseRef, mUserCheckData, mGodPlease;
    ArrayList<Post> array;
    CustomAdapter customAdapter;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.feed_fragment,container,false);
        mDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Posts").child("Posts");
        array=new ArrayList<>();
        Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.prof);
        array.add(new Post(bitmapToString(bm),"Caption","Username"));
        mDatabaseRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()){
                    mUserCheckData=mDatabaseRef.child(ds.getKey());
                    array.add(new Post(mUserCheckData.child("imageString").toString(),mUserCheckData.child("Message").toString(),mUserCheckData.child("Username").toString()));
                    Log.d("TAGPLS",mUserCheckData.child("Username").toString());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        customAdapter=new CustomAdapter(getActivity(),R.layout.item,array);
        list = view.findViewById(R.id.id_listView);
        list.setAdapter(customAdapter);
        customAdapter.notifyDataSetChanged();
        return view;
    }
   /* public ArrayList<Post> retrieve(){
        mDatabaseRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                fetchData(dataSnapshot);
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                fetchData(dataSnapshot);
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        return array;
    }
    private void fetchData(DataSnapshot dataSnapshot)
    {
        array.clear();
        for (DataSnapshot ds : dataSnapshot.getChildren())
        {
            Post newPost=ds.getValue(Post.class);
            array.add(newPost);
        }
    }
*/
    public class CustomAdapter extends ArrayAdapter<Post> {
        Context context;
        List<Post> list;

        public CustomAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<Post> objects) {
            super(context, resource, objects);
            this.context=context;
            list=objects;
        }

        @NonNull
        @Override
        public View getView(int i, @Nullable View convertView, @NonNull ViewGroup parent) {
            LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            View adapterView = layoutInflater.inflate(R.layout.item,null);

            TextView userN = adapterView.findViewById(R.id.profMainTxt);
            ImageView postI = adapterView.findViewById(R.id.imagePost);
            TextView capt = adapterView.findViewById(R.id.captionTxt);
            Log.d("TAGHI",array.get(i).getUsername()+" is null");
            userN.setText(array.get(i).getUsername());
            postI.setImageBitmap(stringToBitMap(array.get(i).getBitmapString()));
            capt.setText(array.get(i).getCaption());
            notifyDataSetChanged();
            return adapterView;

        }
    }
    public Bitmap stringToBitMap(String encodedString){
        try {
            byte [] encodeByte= Base64.decode(encodedString,Base64.DEFAULT);
            return BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
        } catch(Exception e) {
            e.getMessage();
            return null;
        }
    }
    public String bitmapToString(Bitmap bm){
        ByteArrayOutputStream bYtE = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, bYtE);
        bm.recycle();
        byte[] byteArray = bYtE.toByteArray();
        return Base64.encodeToString(byteArray, Base64.DEFAULT);
    }

}

All the commented out parts were attempts at fixing my error 所有注释掉的部分都是试图修复我的错误

Lastly, My firebase directory 最后,我的firebase目录 在此处输入图片说明

To solve this, just move the following lines of code: 要解决此问题,只需移动以下代码行:

public void onDataChange(DataSnapshot dataSnapshot) {
    for(DataSnapshot ds : dataSnapshot.getChildren()){
        mUserCheckData=mDatabaseRef.child(ds.getKey());
        array.add(new Post(mUserCheckData.child("imageString").toString(),mUserCheckData.child("Message").toString(),mUserCheckData.child("Username").toString()));
        Log.d("TAGPLS",mUserCheckData.child("imageString").toString());
    }
    customAdapter=new CustomAdapter(getActivity(),R.layout.item,array);
    list = view.findViewById(R.id.id_listView);
    list.setAdapter(customAdapter);
    customAdapter.notifyDataSetChanged();
}

inside onDataChange() method right after the for loop ends. 在for循环结束后立即在onDataChange()方法中。

For more information, please see my answer from this post . 有关更多信息,请参见我在这篇文章中的回答。

Edit: According to your edited post, please use the following code: 编辑:根据您编辑的帖子,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference postsRef = rootRef.child("Posts").child("Posts");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String message = ds.child("Message").getValue(String.class);
            Log.d("TAG", message);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
postsRef.addListenerForSingleValueEvent(valueEventListener);

It will print all the messages. 它将打印所有消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM