Open a new screen, with an onMenuItemClick and send data

3

Good, I have a RecyclerView with its CardView , and when I click on the photo I get the drop-down menu that I created with 2 options.

With one of them I want to be able to open another activity to show more information.

Any idea how I can do it ?? Thank you very much

package com.example.usuario.coolmodapp.Lista;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.usuario.coolmodapp.R;

import java.util.List;

/**
 * Created by Usuario on 10/04/2017.
 */

public class PcAdapter extends RecyclerView.Adapter<PcAdapter.PcViewHolder> {

    private List<Pc> items;
    private Context mContext;

    public class PcViewHolder extends RecyclerView.ViewHolder {
        // Campos respectivos de un item
        public TextView nombre;
        public TextView precio;
        public TextView descripcion;
        public ImageView imagen;
        public ImageView mas;

        public PcViewHolder(View v) {
            super(v);
            nombre = (TextView) v.findViewById(R.id.nombre);
            precio = (TextView) v.findViewById(R.id.precio);
            descripcion = (TextView) v.findViewById(R.id.descripcion);
            imagen = (ImageView) v.findViewById(R.id.imagen);
        }
    }

    public PcAdapter(Context mContext, List<Pc> items)
    {
        this.mContext = mContext;
        this.items = items;

    }

    @Override
    public PcViewHolder onCreateViewHolder(ViewGroup parent, int viewTipe) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cardview_layout, parent, false);
        return new PcViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final PcViewHolder holder, int position) {
        Pc pc = items.get(position);
        holder.nombre.setText(pc.getNombre());
        holder.precio.setText(pc.getPrecio());
        holder.descripcion.setText(pc.getDescripcion());
        holder.imagen.setImageResource(pc.getImagen());

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

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

    class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener{

        public MyMenuItemClickListener(){
        }
//Donde estan las 2 opciones del menu
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId())
            {
                case R.id.action_add:
                    Toast.makeText(mContext, "Añadir", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.action_info:
                    Toast.makeText(mContext, "Mas informacion", Toast.LENGTH_SHORT).show();
                    return true;
                default:
            }
            return false;
        }
    }

    @Override
    public int getItemCount() {
        return items.size();
    }
}
    
asked by Cristian Prieto Beltran 20.04.2017 в 10:42
source

5 answers

1

Finally I put it like that and it works

@Override
        public boolean onMenuItemClick(MenuItem item) {
            final Intent intent;
            switch (item.getItemId())
            {
                case R.id.action_add:
                    Toast.makeText(mContext, "Añadir", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.action_info:
                    mContext.startActivity(new Intent(mContext, Completo.class));
                    break;
                default:
            }
            return false;
        }
    
answered by 20.04.2017 / 11:43
source
5

You've almost done it already:

@Override
            public boolean onMenuItemClick(MenuItem item) {
                final Intent intent;
                switch (item.getItemId())
                {
                    case R.id.action_add:
                        Toast.makeText(mContext, "Añadir",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.action_info:
                        intent =  new Intent(mContext, TuActivityDestino.class);
                        break;
                    default:
                }
                mContext.startActivity(intent);
                return true;
            }

As you can see, it's simple. Test and tell us.

    
answered by 20.04.2017 в 10:54
3

To open an activity from another, use the function startActivity(...)

in your case when computing the menu option

mContext.startActivity(new Intent(mContext, OtraActividad.class));
    
answered by 20.04.2017 в 11:06
2
  

Open a new screen, with an onMenuItemClick and send data.

To open a new Activity you would do an intent , as for sending data, you can add a bundle to send the data , for this you have as an example this question:

Passing data between activities

The change you would make would be to define a Intent to open the desired Activity , according to the option selected in the menu, and add data so that they are received in Activity to open.

        @Override
            public boolean onMenuItemClick(MenuItem item) {
                // * Define el Intent.
                final Intent intent = null;
                switch (item.getItemId())
                {
                    case R.id.action_add:
                        Toast.makeText(mContext, "Añadir",Toast.LENGTH_SHORT).show();
                        // * Define clase a abrir mediante Intent.
                        intent =  new Intent(mContext, OtraActivity1.class);
                        break;
                    case R.id.action_info:
                        // * Define clase a abrir mediante Intent.
                        intent =  new Intent(mContext, OtraActivity2.class);
                        break;
                    default:
                      // * Opción default clase a abrir mediante Intent.
                        intent =  new Intent(mContext, DefaultActivity.class);

                        break;
                }

               // * Agrega datos para enviar.
                intent.putExtra("usuario", "Cristian!");
                intent.putExtra("id", 123);
                intent.putExtra("latitud", 0.12324234);

                // * Inicia Activity
                mContext.startActivity(intent);
                return true;
            }
    
answered by 20.04.2017 в 17:30
1

First of all, it is not a good practice to create the listener onClick in onBindViewHolder :

Remove this line of code from onBindViewHolder :

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

And add this in the constructor of the class PcViewHolder :

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

If you want to start a DialogFragment use the following code:

  FragmentManager fm = ((FragmentActivity)mContext).getSupportFragmentManager();
  YourDialog yourDialog = new YourDialog();
  yourDialog.show(fm, "YourDialog");

If you want to start an activity:

Intent iActivity = new Intent(mContext, YourActivity.class);
mContext.startActivity(iActivity);
    
answered by 20.04.2017 в 11:12