0

I am trying to pass the Gson object to intent by using the below code and sen it to another activity for detail. I tried to use code from this instruction in comment, but still eror.. ( How to pass gson serialised object to Intent in android? )

This is my code:

public class CustomListAdapter extends ArrayAdapter<Beer> {


    Context context;
    int layoutResourceId;
    public List<Beer> data = null;


   public CustomListAdapter(Context context, int resource, List<Beer> objects) {
        super(context, resource, objects);

        this.context = context;
        this.layoutResourceId = resource;
        this.context = context;
        this.data = objects;

    }

    static class DataHolder
    {
        ImageView ivBeer;
        TextView tvBeerName;
        TextView tvDescription;
        TextView abv;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {

        DataHolder holder = null;

        if (convertView == null)
        {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            convertView = inflater.inflate(layoutResourceId, parent,false);
            holder = new DataHolder();

            holder.ivBeer = (ImageView) convertView.findViewById(R.id.ivBeer);
            holder.tvBeerName = (TextView) convertView.findViewById(R.id.tvBeerName);
            holder.tvDescription = (TextView) convertView.findViewById(R.id.tvDescription);
            holder.abv = (TextView) convertView.findViewById(R.id.abv);

            convertView.setTag(holder);
        }

        else
        {
            holder = (DataHolder) convertView.getTag();
        }

        Beer beer=data.get(position);

        holder.tvBeerName.setText(beer.name);
        Picasso.with(context).load(beer.imageUrl).into(holder.ivBeer);
        holder.tvDescription.setText(beer.description);
        holder.abv.setText(beer.abv);


// this is the trouble part: 

        Gson gson = new Gson();
            Intent intent = new Intent (CustomListAdapter.this, BeerDetailActivity.class);
            intent.putExtra("obj", gson.toJson(data));


        return convertView;

    }
}
pleft
  • 7,567
  • 2
  • 21
  • 45
  • Please provide the log so that the community can see the error and provide inputs. – Soumya Nov 02 '17 at 20:47
  • Error:(103, 25) error: no suitable constructor found for Intent(CustomListAdapter,Class) constructor Intent.Intent(String,Uri) is not applicable (argument mismatch; CustomListAdapter cannot be converted to String) constructor Intent.Intent(Context,Class>) is not applicable (argument mismatch; CustomListAdapter cannot be converted to Context) – Tomin_com Nov 02 '17 at 20:59

1 Answers1

1

If you want to pass the data object to another activity, BeerDetailActivity, you have to "call" this activity to start by:

context.startActivity(intent);

right after the line:

intent.putExtra("obj", gson.toJson(data));

However I am not sure if this is a correct thing to do inside the getView method of your adapter

pleft
  • 7,567
  • 2
  • 21
  • 45