Alberkko619
Alberkko619

Reputation: 39

Passing item data to activity

I'm trying to retrieve the data from my database and send it to each Recyclerview item, so far I've retrieved the data and populated my recyclerviewer however, I am not sure how to send this data to the page of each item. I have created an onclick that returns the position of the item, and I can get the position of the item inside the profile activity with an

putExtra()

but not sure how to do the same for the Names and images of the items.

MainActivity.java

public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter.OnItemClickListener {

private RecyclerView mRecyclerView;
private RecyclerViewAdapter mAdapter;
private DatabaseReference mDatabaseRef;
private List<Buildings> mUploads;

private String name;


private FirebaseStorage mStorage;
private ValueEventListener mDBListener;

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

    mRecyclerView = findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mUploads = new ArrayList<>();
    mStorage = FirebaseStorage.getInstance();
    mDatabaseRef = FirebaseDatabase.getInstance().getReference("Buildings");

    mAdapter = new RecyclerViewAdapter(MainActivity.this, mUploads);

    mRecyclerView.setAdapter(mAdapter);

    mAdapter.setOnItemClickListener(MainActivity.this);

    mDBListener = mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            mUploads.clear();

            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Buildings upload = postSnapshot.getValue(Buildings.class);
                upload.setKey(postSnapshot.getKey());
                mUploads.add(upload);

                name = upload.getName();
            }

            mAdapter.notifyDataSetChanged();

         //   mAdapter = new RecyclerViewAdapter(MainActivity.this, mUploads);

        //    mRecyclerView.setAdapter(mAdapter);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(MainActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();

        }
    });
}

@Override
public void onItemClick(int position) {


   // String buildingName = mUploads.get(position).toString();

    Intent mainIntent = new Intent(MainActivity.this, ProfileActivity2.class);
   // mainIntent.putExtra("pos", buildingName);
    startActivity(mainIntent);
    finish();
}

@Override
public void onWhatEverClick(int position) {
    Toast.makeText(this, "Whatever click at position: " + position, Toast.LENGTH_SHORT).show();
}

@Override
public void onDeleteClick(int position) {
    Buildings selectedItem = mUploads.get(position);
    final String selectedKey = selectedItem.getKey();

    StorageReference imageRef = mStorage.getReferenceFromUrl(selectedItem.getImage_url());
    imageRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            mDatabaseRef.child(selectedKey).removeValue();
            Toast.makeText(MainActivity.this, "Item deleted", Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mDatabaseRef.removeEventListener(mDBListener);
}

RecyclerViewAdapter.Java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ImageViewHolder> {

private Context mContext;
private List<Buildings> mUploads;
private OnItemClickListener mListener;

public RecyclerViewAdapter(Context context, List<Buildings> uploads) {
    mContext = context;
    mUploads = uploads;
}

@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.buildings_row_item, parent, false);
    return new ImageViewHolder(v);


}

@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
    Buildings uploadCurrent = mUploads.get(position);
    holder.textViewName.setText(uploadCurrent.getName());
    Glide.with(mContext).load(mUploads.get(position).getImage_url()).into(holder.imageView);
}


@Override
public int getItemCount() {
    return mUploads.size();
}

public class ImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
        View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener {

    public TextView textViewName;
    public ImageView imageView;
    LinearLayout view_container;

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

        textViewName = itemView.findViewById(R.id.building_name);
        imageView = itemView.findViewById(R.id.thumbnail);
        view_container = itemView.findViewById(R.id.container);

        itemView.setOnClickListener(this);
        itemView.setOnCreateContextMenuListener(this);

    }


    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if (mListener != null) {
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {

                switch (item.getItemId()) {
                    case 1:
                        mListener.onWhatEverClick(position);
                        return true;
                    case 2:
                        mListener.onDeleteClick(position);
                        return true;
                }
            }
        }
        return false;
    }


    @Override
    public void onClick(View v) {
        if (mListener != null) {
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {
                mListener.onItemClick(position);
            }
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Select Action");
        MenuItem doWhatever = menu.add(Menu.NONE, 1, 1, "Do whatever");
        MenuItem delete = menu.add(Menu.NONE, 2, 2, "Delete");

        doWhatever.setOnMenuItemClickListener(this);
        delete.setOnMenuItemClickListener(this);
    }
}

public interface OnItemClickListener {
    void onItemClick(int position);

    void onWhatEverClick(int position);

    void onDeleteClick(int position);
}

public void setOnItemClickListener(OnItemClickListener listener) {
    mListener = listener;
}

}

Upvotes: 0

Views: 65

Answers (2)

Jignesh Mayani
Jignesh Mayani

Reputation: 7193

First make your model class Buildings Serializable, then in onItemClick() get item value and pass this data to next activity to get all data related to this item.

Buildings currentItem = mUploads.get(position);
mainIntent.putExtra("buildingData", buildingName);

in detail activity class get item data:

Buildings data = (Buildings) getIntent().getSerializableExtra("buildingData");

Upvotes: 1

RITESH KUMAR
RITESH KUMAR

Reputation: 1100

why are you not using Interface for this... Create an Interface

public interface SendData {
public void mainData(int Pos, BeanData data); // this is your complete bean class which is send from adapter's item click to main class

}

Make object of this interface in your adatper :-

SendData sendData;

then under constructore :- this.sendData = sendData;

then onitemClick :-

sendData.mainData(position, data); // herer data is your bean class

and then implement this interface in your Mainactivity that's it....

Make sure you already send Interface object from MainAcitvity to recyclerview when setting your adapter with recycler view data.

Upvotes: 0

Related Questions