简体   繁体   中英

How to open pdf files stored in assets folder while i am using listview?

I have some list of 6 subjects,I want to load different pdf file when i click on different list items.Every list item should migrate to their respective subject pdf file ??How can this be possible by using only one activity and getting migrated to multiple pdf files by their respective item on click ?? Can anyone help me with this ?? activity.java

activity1.java

Put this code at where you wanted to handle listview click

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

                Intent i = new Intent(this,PdfActivity.class); // Common activity for all
                i.putExtra("position",position); // send position to Intent
                startActivity(i);

            }
        });

and on other activity which is PDfActivity which I called , you can change name whatever you want ... do below in onCreate()

    int position = 0;
        Bundle bundle = getIntent().getExtras();
                if (bundle != null) {
   position = bundle.getInt("position") // String Which are send through intent
        }



 if(position==0){
      //  view.fromAsset(<your file Name in position 0 on the list>.pdf).load();
   } else if(position==1){
   // view.fromAsset(<your file Name in position 1 on the list>.pdf).load();
   }else if(position==2){
    //view.fromAsset(<your file Name in position 2 on the list>.pdf).load();
   }else if(position==3){
    //view.fromAsset(<your file Name in position 3 on the list>.pdf).load();
   }else if(position==4){
    //view.fromAsset(<your file Name in position 4 on the list>.pdf).load();
   }

   and So on.....
   //in your on create motod do it 
   listView=findViewById(R.id.grid_View);
  UnlockAdapter adapter= new UnlockAdapter(Home.this,values);
 listView.setAdapter(adapter);
 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(Home.this, "you pressed on"+position, 
      Toast.LENGTH_SHORT).show();
        String str_pos = String.valueOf(position);
        Intent intent = new Intent(getApplicationContext(),Sub_Details.class);
        intent.putExtra("myKey", str_pos);
        startActivity(intent);
    }
});
 // here is the activty where you send your intent(Sub_Detail)

 Bundle extras = getIntent().getExtras();
  String tmp = extras.getString("myKey");
  Toast.makeText(this, "new Activity: "+tmp, Toast.LENGTH_SHORT).show();
  int tmps= Integer.parseInt(tmp);
  // now do whatever you want on that position


  if (tmps==0){
   
    pdfview.fromAsset("1.pdf").load();
  }
  if (tmps==1){
   pdfview.fromAsset("2.pdf").load();
  }
  if (tmps==2){
   pdfview.fromAsset("3.pdf").load();
  }

使用这个库打开 pdf 文件Android PdfViewer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM