Intent error with private class

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

Answer

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);

Leave a Reply

Your email address will not be published. Required fields are marked *