简体   繁体   English

从 ViewHolder onclick 开始活动

[英]Start activity from ViewHolder onclick

I´m tryng to start a new activity when the user clicks on a RecycleView element .当用户单击 RecycleView 元素时,我正在尝试启动一个新活动。 The problem is that only one field of object I want to pass to JSON is show in the row_item Layout and don´t know how to retrieve the other 3 object fields.问题是只有一个我想传递给 JSON 的对象字段显示在 row_item 布局中,并且不知道如何检索其他 3 个对象字段。

Here are the steps I´m following .这是我正在遵循的步骤。

First I send a list of objects (Poliza) to the adapter .首先,我将对象列表 (Poliza) 发送到适配器。 Second I show only one field of the object (poliza) in the RecycleView .其次,我在 RecycleView 中只显示对象 (poliza) 的一个字段。 Third I want to start a new activity when the user clicks on the RecycleView item , sending a JSON of the object with the other object fields that are not show in the row_item .第三,当用户单击 RecycleView 项时,我想启动一个新活动,发送该对象的 JSON 以及未显示在 row_item 中的其他对象字段。

Here is my adapter这是我的适配器

public class PolizasAdapter extends RecyclerView.Adapter<PolizasAdapter.PolizaViewHolder> {

    private List<Poliza> listapoliza = null;
    private Context context;

    public PolizasAdapter(  List<Poliza> listapoliza) {
        this.listapoliza = listapoliza;
    }

    public class PolizaViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        // each data item is just a string in this case
        TextView txtPoliza;

        public PolizaViewHolder(View view) {
            super(view);
            //Agregamos un onclickListener
            view.setOnClickListener(this);
            context = view.getContext();
            //Hacemos referencia a las vistas del rowitem
            txtPoliza = (TextView) view.findViewById(R.id.txt_poliza);
        }

        @Override
        public void onClick(View view) {
            //Evento que que se genera cuando se da click
            TextView texto = (TextView) view.findViewById(R.id.txt_poliza);
            String str = texto.getText().toString();
            System.out.println(str);
            //Iniciamos actividad mostrando el detalle

             Intent intent = new Intent(context , DetalleActivity.class);
             intent.putExtra("id_poliza",str);
            //Whats next ?
            //How to add the other 3 fields of my object to the putExtra


        }
    }

    @Override
    public PolizasAdapter.PolizaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //Creamos una nueva vista
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
        PolizaViewHolder viewHolder = new PolizaViewHolder(view);
        return viewHolder;
    }

     @Override
    public void onBindViewHolder(PolizasAdapter.PolizaViewHolder holder, int position) {

        holder.txtPoliza.setText("Poliza : " + String.valueOf(listapoliza.get(position).getPoliza()));
    }

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

For starting the Activity you should do context.startActivity(intent);要启动 Activity,您应该执行context.startActivity(intent); As for the 3 JSON parameter you're talking about, i couldn't find any reference of them in your code (there's no reference to any JSON whatsoever).至于您正在谈论的 3 个 JSON 参数,我在您的代码中找不到它们的任何引用(没有任何 JSON 的引用)。 I guess you might be talking about a field from Poliza object, but i can't be sure.我猜您可能在谈论来自 Poliza 对象的字段,但我不能确定。

Use Activity context使用活动上下文

Intent intent = new Intent(context,AnotherActivity.class);
context.startActivity(intent);

使用view.context.startactivity(new Intent(view.context.this, NewActivity().class))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM