when i click on button, that it is into FourFragment
; when i click this button i want that checkbox selected into FirstFragment
(in my way MyListFRagment
) and checkbox selected into ThreeFragment
, are passing into PDF....but with my code PDF not contains variables...why? I hope that you can help me!
Thanks in advance!
First Fragment:
public class MyListFragment extends Fragment implements
android.widget.CompoundButton.OnCheckedChangeListener {
ListView lv;
ArrayList<Planet> planetList;
static PlanetAdapter plAdapter;
BirraAdapter biAdapter;
PlanetAdapter.PlanetHolder holder;
private static Context context = null;
private static FragmentActivity mInstance;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//super.onDestroy();
SharedPreferences settings = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
context = getActivity();
mInstance= getActivity();
Button mButton = (Button) rootView.findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showResult(v);
}
});
//return inflater.inflate(R.layout.fragment_list2, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lv = (ListView)getView().findViewById(R.id.listview);
displayPlanetList();
}
private void displayPlanetList() {
planetList = new ArrayList<Planet>();
planetList.add(new Planet("Margherita", 6, "€",1));
planetList.add(new Planet("Diavola", 7,"€",2));
planetList.add(new Planet("Bufalina", 5,"€",3));
planetList.add(new Planet("Marinara", 5, "€",4));
planetList.add(new Planet("Viennese", 4, "€", 5));
plAdapter = new PlanetAdapter(planetList, getContext()) {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = lv.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
Planet p = planetList.get(pos);
p.setSelected(isChecked);
/*Toast.makeText(
getActivity(),
"Clicked on Pizza: " + p.getName() + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();*/
}
}
};
lv.setAdapter(plAdapter);
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
/*int pos = lv.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
Planet p = planetList.get(pos);
p.setSelected(isChecked);
*//*Toast.makeText(
getActivity(),
"Clicked on Planet: " + p.getName() + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();*//*
}*/
}
public static void showResult(View v) {
String result = "Selected Product are :";
int totalAmount=0;
String result2 = "";
int totalAmount2=0;
String a="";
String z="";
for (Planet p : plAdapter.getBox()) {
if (p.isSelected()){
result += "\n" + p.getName()+" "+p.getDistance()+"€"+"q.tà :"+p.getQuantità();
int quantitaInt= Integer.parseInt(p.getQuantità() );
totalAmount2+=p.getDistance() * quantitaInt;
z=String.valueOf(totalAmount2);
}
}
Toast.makeText(context, result + "\n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();
//Toast.makeText(getActivity(), result2 + "\n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();
Bundle bun = new Bundle();
bun.putString("scelta4", result);
bun.putString("scelta5", z);
FourFragment fgsearch = new FourFragment();
fgsearch.setArguments(bun);
android.support.v4.app.FragmentTransaction transaction = mInstance.getSupportFragmentManager().beginTransaction();
//transaction.replace(R.id.content_main, fgsearch);
//transaction.replace(R.id.content_main, fgsearch);
transaction.commit();
/*Bundle bun = new Bundle();
// bun.putString("totalepizze",z);
bun.putInt("totalepizze",totalAmount2);
TwoFragment fgsearch = new TwoFragment();
fgsearch.setArguments(bun);
android.support.v4.app.FragmentTransaction transaction = mInstance.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_main2, fgsearch);
transaction.commit();*/
/*
Bundle bun = new Bundle();
bun.putString("scelta", result2);
TwoFragment fgsearch = new TwoFragment();
fgsearch.setArguments(bun);
android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_main2, fgsearch);
transaction.commit();
*/
}
}
Three Fragment:
public class ThreeFragment extends Fragment implements
android.widget.CompoundButton.OnCheckedChangeListener {
ListView lv2;
ArrayList<Planet> planetList;
ListView lv;
ArrayList<Birra> birraList;
static BirraAdapter biAdapter;
PlanetAdapter plAdapter;
Planet p;
static String myInt="";
private static Context context = null;
private static FragmentActivity mInstance;
PlanetAdapter.PlanetHolder holder;
public ThreeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_three, container, false);
context = getActivity();
mInstance= getActivity();
Button mButton = (Button) rootView.findViewById(R.id.button2);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MyListFragment mlf=new MyListFragment();
mlf.showResult(v);
// MyListFragment.showResult(v);
showResult2(v);
}
});
//return inflater.inflate(R.layout.fragment_list2, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lv2 = (ListView) getView().findViewById(R.id.listview2);
displayBirraList();
}
private void displayBirraList() {
birraList = new ArrayList<Birra>();
birraList.add(new Birra("Paulaner", 6, "€"));
birraList.add(new Birra("Forst", 7, "€"));
birraList.add(new Birra("Peroni", 5, "€"));
birraList.add(new Birra("Corona", 5, "€"));
birraList.add(new Birra("Nastro Azzurro", 4, "€"));
biAdapter = new BirraAdapter(birraList, getContext()) {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = lv2.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
Birra b = birraList.get(pos);
b.setSelected(isChecked);
/*Toast.makeText(
getActivity(),
"Clicked on Pizza: " + p.getName() + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();*/
}
}
};
lv2.setAdapter(biAdapter);
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
/*int pos = lv.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
Planet p = planetList.get(pos);
p.setSelected(isChecked);
*//**//**//**//*Toast.makeText(
getActivity(),
"Clicked on Planet: " + p.getName() + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();*//**//**//**//*
}
*/
}
public static void showResult2(View v) {
String result = "Selected Product are :";
String a="";
int totalAmount = 0;
// String a = "";
for (Birra b : biAdapter.getBox()) {
if (b.selected) {
result += "\n" + b.name + " " + b.distance + "€" + "q.tà :" + b.getQuantità();
try {
int quantitaInt = Integer.parseInt(b.getQuantità());
totalAmount += b.distance * quantitaInt;
a=String.valueOf(totalAmount);
Bundle bun2 = new Bundle();
bun2.putString("scelta2", result);
bun2.putString("scelta3", a);
FourFragment fgsearch2 = new FourFragment();
fgsearch2.setArguments(bun2);
//android.support.v4.app.FragmentTransaction transaction2 = getActivity().getSupportFragmentManager().beginTransaction();
android.support.v4.app.FragmentTransaction transaction2 = mInstance.getSupportFragmentManager().beginTransaction();
// transaction2.replace(R.id.content_main2, fgsearch2);
// transaction2.replace(R.id.content_main2, fgsearch2);
transaction2.commit();
}catch(NumberFormatException exc){
System.out.println(exc);
Toast.makeText(context, "inserisci una quantita' a \""+b.name, Toast.LENGTH_LONG).show();
}
}
}
Toast.makeText(context, result + "\n" +myInt + "\n" + "Total Amount:=" + totalAmount + "€", Toast.LENGTH_LONG).show();
}
}
Four Fragment:
public class FourFragment extends android.support.v4.app.Fragment {
private View v;
Intent chooser = null;
String myInt = "";
String ciao = "";
private String string = "";
private String string3 = "";
private String string4 = "";
private String string5 = "";
private ProgressDialog processingDialog;
private MakePDF mProgressbarAsync;
// private ProgressBar pdfProgress;
private int mProgressStatus = 0;
public FourFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
processingDialog = new ProgressDialog(getContext());
processingDialog.setCancelable(true);
processingDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
processingDialog.setMessage("Work in Progress ...");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_four, container, false);
Button mButton = (Button) rootView.findViewById(R.id.cazzo);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MyListFragment mlf=new MyListFragment();
mlf.showResult(v);
ThreeFragment tf=new ThreeFragment();
tf.showResult2(v);
processingDialog.show();
mProgressbarAsync=new MakePDF();
mProgressbarAsync.execute();
// new MakePDF(getContext()).execute();
}
});
//TextView titolo3 = (TextView) rootView.findViewById(R.id.result);
TextView titolo2 = (TextView) rootView.findViewById(R.id.result2);
// TextView titolo4 = (TextView) rootView.findViewById(R.id.resultpizze);
// TextView titolo5 = (TextView) rootView.findViewById(R.id.result2);
// TextView titolo6 = (TextView) rootView.findViewById(R.id.resultbirre);
//pdfProgress = (ProgressBar) rootView.findViewById(R.id.progressbar);
// pdfProgress.setVisibility(View.GONE);
Bundle bundle = getArguments();
if (bundle != null) {
string = bundle.getString("scelta4");
// titolo3.setText(string);
string3 = bundle.getString("scelta5");
// titolo4.setText(string3);
}
Bundle bundle2 = getArguments();
if (bundle2 != null) {
string4 = bundle2.getString("scelta2");
// titolo5.setText(string4);
string5 = bundle2.getString("scelta3");
// titolo6.setText(string5);
}
return rootView;
}
public void createPDF() {
Document doc = new Document();
try {
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/droidText";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
Log.d("PDFCreator", "PDF Path: " + path);
//File file = new File(dir, "sample.pdf");
File file = new File(dir, "salve.pdf");
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter.getInstance(doc, fOut);
// open the document
doc.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getContext()
.getResources(), R.drawable.androtuto);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Image myImg = Image.getInstance(stream.toByteArray());
myImg.setAlignment(Image.MIDDLE);
// add image to document
doc.add(myImg);
Paragraph p1 = new Paragraph(string+string3);
Font paraFont = new Font(Font.COURIER);
p1.setAlignment(Paragraph.ALIGN_CENTER);
p1.setFont(paraFont);
// add paragraph to document
doc.add(p1);
Paragraph p2 = new Paragraph(string4+string5);
Font paraFont2 = new Font(Font.COURIER, 14.0f, Color.GREEN);
p2.setAlignment(Paragraph.ALIGN_CENTER);
p2.setFont(paraFont2);
doc.add(p2);
/* Paragraph p3 = new Paragraph("TOTALE: "+tot);
Font paraFont3 = new Font(Font.COURIER, 14.0f, Color.GREEN);
p3.setAlignment(Paragraph.ALIGN_CENTER);
p3.setFont(paraFont3);
doc.add(p3);*/
stream = new ByteArrayOutputStream();
bitmap = BitmapFactory.decodeResource(getContext()
.getResources(), R.drawable.android);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
myImg = Image.getInstance(stream.toByteArray());
myImg.setAlignment(Image.MIDDLE);
// add image to document
doc.add(myImg);
// set footer
Phrase footerText = new Phrase("Pied de page ");
HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
doc.setFooter(pdfFooter);
} catch (DocumentException de) {
// Log.e("PDFCreator", "DocumentException:" + de);
Log.e("PDFCreator", "DocumentException:" + de.getMessage());
} catch (IOException e) {
// Log.e("PDFCreator", "ioException:" + e);
Log.e("PDFCreator", "DocumentException:" + e.getMessage());
} finally {
doc.close();
}
}
public void viewPDF() {
String path = "/sdcard/droidText/salve.pdf";
File targetFile = new File(path);
Uri targetUri = Uri.fromFile(targetFile);
Intent intent;
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri, "application/pdf");
startActivity(intent);
}
private class MakePDF extends AsyncTask<Void, Integer, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressStatus = 0;
}
/** This callback method is invoked on calling execute() method
* on an instance of this class */
@Override
protected Void doInBackground(Void...params) {
while(mProgressStatus<100){
try{
mProgressStatus++;
/** Invokes the callback method onProgressUpdate */
publishProgress(mProgressStatus);
/** Sleeps this thread for 100ms */
Thread.sleep(100);
}catch(Exception e){
Log.d("Exception", e.toString());
}
}
createPDF();
return null;
}
/** This callback method is invoked when publishProgress()
* method is called */
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
processingDialog.setProgress(mProgressStatus);
}
/** This callback method is invoked when the background function
* doInBackground() is executed completely */
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
processingDialog.dismiss();
viewPDF();
}
}
}
PS: Toast works when i click on button that stay into FOUR FRAGMENT
, while passing parameter into PDF no....
Aucun commentaire:
Enregistrer un commentaire