[英]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 ??我有一些 6 个主题的列表,当我单击不同的列表项时,我想加载不同的 pdf 文件。每个列表项都应迁移到各自的主题 pdf 文件??仅使用一个活动并迁移到如何做到这一点多个 pdf 文件按各自的项目单击 ?? Can anyone help me with this ??
谁能帮我这个 ?? activity.java
活动.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()
以及我称之为
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.