0

I'm building on a tutorial I did in which I created a RecyclerView screen with cards with selectable options. I want one of the selectable options to bring the user to a new activity that has more information & options about the card they selected. My problem is when I try to transfer traits of that specific card to the next SlideViewActivity.java activity I am unable to successfully do so. I tried transforming my list into an array then sending that, but I keep obtaining a null value (which could be due to my syntax for all I know).

Any clarification & guidance would be appreciated, let me know if you would want any of the other code as well.

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
private Context mContext;
private List<Properties> dogList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title, count;
    public ImageView thumbnail, overflow;

    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title);
        count = (TextView) view.findViewById(R.id.count);
        thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
        overflow = (ImageView) view.findViewById(R.id.overflow);
    }
}

public Adapter(Context mContext, List<Properties> dogList) {
    this.mContext = mContext;
    this.dogList = dogList;
}

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

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    Properties dog = dogList.get(position);
    holder.title.setText(dog.getName());
    // loading dog cover using Glide library
    Glide.with(mContext).load(dog.getThumbnail()).into(holder.thumbnail);
    holder.overflow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showPopupMenu(holder.overflow);
        }
    });
}

/**
 * Showing popup menu when tapping on icon
 */
private void showPopupMenu(View view) {
    // inflate menu
    PopupMenu popup = new PopupMenu(mContext, view);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu, popup.getMenu());
    popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
    popup.show();
}

/**
 * Click listener for popup menu items
 */
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {

    public MyMenuItemClickListener() {
    }

    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.action_add_favourite:
                Toast.makeText(mContext, "Add to favourite", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.action_more_info:
                Intent slideStart = new Intent(mContext, SlideViewActivity.class);
                String[] dogArray = new String[dogList.size()];
                slideStart.putExtra("List", dogArray);
                Log.e("putting extra", String.valueOf(dogArray[0]));
                //TODO:MAKE NAME TRANSFERS WORK
                mContext.startActivity(slideStart);
                return true;
            default:
        }
        return false;
    }
}

Adding Properties.java:

public class Properties {
private String name;
private String info;
private int thumbnail;

public Properties() {
}

public Properties(String name, String info, int thumbnail) {
    this.name = name;
    this.info = info;
    this.thumbnail = thumbnail;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getInfo() {
    return info;
}

public void getInfo(String info) {
    this.info = info;
}

public int getThumbnail() {
    return thumbnail;
}

public void setThumbnail(int thumbnail) {
    this.thumbnail = thumbnail;
}
}
lexalenka
  • 307
  • 4
  • 16

4 Answers4

1

You can pass ArrayList<T>, if T is Serializable.

for example:

ArrayList<String> list = new ArrayList<String>();
intent.putExtra("list", list);

use getSerializableExtra to extract data

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I found this way to be the simplest - able to get size but having trouble getting name values but that's probably my syntax at this point. I'll let you know if I have major problems after trying a bit more, thanks :) – lexalenka Jan 23 '17 at 18:00
0

Your array is dogList.size()-d null values.

String[] dogArray = new String[dogList.size()];
slideStart.putExtra("List", dogArray);

You should see null logged here.

Log.e("putting extra", String.valueOf(dogArray[0]));

It's not clear what type of class you have for Properties, but if it is your class, and not java.util.Properties, you should implement Parcelable on that class, then you'd have

intent.putParcelableArrayList("List", dogList)

(Tip: You should rename that class to Dog to avoid confusion for yourself and others)

But if you are just worried about getting null, case matters. You put a key="List", so make sure you aren't getting key="list" or anything else but "List"

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Two ways possible for that::

  1. First

While Sending the list using intent::

Intent slideStart = new Intent(mContext, SlideViewActivity.class);
slideStart.putExtra("List", dogList);
mContext.startActivity(slideStart);

In This just pass the list as is it is ::

But your class Properties should be implements "Serializable"

Now while receiving that intent ::

ArrayList<Properties> dogList =(ArrayList<Properties>) getIntent().getExtras().getSerializable("List");
  1. Second way :: using Library

One library is there for converting string to arraylist & vice versa ,

compile 'com.google.code.gson:gson:2.4'

So, for this while sending list convert that list to string like ::

Type listType  = new TypeToken<List<Properties>>() {
                        }.getType();
String listString=new Gson().toJson(dogList,listType );

pass this simple as string with intent:::

Intent slideStart = new Intent(mContext, SlideViewActivity.class);
slideStart.putExtra("List", listString);
mContext.startActivity(slideStart);

And while getting it back in other activity::

 String listString =getIntent().getExtras().getString("List");
Type listType = new TypeToken<List<Properties>>() {}.getType();
 List<Properties> list=new Gson().fromJson(listString, listType);

Tell me if need more help..

Anil Prajapati
  • 457
  • 3
  • 10
0

I have done a similar project I will share my code. Please take a look and if have doubts ping me

DataAdapter.java

package com.example.vishnum.indiavideo;
import android.content.Context;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    private Context context;
    List<Video_Details> video;

    public DataAdapter(List<Video_Details> video, Context context) {
        super();
        this.context = context;
        this.video = video;
    }

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

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final Video_Details videoDetails =  video.get(position);
        String url;
        final String VideoID;

        holder.title.setText(video.get(position).getTitle());

        VideoID= video.get(position).getV_id();
        url = video.get(position).getThumb();

         Glide.with(context)
                 .load(url)
                 .override(150,70)
                 .into(holder.thumb);
       //viewHolder.thumb.setText(android.get(i).getVer());
      //  viewHolder.tv_api_level.setText(android.get(i).getApi());

        holder.vm.setOnClickListener(new View.OnClickListener() {
                                         @Override
                                         public void onClick(View v) {
                                             Toast.makeText(v.getContext(), "You Clicked"+video.get(position).getV_id(), Toast.LENGTH_SHORT).show();
                                             Intent intent = new Intent(v.getContext(),Play_Video.class);
                                             intent.putExtra("VideoId",(video.get(position).getV_id()));
                                             intent.putExtra("Title",(video.get(position).getTitle()));
                                             v.getContext().startActivity(intent);


                                         }
                                     }

        );
    }

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




    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView title;
        public ImageView thumb;
        public String videoid;
        public View vm;
        public ViewHolder(View view) {
            super(view);
            vm = view;
            title = (TextView)view.findViewById(R.id.title);
            thumb = (ImageView) view.findViewById(R.id.thumb);



            //tv_version = (TextView)view.findViewById(R.id.tv_version);
            //tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);

        }
    }


}

Video_Details.java

package com.example.vishnum.indiavideo;
public class Video_Details {
    private String id;
    private String v_id;
    private String title;
    private String thumb;

    public String getId() {
        return id;
    }

    public String getV_id() {
        return v_id;
    }

    public String getTitle() {
        return title;
    }

    public String getThumb() {
        return (Constants.urlvideo+v_id+"/0.jpg");
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setV_id(String v_id) {
        this.v_id = v_id;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setThumb(String v_id) {
        this.thumb =Constants.urlvideo+v_id+"/0.jpg";
    }
}
Vishnu M Menon
  • 1,459
  • 2
  • 19
  • 34