0

I am trying to call another class using intent. But it is not working. I am using android studio to implement this project.

The app should show a screen with logos of popular brands like amazon, google, twitter, facebook, etc. When a logo is clicked, it shows another screen(activity), by calling AnotherActivity.java

Below is the class which seems to contain the problem:

package com.aquino.gridlayoutmanagerrecyclerview;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityMain extends AppCompatActivity {

    RecyclerView rvMain;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rvMain = (RecyclerView) findViewById(R.id.rvMain);
        Bitmap[] logos = new Bitmap[12];
        logos[0] = BitmapFactory.decodeResource(getResources(), 
R.drawable.medida2);
    logos[1] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_fb);
    logos[2] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_google);
    logos[3] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_insta);
    logos[4] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_linkedin);
    logos[5] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_microsoft);
    logos[6] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_myspace);
    logos[7] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_skype);
    logos[8] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_snapchat);
    logos[9] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_twitter);
    logos[10] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_viber);
    logos[11] = BitmapFactory.decodeResource(getResources(), R.drawable.logo_whatsapp);
    MyAdapter adapter = new MyAdapter(getResources().getStringArray(R.array.company_list), logos);
    rvMain.setLayoutManager(new GridLayoutManager(ActivityMain.this, 2));
    rvMain.setAdapter(adapter);
}//end of onCreate()

private class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

    String[] companyList;
    Bitmap[] logoList;

    public MyAdapter(String[] companyList, Bitmap[] logoList) {
        this.companyList = companyList;
        this.logoList = logoList;
    }


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

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.logo.setImageBitmap(logoList[position]);
        holder.logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(this, AnotherActivity.class);
                startActivity(intent);
            }
        });
        holder.name.setText(companyList[position]);
    }

    @Override
    public int getItemCount() {
        return companyList.length;
    }

}//end of MyAdapter
private class MyViewHolder extends RecyclerView.ViewHolder{

    public ImageView logo;
    public TextView name;

    public MyViewHolder(View itemView) {
        super(itemView);
        logo = (ImageView)itemView.findViewById(R.id.ivLogo);
        name = (TextView)itemView.findViewById(R.id.tvCompany);
    }
 }//end of MyViewHolder
}

I cannot figure out why it is not working to add intent inside private class MyAdapter.

Any help is welcome

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Lua
  • 57
  • 8
  • what error you are getting? – Nouman Ch Feb 01 '18 at 13:44
  • @NoumanCh the error I am getting is: Error:(73, 37) error: no suitable constructor found for Intent(,Class) constructor Intent.Intent(String,Uri) is not applicable (argument mismatch; cannot be converted to String) constructor Intent.Intent(Context,Class>) is not applicable (argument mismatch; cannot be converted to Context) – Lua Feb 01 '18 at 19:54

2 Answers2

0

Im not sure what error you are getting.

good way to implement onclick in recyclerview is to create an interface.

implement the interface in the activity, create intent in the implemented method of interface in activity and call it in recyclerview

or use this :

Defining a RecyclerView's onCLickListener in an Activity

Raul
  • 104
  • 8
0

As per your code, all are fine. but in the private adapter, you will not get the context of the class directly by "this". so just replace code.

 Intent intent = new Intent(ActivityMain.this, AnotherActivity.class);
 startActivity(intent);
Pratik Satani
  • 1,115
  • 1
  • 9
  • 25
  • yes, now is compiling. Thank you! I thought the problem was because intent was inside a private class. The code now is compiling, but other error is showing, I will look for what is the cause. Thank you so much. – Lua Feb 01 '18 at 20:00