agradecería vuestra ayuda para poder pasar información de un Activity a un Fragmen.
Estoy intentando pasar la ruta y en nombre de un archivo "pdf" a un fragmen para que este sea mostrado en pantalla.
Primero muestro la lista de archivos que contiene la carpeta, y desde aqui selecciono un archivo pdf para abrirlo.
Cg_A2.java extiende de Fragment
public class Cg_A2 extends Fragment {
public Cg_A2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate( R.layout.cg_a2, container, false );
final ListView lv= view.findViewById(R.id.lv);
lv.setAdapter(new PDF_CustomAdapter(getActivity(),getPDFs()));
return view;
}
private ArrayList<PDFDoc> getPDFs(){
ArrayList<PDFDoc> pdfDocs=new ArrayList<>();
File downloadsFolder = new File( Environment.getExternalStorageDirectory()+"/Documentos PDF/");
PDFDoc pdfDoc;
if(downloadsFolder.exists())
{
//GET ALL FILES IN DOWNLOAD FOLDER
File[] files=downloadsFolder.listFiles();
Arrays.sort(files);
//LOOP THRU THOSE FILES GETTING NAME AND URI
for (int i=0;i<files.length;i++)
{
File file=files[i];
if(file.getPath().endsWith("pdf"))
{
pdfDoc=new PDFDoc();
pdfDoc.setName(file.getName());
pdfDoc.setPath(file.getAbsolutePath());
pdfDocs.add(pdfDoc);
}
}
}
return pdfDocs;
}
}
PDF_CustomAdapter,java que extiende de BaseAdapter
public class PDF_CustomAdapter extends BaseAdapter {
Context c;
ArrayList<PDFDoc> pdfDocs;
FragmentManager fragment;
public PDF_CustomAdapter(Context c, ArrayList<PDFDoc> pdfDocs) {
this.c = c;
this.pdfDocs = pdfDocs;
}
@Override
public int getCount() {
return pdfDocs.size();
}
@Override
public Object getItem(int i) {
return pdfDocs.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view==null)
{
//INFLATE CUSTOM LAYOUT
view= LayoutInflater.from(c).inflate(R.layout.pdf_model,viewGroup,false);
}
final PDFDoc pdfDoc= (PDFDoc) this.getItem(i);
TextView nameTxt= (TextView) view.findViewById(R.id.nameTxt);
ImageView img= (ImageView) view.findViewById(R.id.pdfImage);
//BIND DATA
nameTxt.setText(pdfDoc.getName());
img.setImageResource(R.drawable.pdf_icon);
//VIEW ITEM CLICK
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openPDFView(pdfDoc.getPath());
}
});
return view;
}
//OPEN PDF VIEW
private void openPDFView(String path)
{
// Intent i=new Intent(c, PDF_Activity.class);
// i.putExtra("PATH",path);
// c.startActivity(i);
Fragment f = new PDF_Activity_2();
Bundle args = new Bundle();
args.putSerializable("PATH",path);
f.setArguments(args);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.pdfView,f).addToBackStack(null)
.commit();
}
}
Fragment.java
public class PDF_Activity_2 extends Fragment {
String path;
public PDF_Activity_2() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
path = (String) getArguments().getSerializable("PATH");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.pdf_activity_pdf_2, container, false);
PDFView pdfView= rootView.findViewById(R.id.pdfView);
//UNPACK OUR DATA FROM INTENT
// Intent i=this.getIntent();
// String path=i.getExtras().getString("PATH");
// String path = getArguments().getString("PATH");
//GET THE PDF_1 FILE
File file=new File(path);
if(file.canRead())
{
//LOAD IT
pdfView.fromFile(file).defaultPage(1).onLoad(new OnLoadCompleteListener() {
@Override
public void loadComplete(int nbPages) {
Toast.makeText(getActivity(), String.valueOf(nbPages), Toast.LENGTH_LONG).show();
}
}).load();
}
return rootView;
}
}
pdf_activity_pdf.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.darwindeveloper.horizontalscrollmenu.PDF_Activity">
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
PDFDoc.java
public class PDFDoc {
String name,path;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
Como digo si alguien me puede echar una mano a resolver este problema, se lo agradecería.