Edit / Delete Firebase data on a RecyclerView

0

Good, I have an APP that shows some information about Notices (Name of warning, description, location) and all this is saved in FireBase, I show the data with a RecyclerView and its CardView Adapter.

I use a PopupMenu to show the 2 options How can I make the Edit / Delete a Notice options?

My idea would be, to know in which position you click on the RecyclerView and to collect the Uid that you have and edit your data, some idea of how you could do it? How to know the position and open the correct Notice?

I leave here the code that I have to show the Data

public class VerAvisos extends MenuAvisos
{

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    FirebaseUser user = firebaseAuth.getInstance().getCurrentUser();

    List<Aviso> avisos;
    RecyclerView rv;
    AdaptadorAvisos adapter;

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

        avisos = new ArrayList<>();

        rv=(RecyclerView) findViewById(R.id.rvAvisos);
        rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AdaptadorAvisos(this,avisos);
        rv.setAdapter(adapter);

        database.getReference("Aviso").child(user.getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                avisos.clear();
                for(DataSnapshot snapshot :
                        dataSnapshot.getChildren()){

                    Aviso aviso2 = snapshot.getValue(Aviso.class);
                    avisos.add(aviso2);

                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

And its Adapter

public class AdaptadorAvisos extends RecyclerView.Adapter<AdaptadorAvisos.AvisosviewHolder> {

    List<Aviso> avisos;
    private Context mContext;

    public static class AvisosviewHolder extends RecyclerView.ViewHolder {

        TextView tvAviso;
        TextView tvDescripcion;
        TextView tvUbicacion;

        public AvisosviewHolder(View itemView) {
            super(itemView);
            tvAviso = (TextView) itemView.findViewById(R.id.tvAviso);
            tvDescripcion = (TextView) itemView.findViewById(R.id.tvDescripcion);
            tvUbicacion = (TextView) itemView.findViewById(R.id.tvUbicacion);
        }
    }

    public AdaptadorAvisos(Context mContext, List<Aviso> avisos)
    {
        this.mContext = mContext;
        this.avisos = avisos;
    }

    @Override
    public AvisosviewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_aviso, parent, false);
        AvisosviewHolder holder = new AvisosviewHolder(v);

        return holder;
    }

    @Override
    public void onBindViewHolder(final AvisosviewHolder holder, int position) {
        Aviso aviso = avisos.get(position);
        holder.tvAviso.setText(aviso.getAviso());
        holder.tvDescripcion.setText(aviso.getDescripcion());
        holder.tvUbicacion.setText(aviso.getUbicacion());

        holder.tvAviso.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupMenu(holder.tvAviso);
            }
        });
    }

    private void showPopupMenu(View v)
    {
        PopupMenu popup = new PopupMenu(mContext, v);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.menu_avisos, popup.getMenu());
        popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
        popup.show();
    }

    class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener{

        public MyMenuItemClickListener(){
        }

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = null;
            switch (item.getItemId())
            {
                case R.id.action_eliminar:
                    Toast.makeText(mContext, "Eliminar Aviso (Test)", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.action_editar:
                    Toast.makeText(mContext, "Editar Aviso (Test)", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.action_volver:
                    intent = new Intent(mContext, Menu.class);
                default:
            }
            mContext.startActivity(intent);
            return true;
        }
    }

    @Override
    public int getItemCount() {
        return avisos.size();
    }
}
    
asked by Cristian Prieto Beltran 08.05.2017 в 11:02
source

1 answer

0

I think it would be more appropriate for the adapter to pass the full notice to the view in the onClick by means of a listener. In the end I give you an example. However, you could also send the full Notice to the MyMenuItemClickListener class and use it later on the onMenuItemClick

private void showPopupMenu(View v, Aviso aviso)
    {
        PopupMenu popup = new PopupMenu(mContext, v);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.menu_avisos, popup.getMenu());
        popup.setOnMenuItemClickListener(new MyMenuItemClickListener(aviso));
        popup.show();
    }
...
...
...
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener{

private Aviso aviso;

public MyMenuItemClickListener(Aviso){
  this.aviso = aviso;
        }

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = null;
            switch (item.getItemId())
            {
                case R.id.action_eliminar:
                    Toast.makeText(mContext, "Eliminar Aviso (Test) " + aviso.getAviso(), Toast.LENGTH_SHORT).show();
                    return true;

...
...
...

I give you an example of how you can pass Notice through a new listener. In the example, the entity is called Goal

Adapter

public class GoalListAdapter extends RecyclerView.Adapter<GoalListAdapter.ViewHolder> {

    private List<Goal> goalList;
    private GoalItemClickListener goalItemClickListener;

    public GoalListAdapter() {
    }

    public void setGoalList(List<Goal> goalList) {
        this.goalList = goalList;
        notifyDataSetChanged();
    }

    public void setGoalItemClickListener(GoalItemClickListener goalItemClickListener) {
        this.goalItemClickListener = goalItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goal_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Goal goal = goalList.get(position);
        holder.setClickListener(goal, goalItemClickListener);

        holder.tvTitle.setText(goal.getTitle());
        holder.tvDescription.setText(goal.getDescription());
    }

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

    public void add(Goal goal){
        if (!goalList.contains(goal)){
            goalList.add(goal);
            notifyDataSetChanged();
        }
    }

    public void update(Goal goal){
        if (goalList.contains(goal)){
            goalList.set(goalList.indexOf(goal), goal);
            notifyDataSetChanged();
        }
    }

    public void remove(Goal goal){
        if (goalList.contains(goal)){
            goalList.remove(goal);
            notifyDataSetChanged();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.goal_list_item_tv_title)
        TextView tvTitle;
        @BindView(R.id.goal_list_item_tv_description)
        TextView tvDescription;

        private View view;

        public ViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
            ButterKnife.bind(this, view);
        }

        private void setClickListener(final Goal goal, final GoalItemClickListener listener){

            if (listener != null) {
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.onItemClick(goal);
                    }
                });

                view.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        listener.onItemLongClick(goal);
                        return false;
                    }
                });
            }
        }
    }
}

View public class GoalListActivity extends BaseActivity implements GoalListView, GoalItemClickListener {

private static final String TAG = GoalListActivity.class.getSimpleName();

@BindView(R.id.goal_list_activity)
CoordinatorLayout coordinatorLayout;

@BindView(R.id.goal_list_activity_toolbar)
Toolbar toolbar;

@BindView(R.id.goal_list_activity_rv_concepts)
RecyclerView rvConcepts;

@Inject
GoalListPresenter presenter;

@Inject
GoalListAdapter adapter;

@Inject
LinearLayoutManager linearLayoutManager;

@Inject
VerticalDividerItemDecoration dividerItemDecoration;

@Override
protected int getToolbar() {
    return R.id.goal_list_activity_toolbar;
}

@Override
protected int getLayout() {
    return R.layout.goal_list_activity;
}

@Nullable
@Override
protected BasePresenter getPresenter() {
    return presenter;
}

@Override
public void setUpComponent(AppComponent appComponent) {
    DaggerGoalListComponent.builder()
            .appComponent(appComponent)
            .goalListModule(new GoalListModule(this))
            .build()
            .inject(this);
}

@Override
public void setUpView() {
    rvConcepts.setLayoutManager(linearLayoutManager);
    rvConcepts.addItemDecoration(dividerItemDecoration);
    rvConcepts.setHasFixedSize(true);
    rvConcepts.setAdapter(adapter);

    adapter.setGoalItemClickListener(this);
}

@Override
public void navigateToConceptAddView() {
    Intent intent = new Intent(this, GoalAddActivity.class);
    startActivity(intent);
}

@Override
public void showConceptList(List<Goal> goalList) {
    adapter.setGoalList(goalList);
}


@OnClick(R.id.goal_list_activity_fab)
public void onClick() {
    presenter.showConceptAdd();
}

@Override
public void showError(String message) {
    Snackbar.make(coordinatorLayout, message, Snackbar.LENGTH_LONG).show();
}

@Override
public void onItemClick(Goal goal) {

}

@Override
public void onItemLongClick(Goal goal) {

}

}

As you can see, the view sets the adapter in the recyclerview and sets the view itself as GoalItemClickListener (adapter.setGoalItemClickListener (this);) since it implements it

@Override
public void onItemClick(Goal goal) {

}

@Override
public void onItemLongClick(Goal goal) {

}
    
answered by 08.05.2017 в 18:11