Home > Net >  Android: pass data shown on recyclerview and populate another acitivty
Android: pass data shown on recyclerview and populate another acitivty

Time:10-11

i want to pass the data received by recyclerview from firebase realtime database to antother activity. so when user click on anyitem it would open another activity with full data received(Image, name, location, description).

Note: i tried using setonclick listener for firebaseRecyclerAdapter but android studio show no OnClickListener, so where am i suppose to use onclick?

my MainAcitivty :

@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerOptions<HomeWorkView> options = new FirebaseRecyclerOptions.Builder<HomeWorkView>()
            .setQuery(reference, HomeWorkView.class)
            .build();

    FirebaseRecyclerAdapter<HomeWorkView, HomeScreenWorkViewHolder> firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<HomeWorkView, HomeScreenWorkViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull HomeScreenWorkViewHolder viewHolder, int i, @NonNull HomeWorkView homeWorkView) {
                    viewHolder.setdetails(getApplicationContext(), homeWorkView.getName(), homeWorkView.getImage(), homeWorkView.getDescription(), homeWorkView.getLocation());
                }

                @NonNull
                @Override
                public HomeScreenWorkViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.work_item_display_layout, parent,false);
                    HomeScreenWorkViewHolder holder = new HomeScreenWorkViewHolder(view);
                    return holder;
                }
            };
    firebaseRecyclerAdapter.startListening();
    mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}

ViewHolder:

public class HomeScreenWorkViewHolder  extends RecyclerView.ViewHolder {
    View view;
    public HomeScreenWorkViewHolder(@NonNull View itemView) {
        super(itemView);

        view = itemView;
    }

    public void setdetails(Context context, String name, String image, String description, String location) {
        TextView mtitletv = view.findViewById(R.id.product_layout_name);
        TextView mdesrcriptiontv = view.findViewById(R.id.product_layout_description);
        TextView mlocationtv = view.findViewById(R.id.product_layout_location);
        ImageView mImagetv = view.findViewById(R.id.product_layout_image);


        mtitletv.setText(name);
        mdesrcriptiontv.setText(description);
        mlocationtv.setText(location);
        Picasso.get().load(image).into(mImagetv);

    }
}

Class:

public class HomeWorkView {

    String Name, Image, Description, Location;

    public HomeWorkView() {

    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getImage() {
        return Image;
    }

    public void setImage(String image) {
        Image = image;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public String getLocation() {
        return Location;
    }

    public void setLocation(String location) {
        Location = location;
    }
}

CodePudding user response:

Create onItemClickListener for firebaseAdapter: create itemclicked callback:

public interface OnItemClick{
    void onItemClicked(int position);
}

Change your viewHolder:

public class HomeScreenWorkViewHolder  extends RecyclerView.ViewHolder{
   public HomeScreenWorkViewHolder(@NonNull View itemView,OnItemClick callBack) {
    super(itemView);
     view = itemView;
       itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.onItemClicked(getAdapterPosition());
            }
        });
   }
}

Change onStart:

@Override
protected void onStart() {
   super.onStart();
OnItemClick onItemClick = new OnItemClick{
    @Overide
      void onItemClicked( int position ){
        HomeWorkView item = firebaseRecyclerAdapter.getItem(position);
        String name = item.name;
        String description = item.description
        Intent intent =new  Intent(this@HomeActivity, 
         AnotherActivity::class);
        intent.putExtra("name",name);
        intent.putExtra("description",description);
        startActivity(intent);
    }
 }
};

And change this line:

 HomeScreenWorkViewHolder holder = new HomeScreenWorkViewHolder(view);

to

HomeScreenWorkViewHolder holder = new HomeScreenWorkViewHolder(view,onItemClick);

You can pass data between activities using intent. In your activities:

firebaseRecyclerAdapter.setOnItemClickListener(new  OnItemClick{
    @Overide
      void onItemClicked( int position ){
        HomeWorkView item = firebaseRecyclerAdapter.getItem(position);
        String name = item.name;
        String description = item.description
        Intent intent =new  Intent(this@HomeActivity, 
         AnotherActivity::class);
        intent.putExtra("name",name);
        intent.putExtra("description",description);
        startActivity(intent);
    }
})

In onCreate method of AnotherActivity:

String name =  getIntent.getExtras().getString("name");

Another solution, make your HomeWorkView implements Serializable :

public class HomeWorkView implements Serializable {
}

And pass it to AnotherActivity:

firebaseRecyclerAdapter.setOnItemClickListener(new  OnItemClick{
@Overide
  void OnItemClicked( HomeWorkView item ){
    Intent intent =new  Intent(this@HomeActivity, 
     AnotherActivity::class);
    intent.putExtra("homework",item);
    startActivity(intent);
}

}) Then in onCreate method of AnotherActivity:

HomeWorkView data = getIntent().getSerializableExtra("homework");
  • Related