Java – How to open a new intent from recyclerview onclick

How to open a new intent from recyclerview onclick… here is a solution to the problem.

How to open a new intent from recyclerview onclick

I’m still a beginner in Android development, I set up some new views after clicking on my recylerview project, but my app is mixed between kotlin and android because I got some source code from the internet, but my project was compiled by kotlin from the beginning.

// Set New View Adapter
 Based on Java

holder.itemImageView.setOnClickListener(new CustomOnItemClickListener(position, new CustomOnItemClickListener.OnItemClickCallback() {
                @Override
                public void onItemClicked(View view, int position) {
                    if(holder.itemNameTextView.getText().equals("Pemerintahan (OPD)")){
                        open new intent
                    else if(holder.itemNameTextView.getText().equals("Pelayanan Publik")){
                       open new intent
                    }

The sample code I cited is to use kotlin show new activity started like this

// Referenced Code
 Based on Kotlin

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        creativeViewPagerView.setCreativeViewPagerAdapter(NatureCreativePagerAdapter(this))
    }
}

Exegesis:
createViewPagerView = is a property value on the xml activity main

NatureCreativePagerAdapter = is the adapter I will use

setCreativeViewPagerAdapter = function in the class with the following code

// setCreativeViewPagerAdapter\
 Based on Kotlin

fun setCreativeViewPagerAdapter(creativePagerAdapter: CreativePagerAdapter) {
    post({
      this.creativePagerAdapter = creativePagerAdapter
       Setup adapter for palette manager
      paletteCacheManager.setCreativeViewAdapter(creativePagerAdapter)
      paletteCacheManager.cachePalettesAroundPositionAsync(0, {
        refreshBackgroundColor(0, 0f)
      })

 Setup image adapter
      creativeImageAdapter.creativePagerAdapter = creativePagerAdapter
      creativeHeaderRecycler.layoutManager = LinearLayoutManager(context,
              LinearLayoutManager.HORIZONTAL, false)
      creativeHeaderRecycler.adapter = creativeImageAdapter

 Setup content adapter
      creativeContentAdapter.creativePagerAdapter = creativePagerAdapter
      creativeContentViewPager.adapter = creativeContentAdapter

creativeHeaderRecycler.post({ refreshImagesPosition(0f, 0) })
    })
  }

My question is how to get the referenced sample code to work on my //Set New View adapter
Thank you so much.

Solution

To create a new Intent, you only need an instance of the Context. You can get it from any instance of View (you have it in the onItemClicked method):

Context context = view.getContext();

Now to create and start Intent, you can write code like this:

Intent intent = new Intent(context, ActivityYouNeedToStart.class);
context.startActivity(intent);

So, the final code is:

@Override
public void onItemClicked(View view, int position) {
    Context context = view.getContext();
    Intent intent = new Intent(context, ActivityYouNeedToStart.class);
    context.startActivity(intent);
}

Related Problems and Solutions