[英]How to open pdf files stored in assets folder while i am using listview?
將此代碼放在您要處理列表視圖單擊的位置
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.