Issue getting data based on item click from Firebase database.

  • Replies:0
Karambit
  • Forum posts: 1

Nov 12, 2018, 1:40:53 PM via Website

Hi, can anybody help im trying to get data related with items clicked in the recycler view.
In the class ArtistsActivity.java when an artists is clicked on its suppose to get the data and pass it to the next activity ArtistsBiography.java. Below is the database image from firebase. How do I get the 'stageName' value associated with the respective artist and set it in ArtistsBiography.java?

Capture2

// Artists class model

public class ArtistDetails {

public String image,name,stageName; // variables must match the same format as in firebase DB.

public ArtistDetails(){}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getStageName() {
    return stageName;
}

public void setStageName(String stageName) {
    this.stageName = stageName;
}

}

// Artists Activity

public class ArtistsActivity extends AppCompatActivity {

RecyclerView mRecyclerview;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
String stage;
int itemPosition;
FirebaseRecyclerAdapter<ArtistDetails,ViewHolder> firebaseRecyclerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_artists);

    //toolbar support to set our custom toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_id);
    toolbar.setTitle("Artists");
    setSupportActionBar(toolbar);

    // mRecyclerView
    mRecyclerview = findViewById(R.id.recyclerView_main);
    mRecyclerview.setHasFixedSize(true);

    // set recycler view layout with two column grid
    mRecyclerview.setLayoutManager(new GridLayoutManager(this,2));

    //query firebase DB for instance
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    // use the instance to query the  Artists table
    mRef = mFirebaseDatabase.getReference("Artist");
}
//load data to recylcer view on start to the artists section
@Override
protected void onStart() {
    super.onStart();
     firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<ArtistDetails, ViewHolder>(
                    ArtistDetails.class,
                    R.layout.cardview_row,
                    ViewHolder.class,
                    mRef) {
                @Override
                protected void populateViewHolder(ViewHolder viewHolder, ArtistDetails artists, int position) {

                    viewHolder.setArtistDetails(getApplicationContext(),artists.getImage(),artists.getName());
                }

                @Override
                public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                   ViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);

                    viewHolder.setOnClickListener(new ViewHolder.ClickListener() {
                        @Override
                        public void onitemClick( View view,  int position) {

                            Toast.makeText(ArtistsActivity.this,"" + position,Toast.LENGTH_SHORT).show();
                            Intent biography = new Intent(view.getContext(), ArtistsBiography.class);

                            // set image view and text view from cardview_row.xml in activity_artists.xml recycler view
                            ImageView mimageViewartist = view.findViewById(R.id.artistsimage_id);
                            TextView mnameViewartist = view.findViewById(R.id.artistsname_id);

                            //get Data from image and text views
                            String mName = mnameViewartist.getText().toString();
                            Drawable mDrawable = mimageViewartist.getDrawable();
                            Bitmap mbitmap = ((BitmapDrawable) mDrawable).getBitmap();

                            // pass image and name data to ArtistsBiography activity
                            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                            mbitmap.compress(Bitmap.CompressFormat.PNG,100,outStream);
                            byte[] bytes = outStream.toByteArray();
                            biography.putExtra("image",bytes);
                            biography.putExtra("name",mName);
                            biography.putExtra("ArtistID",firebaseRecyclerAdapter.getRef(position).getKey());
                            startActivity(biography);
                        }
                    });
                    return viewHolder;
                }
            };
    // set adapter to recycler view to view data
    mRecyclerview.setAdapter(firebaseRecyclerAdapter);
}

}

// ArtistsBiography class

public class ArtistsBiography extends AppCompatActivity {

TextView mName;
ImageView mImageView;
TextView mStageName;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
String stage;
ArtistsActivity ac;
String itemPosition;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_artists_biography);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_id);
    toolbar.setTitle("Biography");
    setSupportActionBar(toolbar);

    mName = findViewById(R.id.name_bio);
    mImageView = findViewById(R.id.circleBio_image);
    mStageName = findViewById(R.id.stage_name_bio);

    // get intent data from Artists Activity
    byte[] bytes = getIntent().getByteArrayExtra("image");
    String name = getIntent().getStringExtra("name");
    itemPosition = getIntent().getStringExtra("ArtistID");
    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

    //query firebase DB for instance
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    // use the instance to query the  Artists table
    mRef = mFirebaseDatabase.getReference("Biography");

    //set data to views
    mName.setText(name);
    mImageView.setImageBitmap(bmp);

    // think the issue lies here
    mRef.orderByChild("bioID").equalTo(itemPosition).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                stage = snapshot.child("stageName").getValue().toString();
                mStageName.setText(stage);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

}

// ViewHolder class

  public class ViewHolder extends RecyclerView.ViewHolder{

    View mView;
    private ViewHolder.ClickListener mClickListener;

    public ViewHolder(View itemView) {
    super(itemView);

    mView = itemView;

    //item click
    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mClickListener.onitemClick(view,getAdapterPosition());
        }
    });
}
// set artist details to cardview_row
public void setArtistDetails(Context ctx, String image,String name){

    // set image view and text view from cardview_row.xml in activity_artists.xml recycler view
    ImageView imageViewartist = mView.findViewById(R.id.artistsimage_id);
    TextView nameViewartist = mView.findViewById(R.id.artistsname_id);
    //set data,we use Picasso library to set image
    Picasso.get().load(image).into(imageViewartist);
    nameViewartist.setText(name);
}

// interface for callback
public interface ClickListener{
    void onitemClick(View view,int position);
}

public void setOnClickListener(ViewHolder.ClickListener clickListener){
    mClickListener = clickListener;
}

// set advert to cardview_horizontal
public void setAdvertisement(Context ctx, String image){

    ImageView imageViewAdvert = mView.findViewById(R.id.imageviewadvert_id);
    Picasso.get().load(image).into(imageViewAdvert);
}
// set data to news textview
public void setInfoData(Context ctx, String info){

    TextView textViewInfo = mView.findViewById(R.id.advertnews_id);
    textViewInfo.setText(info);
}

public void setBiography(Context ctx, String topSong){

    TextView biotopsong = mView.findViewById(R.id.bio_details_id);
    biotopsong.setText(topSong);
}

}

Be the first to answer