如何将详细信息从Recycler视图传递到另一个活动?

时间:2015-10-26 08:40:46

标签: android android-recyclerview recycler-adapter

public class RVAdapter extends
RecyclerView.Adapter<RVAdapter.PersonViewHolder> {    public
List<Person> persons= Collections.emptyList(); //    private Context
mContext; private LayoutInflater inflater;



    ImageLoader mImageLoader = AppController.getInstance().getImageLoader();

    private static Context context;


    public RVAdapter(Context context, List<Person> persons) {
        this.persons = persons;
          this.context = context;
        inflater = LayoutInflater.from(context);
    }

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


        return pvh;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder holder, int postions) {

        Person person = persons.get(postions);
        holder.phoneName.setText(person.getKitchen_accesries());
        holder.phonePrice.setText(person.getKitchen_price());
        holder.itemView.setTag(person);

   if (mImageLoader == null)
           mImageLoader = AppController.getInstance().getImageLoader();
      holder.thumbnail.setImageUrl(person.getThumbnail(), mImageLoader);


    }


    @Override
    public int getItemCount() {
        if (persons != null) {
            return persons.size();
        }
        return 0;
    }


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    public static class PersonViewHolder extends RecyclerView .ViewHolder
            implements View
            .OnClickListener  {
        CardView cv;
        TextView phoneName;
        TextView phonePrice;
        public List<Person> persons;


        NetworkImageView thumbnail;

        PersonViewHolder(View itemView) {
            super(itemView);

            cv = (CardView) itemView.findViewById(R.id.card_view);
            phoneName = (TextView) itemView.findViewById(R.id.phone_name);
            phonePrice = (TextView) itemView.findViewById(R.id.phone_price);
            thumbnail = (NetworkImageView) itemView.findViewById(R.id.thumbnail);
            itemView.setOnClickListener(this); //
        }

        @Override
        public void onClick(View view ) {



            Intent intent = new Intent(context,ViewActivity.class);

            context.startActivity(intent);

        }


    }


}
  

一旦我触摸了recyclerview上的项目,它就会转到Viewactivity类,但是我无法将详细信息从我的列表传递给detailsactivity类。   我想使用put extra将我的网络服务中的三个细节传递给视图活动。但是在Onclick方法中,我没有从列表中获得项目的确切位置。

3 个答案:

答案 0 :(得分:1)

嘿,请在你的onclick中添加以下内容

      Person person =(Person)view.getTag();
        String strUrl= person.getThumbnail();
        String product1=person.getKitchen_accesries();
        intent.putExtra("Your string key",strUrl);
        intent.putExtra("Your string key",product1);
        intent.putExtra("Your string key",product1);
      context.startActivity(intent);
  

然后以详细信息的形式访问

中的所有内容
phone=(TextView)findViewById(R.id.textView_phone);
    price=(TextView)findViewById(R.id.textView_price);
    product=(ImageView)findViewById(R.id.imageView);




    Bundle bundle = getIntent().getExtras();

    String url = bundle.getString("Image");

    phone.setText(bundle.getString("Your string key"));
    price.setText(bundle.getString("Your string key"));


    Picasso.with(getApplicationContext()).load(url).into(product);

答案 1 :(得分:0)

  

我想将我的网络服务中的三个细节传递给视图   使用额外的活动

因为itemView是想要访问值以发送其他Activity的所有视图的根视图。所以使用onClick方法的view参数来访问视图:

@Override
        public void onClick(View view ) {
        TextView  phoneName = (TextView) view.findViewById(R.id.phone_name);
          // do same for others
         Intent intent = new Intent(context,ViewActivity.class);
        intent.putExtra("phone_name", phoneName.getText().toString());
          /// do same for other values
         context.startActivity(intent);

        }

答案 2 :(得分:0)

另一种方法是实现不使用接口而是运行时 使用此方法,您将能够在实现单击侦听器

时访问该对象
  

我已更新代码以在itemView上设置OnClickListener,而不是持有者本身。仅供参考,itemView是持有者持有的视图,以及将在回收者视图中显示的视图。

E.g

    @Override
    public void onBindViewHolder(PersonViewHolder holder, int postions) {

        Person person = persons.get(postions);
        holder.phoneName.setText(person.getKitchen_accesries());
        holder.phonePrice.setText(person.getKitchen_price());
        holder.itemView.setTag(person);

        if (mImageLoader == null)
           mImageLoader = AppController.getInstance().getImageLoader();
        holder.thumbnail.setImageUrl(person.getThumbnail(), mImageLoader);

        holder.itemView.setOnClickListener(new OnClickListener(View v){
            Intent i = new Intent(mContext, ViewActivity.class);
            i.putExtra("Hi",person.name);
            mContext.startActivity(i);
        });

    }