How to open new intent from recylerview onclick

Shin :

I am still beginner on Android dev, I'm setting up some new view after my recylerview item clicked, but my app are mixed between kotlin and android because i get some source code from internet, but my project since the beginning were compiled by kotlin.

// 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 referenced are used 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))
    }
}

notes : createViewPagerView = are attribute value on xml activity main NatureCreativePagerAdapter = are the adapter i will use

setCreativeViewPagerAdapter = a function inside a class with code below

// 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 Many Thanks.

Andrew Churilo :

To create a new Intent you need just an instance of Context. You can get it from any instance of View (you have it inside 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, 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);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=91805&siteId=1