0

How can I go to another activity by clicking a list item?

myPDFListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @SuppressLint("IntentReset")
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            getSemesters getsemesters = getSemesters.get(position);
            Intent intent = new Intent();
            intent.setType(Intent.ACTION_VIEW);
            startActivity(intent);

        }
    });

This is my old code. With this code I'm able to view the PDF's. But now what I want is when I am clicking on a list item it should go to a new activity with passing a string to another activity. What should be changed? Please suggest me.

2 Answers2

0

To pass a string you can use putExtra method

Intent intent = new Intent();
intent.setType(Intent.ACTION_VIEW);
intent.putExtra("PDF_KEY", "SELECTED PDF VALUE");
startActivity(intent);

and use this to get the value in the opened activity

 getIntent().getStringExtra('PDF_KEY')

Edited:

the error showing because this here means the listener, try this way

Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.setType(Intent.ACTION_VIEW);
intent.putExtra("PDF_KEY", "SELECTED PDF VALUE");
startActivity(intent);
plsankar
  • 437
  • 1
  • 5
  • 11
  • By using this line intent.setType(Intent.ACTION_VIEW); it will not go to another activity. We should provide the class names, but when I write the code in this form (Intent intent = new Intent(this, anyClassName.class);). It shows error. – BhaskarGogoi Jul 03 '19 at 17:12
  • @bhaskargogoi hi, try the new code – plsankar Jul 03 '19 at 17:17
0

To go to new Activity try this

Intent intent = new Intent(this,YourNewActivityName.class);
intent.putExtra("KEY", "PDF VALUE");
startActivity(intent);

and to get this value in new Activity use

String value=getIntent().getStringExtra('PDF_KEY');
Lokik Soni
  • 602
  • 1
  • 5
  • 25