簡體   English   中英

如何在使用 listview 時打開資產文件夾中存儲的 pdf 文件?

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

我有一些 6 個主題的列表,當我單擊不同的列表項時,我想加載不同的 pdf 文件。每個列表項都應遷移到各自的主題 pdf 文件??僅使用一個活動並遷移到如何做到這一點多個 pdf 文件按各自的項目單擊 ?? 誰能幫我這個 ?? 活動.java

活動1.java

將此代碼放在您要處理列表視圖單擊的位置

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

            }
        });

以及我稱之為PDfActivity其他活動,您可以隨意更改名稱...在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM