0

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. 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)

 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)

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 enter image description here

Levi Moreira
  • 11,917
  • 4
  • 32
  • 46

1 Answers1

0

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 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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • could you clarify if i am correctly getting the data from the database correctly because the listview is now producing a bunch of links, not values. – akshay vaswani May 21 '18 at 15:35
  • Furthermore, the listview is reproducing the entirety of the list everytime i post and image, essentially copying and pasting the entirety of the ArrayList everytime. – akshay vaswani May 21 '18 at 15:36
  • Please see my updated answer. Does it log the imageString? – Alex Mamo May 21 '18 at 15:40
  • 05-21 15:42:56.971 5218-5218/-D/TAGPLS: https://dreamapplicationv2.firebaseio.com/Posts/Posts/-LD2MXklXzrJe8qvK0d8/imageString – akshay vaswani May 21 '18 at 15:43
  • Please see a more clear code to display the messages. Does it work? – Alex Mamo May 21 '18 at 15:48
  • 05-21 15:50:58.246 5465-5465/com.example.a10012032.dreamapplicationv2 D/TAG: herer here here hello hello hwerear – akshay vaswani May 21 '18 at 15:52
  • it produces some of the messages, randomly. Also, when i post and image in the app, the message that i posted with that new post does not appear – akshay vaswani May 21 '18 at 15:52
  • It displays the messages from the database. Isn't what you want? Are you sure you have the correct urls? – Alex Mamo May 21 '18 at 15:55
  • If you dont mind me asking, i have another quick question. as i upload these values into my array, i load my array into the listview; The issue is, everytime i create a new post, the entire arraylist copies itself, basically making multiple versions of the entire post list with the new one added each time. I asked this earlier, just clearing up my question. – akshay vaswani May 21 '18 at 18:53
  • First clear the adapter and then call `customAdapter.notifyDataSetChanged();`. – Alex Mamo May 21 '18 at 18:55