Android Fragments 从bundle.getString向类Paragraph od droidText传递参数不起作用

0kjbasz6  于 2023-10-19  发布在  Android
关注(0)|答案(2)|浏览(139)

我不知道为什么变量myInt在我的代码中的某些地方是null。
在这些行中,它是确定的,所以variable不是null:

  1. myInt = bundle2.getString("scelta2", null);
  2. titolo3.setText(myInt);

当我在Paragraph p1=new Paragraph(myInt);中使用变量myInt时,它是null。
有什么问题吗?
TwoFragment.java

  1. public class TwoFragment extends Fragment{
  2. private View v;
  3. Intent chooser=null;
  4. String myInt="";
  5. public TwoFragment() {
  6. // Required empty public constructor
  7. }
  8. @Override
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. }
  12. @Override
  13. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  14. Bundle savedInstanceState) {
  15. // Inflate the layout for this fragment
  16. View rootView = inflater.inflate(R.layout.fragment_two, container, false);
  17. Button mButton = (Button) rootView.findViewById(R.id.newbutton);
  18. mButton.setOnClickListener(new View.OnClickListener() {
  19. public void onClick(View v) {
  20. //sendemail();
  21. createPDF();
  22. viewPDF();
  23. }
  24. });
  25. TextView titolo3 = (TextView)rootView.findViewById(R.id.result);
  26. TextView titolo2 = (TextView)rootView.findViewById(R.id.result2);
  27. TextView titolo4 = (TextView)rootView.findViewById(R.id.resultpizze);
  28. //TextView titolo = (TextView)rootView.findViewById(R.id.quantità3);
  29. /* Bundle bundle2=getArguments();
  30. if(bundle2 != null){
  31. String string = bundle2.getString("scelta2", null);
  32. titolo3.setText(string);
  33. }*/
  34. Bundle bundle2=getArguments();
  35. if(bundle2 != null){
  36. myInt = bundle2.getString("scelta2", null);
  37. titolo3.setText(myInt);
  38. }
  39. Bundle bundle3=getArguments();
  40. if(bundle3 != null){
  41. // String myInt3 = bundle3.getString("totalebirre", null);
  42. // cazzo2=Integer.parseInt(myInt3);
  43. int cazzo2=bundle3.getInt("totalebirre");
  44. titolo2.setText(String.valueOf(cazzo2));
  45. }
  46. Bundle bundle=getArguments();
  47. if(bundle != null){
  48. // String myInt2 = bundle2.getString("totalepizze", null);
  49. // cazzo=Integer.parseInt(myInt2);
  50. //titolo2.setText(myInt2);
  51. String string=bundle.getString("scelta3", null);
  52. titolo4.setText(string);
  53. }
  54. return rootView;
  55. }
  56. /* public void sendemail(){
  57. Intent intent = new Intent(Intent.ACTION_SEND);
  58. intent.setData(Uri.parse("mailto:"));
  59. String[] to={"[email protected]"};
  60. intent.putExtra(Intent.EXTRA_EMAIL,to);
  61. intent.putExtra(Intent.EXTRA_SUBJECT, "ciao");
  62. intent.putExtra(Intent.EXTRA_TEXT, "zao");
  63. intent.setType("message/rfc822");
  64. chooser=intent.createChooser(intent,"manda email");
  65. startActivity(chooser);
  66. }*/
  67. public void createPDF() {
  68. Document doc = new Document();
  69. try {
  70. String path = Environment.getExternalStorageDirectory()
  71. .getAbsolutePath() + "/droidText";
  72. File dir = new File(path);
  73. if (!dir.exists())
  74. dir.mkdirs();
  75. Log.d("PDFCreator", "PDF Path: " + path);
  76. File file = new File(dir, "sample.pdf");
  77. FileOutputStream fOut = new FileOutputStream(file);
  78. PdfWriter.getInstance(doc, fOut);
  79. // open the document
  80. doc.open();
  81. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  82. Bitmap bitmap = BitmapFactory.decodeResource(getContext()
  83. .getResources(), R.drawable.androtuto);
  84. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
  85. Image myImg = Image.getInstance(stream.toByteArray());
  86. myImg.setAlignment(Image.MIDDLE);
  87. // add image to document
  88. doc.add(myImg);
  89. Paragraph p1 = new Paragraph(myInt);
  90. Log.d("ciao",myInt);
  91. Font paraFont = new Font(Font.COURIER);
  92. p1.setAlignment(Paragraph.ALIGN_CENTER);
  93. p1.setFont(paraFont);
  94. // add paragraph to document
  95. doc.add(p1);
  96. Paragraph p2 = new Paragraph("Ciao");
  97. Font paraFont2 = new Font(Font.COURIER, 14.0f, Color.GREEN);
  98. p2.setAlignment(Paragraph.ALIGN_CENTER);
  99. p2.setFont(paraFont2);
  100. doc.add(p2);
  101. stream = new ByteArrayOutputStream();
  102. bitmap = BitmapFactory.decodeResource(getContext()
  103. .getResources(), R.drawable.android);
  104. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
  105. myImg = Image.getInstance(stream.toByteArray());
  106. myImg.setAlignment(Image.MIDDLE);
  107. // add image to document
  108. doc.add(myImg);
  109. // set footer
  110. Phrase footerText = new Phrase("Pied de page ");
  111. HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
  112. doc.setFooter(pdfFooter);
  113. } catch (DocumentException de) {
  114. Log.e("PDFCreator", "DocumentException:" + de);
  115. } catch (IOException e) {
  116. Log.e("PDFCreator", "ioException:" + e);
  117. } finally {
  118. doc.close();
  119. }
  120. }
  121. public void viewPDF(){
  122. String path = "/sdcard/droidText/sample.pdf";
  123. File targetFile = new File(path);
  124. Uri targetUri = Uri.fromFile(targetFile);
  125. Intent intent;
  126. intent = new Intent(Intent.ACTION_VIEW);
  127. intent.setDataAndType(targetUri, "application/pdf");
  128. startActivity(intent);
  129. }
  130. }

ThreeFragment.java
(我传递变量的片段)

  1. public class ThreeFragment extends Fragment implements
  2. android.widget.CompoundButton.OnCheckedChangeListener {
  3. ListView lv2;
  4. ArrayList<Planet> planetList;
  5. ListView lv;
  6. ArrayList<Birra> birraList;
  7. BirraAdapter biAdapter;
  8. PlanetAdapter plAdapter;
  9. Planet p;
  10. String myInt="";
  11. PlanetAdapter.PlanetHolder holder;
  12. public ThreeFragment() {
  13. // Required empty public constructor
  14. }
  15. @Override
  16. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  17. Bundle savedInstanceState) {
  18. // Inflate the layout for this fragment
  19. final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_three, container, false);
  20. Button mButton = (Button) rootView.findViewById(R.id.button2);
  21. mButton.setOnClickListener(new View.OnClickListener() {
  22. public void onClick(View v) {
  23. MyListFragment mlf=new MyListFragment();
  24. mlf.showResult(v);
  25. // MyListFragment.showResult(v);
  26. showResult2(v);
  27. }
  28. });
  29. //return inflater.inflate(R.layout.fragment_list2, container, false);
  30. return rootView;
  31. }
  32. @Override
  33. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  34. super.onViewCreated(view, savedInstanceState);
  35. lv2 = (ListView) getView().findViewById(R.id.listview2);
  36. displayBirraList();
  37. }
  38. private void displayBirraList() {
  39. birraList = new ArrayList<Birra>();
  40. birraList.add(new Birra("Paulaner", 6, "€"));
  41. birraList.add(new Birra("Forst", 7, "€"));
  42. birraList.add(new Birra("Peroni", 5, "€"));
  43. birraList.add(new Birra("Corona", 5, "€"));
  44. birraList.add(new Birra("Nastro Azzurro", 4, "€"));
  45. biAdapter = new BirraAdapter(birraList, getContext()) {
  46. @Override
  47. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  48. int pos = lv2.getPositionForView(buttonView);
  49. if (pos != ListView.INVALID_POSITION) {
  50. Birra b = birraList.get(pos);
  51. b.setSelected(isChecked);
  52. /*Toast.makeText(
  53. getActivity(),
  54. "Clicked on Pizza: " + p.getName() + ". State: is "
  55. + isChecked, Toast.LENGTH_SHORT).show();*/
  56. }
  57. }
  58. };
  59. lv2.setAdapter(biAdapter);
  60. }
  61. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  62. /*int pos = lv.getPositionForView(buttonView);
  63. if (pos != ListView.INVALID_POSITION) {
  64. Planet p = planetList.get(pos);
  65. p.setSelected(isChecked);
  66. *//**//**//**//*Toast.makeText(
  67. getActivity(),
  68. "Clicked on Planet: " + p.getName() + ". State: is "
  69. + isChecked, Toast.LENGTH_SHORT).show();*//**//**//**//*
  70. }
  71. */
  72. }
  73. public void showResult2(View v) {
  74. String result = "Selected Product are :";
  75. int totalAmount = 0;
  76. // String a = "";
  77. for (Birra b : biAdapter.getBox()) {
  78. if (b.selected) {
  79. result += "\n" + b.name + " " + b.distance + "€" + "q.tà :" + b.getQuantità();
  80. int quantitaInt = Integer.parseInt(b.getQuantità());
  81. totalAmount += b.distance * quantitaInt;
  82. // a=String.valueOf(totalAmount);
  83. }
  84. }
  85. /* for (Planet p : plAdapter.getBox()) {
  86. if (p.isSelected()) {
  87. result += "\n" + p.getName() + " " + p.getDistance() + "€" + "q.tà :" + p.getQuantità();
  88. int quantitaInt = Integer.parseInt(p.getQuantità());
  89. //totalAmount2+=p.distance * quantitaInt;
  90. //z=String.valueOf(totalAmount2);
  91. }
  92. }*/
  93. Toast.makeText(getActivity(), result + "\n" +myInt + "\n" + "Total Amount:=" + totalAmount + "€", Toast.LENGTH_LONG).show();
  94. Bundle bun2 = new Bundle();
  95. bun2.putString("scelta2", result);
  96. TwoFragment fgsearch2 = new TwoFragment();
  97. fgsearch2.setArguments(bun2);
  98. android.support.v4.app.FragmentTransaction transaction2 = getActivity().getSupportFragmentManager().beginTransaction();
  99. transaction2.replace(R.id.content_main, fgsearch2);
  100. transaction2.commit();
  101. Bundle bun = new Bundle();
  102. // bun.putString("totalebirre", a);
  103. bun.putInt("totalebirre", totalAmount);
  104. TwoFragment fgsearch = new TwoFragment();
  105. fgsearch.setArguments(bun);
  106. android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
  107. transaction.replace(R.id.content_main2, fgsearch);
  108. transaction.commit();
  109. }
  110. }

调用三个片段的Main:

  1. public class MainBirra extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.mainbirra);
  6. getSupportFragmentManager().beginTransaction().
  7. replace(R.id.fragmentContainer2, new ThreeFragment()).commit();
  8. }
  9. /* @Override protected void onDestroy() {
  10. SharedPreferences preferences=getSharedPreferences("states", Context.MODE_PRIVATE);
  11. SharedPreferences.Editor editor=preferences.edit();
  12. editor.clear().apply();
  13. super.onDestroy();
  14. Log.e("OnDestroy", " callback_2");
  15. }*/
  16. }
falq053o

falq053o1#

在我看来,你可以做一个静态方法,返回你想要启动的片段的instance,在这个方法中,你将初始化你的变量,比如TwoFragment中的myInt。因此,在www.example.com中TwoFragment.java添加此方法

  1. public static TwoFragment getInstance(String myInt){
  2. TwoFragment twoFragment=new TwoFragment();
  3. twoFragment.myInt=myInt;
  4. return twoFragment;
  5. }

当你需要在其他地方启动片段时,使用此代码代替默认构造函数

  1. public class ThreeFragment extends Fragment implements
  2. android.widget.CompoundButton.OnCheckedChangeListener {
  3. ListView lv2;
  4. ArrayList<Planet> planetList;
  5. ListView lv;
  6. ArrayList<Birra> birraList;
  7. BirraAdapter biAdapter;
  8. PlanetAdapter plAdapter;
  9. Planet p;
  10. String myInt="";
  11. PlanetAdapter.PlanetHolder holder;
  12. public ThreeFragment() {
  13. // Required empty public constructor
  14. }
  15. @Override
  16. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  17. Bundle savedInstanceState) {
  18. // Inflate the layout for this fragment
  19. final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_three, container, false);
  20. Button mButton = (Button) rootView.findViewById(R.id.button2);
  21. mButton.setOnClickListener(new View.OnClickListener() {
  22. public void onClick(View v) {
  23. MyListFragment mlf=new MyListFragment();
  24. mlf.showResult(v);
  25. // MyListFragment.showResult(v);
  26. showResult2(v);
  27. }
  28. });
  29. //return inflater.inflate(R.layout.fragment_list2, container, false);
  30. return rootView;
  31. }
  32. @Override
  33. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  34. super.onViewCreated(view, savedInstanceState);
  35. lv2 = (ListView) getView().findViewById(R.id.listview2);
  36. displayBirraList();
  37. }
  38. private void displayBirraList() {
  39. birraList = new ArrayList<Birra>();
  40. birraList.add(new Birra("Paulaner", 6, "€"));
  41. birraList.add(new Birra("Forst", 7, "€"));
  42. birraList.add(new Birra("Peroni", 5, "€"));
  43. birraList.add(new Birra("Corona", 5, "€"));
  44. birraList.add(new Birra("Nastro Azzurro", 4, "€"));
  45. biAdapter = new BirraAdapter(birraList, getContext()) {
  46. @Override
  47. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  48. int pos = lv2.getPositionForView(buttonView);
  49. if (pos != ListView.INVALID_POSITION) {
  50. Birra b = birraList.get(pos);
  51. b.setSelected(isChecked);
  52. /*Toast.makeText(
  53. getActivity(),
  54. "Clicked on Pizza: " + p.getName() + ". State: is "
  55. + isChecked, Toast.LENGTH_SHORT).show();*/
  56. }
  57. }
  58. };
  59. lv2.setAdapter(biAdapter);
  60. }
  61. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  62. /*int pos = lv.getPositionForView(buttonView);
  63. if (pos != ListView.INVALID_POSITION) {
  64. Planet p = planetList.get(pos);
  65. p.setSelected(isChecked);
  66. *//**//**//**//*Toast.makeText(
  67. getActivity(),
  68. "Clicked on Planet: " + p.getName() + ". State: is "
  69. + isChecked, Toast.LENGTH_SHORT).show();*//**//**//**//*
  70. }
  71. */
  72. }
  73. public void showResult2(View v) {
  74. String result = "Selected Product are :";
  75. int totalAmount = 0;
  76. // String a = "";
  77. for (Birra b : biAdapter.getBox()) {
  78. if (b.selected) {
  79. result += "\n" + b.name + " " + b.distance + "€" + "q.tà :" + b.getQuantità();
  80. int quantitaInt = Integer.parseInt(b.getQuantità());
  81. totalAmount += b.distance * quantitaInt;
  82. // a=String.valueOf(totalAmount);
  83. }
  84. }
  85. /* for (Planet p : plAdapter.getBox()) {
  86. if (p.isSelected()) {
  87. result += "\n" + p.getName() + " " + p.getDistance() + "€" + "q.tà :" + p.getQuantità();
  88. int quantitaInt = Integer.parseInt(p.getQuantità());
  89. //totalAmount2+=p.distance * quantitaInt;
  90. //z=String.valueOf(totalAmount2);
  91. }
  92. }*/
  93. Toast.makeText(getActivity(), result + "\n" +myInt + "\n" + "Total Amount:=" + totalAmount + "€", Toast.LENGTH_LONG).show();
  94. TwoFragment fgsearch2=TwoFragment.getInstance(result);
  95. fgsearch2.setArguments(bun2);
  96. android.support.v4.app.FragmentTransaction transaction2 = getActivity().getSupportFragmentManager().beginTransaction();
  97. transaction2.replace(R.id.content_main, fgsearch2);
  98. transaction2.commit();
  99. }
  100. }
展开查看全部
alen0pnh

alen0pnh2#

  1. public class TwoFragment extends Fragment {
  2. private View v;
  3. String myInt="";
  4. int cazzo2;
  5. String scelta3;
  6. public TwoFragment() {
  7. // Required empty public constructor
  8. }
  9. public static TwoFragment getInstance(String myInt,int cazzo2,String scelta3){
  10. TwoFragment twoFragment=new TwoFragment();
  11. twoFragment.myInt=myInt;
  12. twoFragment.cazzo2=cazzo2;
  13. twoFragment.scelta3=scelta3;
  14. return twoFragment;
  15. }
  16. @Override
  17. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  18. Bundle savedInstanceState) {
  19. // Inflate the layout for this fragment
  20. View rootView = inflater.inflate(R.layout.fragment_two, container, false);
  21. Button mButton = (Button) rootView.findViewById(R.id.newbutton);
  22. mButton.setOnClickListener(new View.OnClickListener() {
  23. public void onClick(View v) {
  24. //sendemail();
  25. createPDF();
  26. viewPDF();
  27. }
  28. });
  29. TextView titolo3 = (TextView)rootView.findViewById(R.id.result);
  30. TextView titolo2 = (TextView)rootView.findViewById(R.id.result2);
  31. TextView titolo4 = (TextView)rootView.findViewById(R.id.resultpizze);
  32. titolo3.setText(myInt);
  33. titolo2.setText(String.valueOf(cazzo2));
  34. titolo4.setText(scelta3);
  35. return rootView;
  36. }
  37. /* public void sendemail(){
  38. Intent intent = new Intent(Intent.ACTION_SEND);
  39. intent.setData(Uri.parse("mailto:"));
  40. String[] to={"[email protected]"};
  41. intent.putExtra(Intent.EXTRA_EMAIL,to);
  42. intent.putExtra(Intent.EXTRA_SUBJECT, "ciao");
  43. intent.putExtra(Intent.EXTRA_TEXT, "zao");
  44. intent.setType("message/rfc822");
  45. chooser=intent.createChooser(intent,"manda email");
  46. startActivity(chooser);
  47. }*/
  48. public void createPDF() {
  49. Document doc = new Document();
  50. try {
  51. String path = Environment.getExternalStorageDirectory()
  52. .getAbsolutePath() + "/droidText";
  53. File dir = new File(path);
  54. if (!dir.exists())
  55. dir.mkdirs();
  56. Log.d("PDFCreator", "PDF Path: " + path);
  57. File file = new File(dir, "sample.pdf");
  58. FileOutputStream fOut = new FileOutputStream(file);
  59. PdfWriter.getInstance(doc, fOut);
  60. // open the document
  61. doc.open();
  62. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  63. Bitmap bitmap = BitmapFactory.decodeResource(getContext()
  64. .getResources(), R.drawable.androtuto);
  65. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
  66. Image myImg = Image.getInstance(stream.toByteArray());
  67. myImg.setAlignment(Image.MIDDLE);
  68. // add image to document
  69. doc.add(myImg);
  70. Paragraph p1 = new Paragraph(myInt);
  71. Log.d("ciao",myInt);
  72. Font paraFont = new Font(Font.COURIER);
  73. p1.setAlignment(Paragraph.ALIGN_CENTER);
  74. p1.setFont(paraFont);
  75. // add paragraph to document
  76. doc.add(p1);
  77. Paragraph p2 = new Paragraph("Ciao");
  78. Font paraFont2 = new Font(Font.COURIER, 14.0f, Color.GREEN);
  79. p2.setAlignment(Paragraph.ALIGN_CENTER);
  80. p2.setFont(paraFont2);
  81. doc.add(p2);
  82. stream = new ByteArrayOutputStream();
  83. bitmap = BitmapFactory.decodeResource(getContext()
  84. .getResources(), R.drawable.android);
  85. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
  86. myImg = Image.getInstance(stream.toByteArray());
  87. myImg.setAlignment(Image.MIDDLE);
  88. // add image to document
  89. doc.add(myImg);
  90. // set footer
  91. Phrase footerText = new Phrase("Pied de page ");
  92. HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
  93. doc.setFooter(pdfFooter);
  94. } catch (DocumentException de) {
  95. Log.e("PDFCreator", "DocumentException:" + de);
  96. } catch (IOException e) {
  97. Log.e("PDFCreator", "ioException:" + e);
  98. } finally {
  99. doc.close();
  100. }
  101. }
  102. public void viewPDF(){
  103. String path = "/sdcard/droidText/sample.pdf";
  104. File targetFile = new File(path);
  105. Uri targetUri = Uri.fromFile(targetFile);
  106. Intent intent;
  107. intent = new Intent(Intent.ACTION_VIEW);
  108. intent.setDataAndType(targetUri, "application/pdf");
  109. startActivity(intent);
  110. }
  111. }

其他片段

  1. public class ThreeFragment extends Fragment implements
  2. android.widget.CompoundButton.OnCheckedChangeListener {
  3. ListView lv2;
  4. ArrayList<Planet> planetList;
  5. ListView lv;
  6. ArrayList<Birra> birraList;
  7. BirraAdapter biAdapter;
  8. PlanetAdapter plAdapter;
  9. Planet p;
  10. String myInt="";
  11. PlanetAdapter.PlanetHolder holder;
  12. public ThreeFragment() {
  13. // Required empty public constructor
  14. }
  15. @Override
  16. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  17. Bundle savedInstanceState) {
  18. // Inflate the layout for this fragment
  19. final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_three, container, false);
  20. Button mButton = (Button) rootView.findViewById(R.id.button2);
  21. mButton.setOnClickListener(new View.OnClickListener() {
  22. public void onClick(View v) {
  23. MyListFragment mlf=new MyListFragment();
  24. mlf.showResult(v);
  25. // MyListFragment.showResult(v);
  26. showResult2(v);
  27. }
  28. });
  29. //return inflater.inflate(R.layout.fragment_list2, container, false);
  30. return rootView;
  31. }
  32. @Override
  33. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  34. super.onViewCreated(view, savedInstanceState);
  35. lv2 = (ListView) getView().findViewById(R.id.listview2);
  36. displayBirraList();
  37. }
  38. private void displayBirraList() {
  39. birraList = new ArrayList<Birra>();
  40. birraList.add(new Birra("Paulaner", 6, "€"));
  41. birraList.add(new Birra("Forst", 7, "€"));
  42. birraList.add(new Birra("Peroni", 5, "€"));
  43. birraList.add(new Birra("Corona", 5, "€"));
  44. birraList.add(new Birra("Nastro Azzurro", 4, "€"));
  45. biAdapter = new BirraAdapter(birraList, getContext()) {
  46. @Override
  47. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  48. int pos = lv2.getPositionForView(buttonView);
  49. if (pos != ListView.INVALID_POSITION) {
  50. Birra b = birraList.get(pos);
  51. b.setSelected(isChecked);
  52. /*Toast.makeText(
  53. getActivity(),
  54. "Clicked on Pizza: " + p.getName() + ". State: is "
  55. + isChecked, Toast.LENGTH_SHORT).show();*/
  56. }
  57. }
  58. };
  59. lv2.setAdapter(biAdapter);
  60. }
  61. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  62. /*int pos = lv.getPositionForView(buttonView);
  63. if (pos != ListView.INVALID_POSITION) {
  64. Planet p = planetList.get(pos);
  65. p.setSelected(isChecked);
  66. *//**//**//**//*Toast.makeText(
  67. getActivity(),
  68. "Clicked on Planet: " + p.getName() + ". State: is "
  69. + isChecked, Toast.LENGTH_SHORT).show();*//**//**//**//*
  70. }
  71. */
  72. }
  73. public void showResult2(View v) {
  74. String result = "Selected Product are :";
  75. int totalAmount = 0;
  76. // String a = "";
  77. for (Birra b : biAdapter.getBox()) {
  78. if (b.selected) {
  79. result += "\n" + b.name + " " + b.distance + "€" + "q.tà :" + b.getQuantità();
  80. int quantitaInt = Integer.parseInt(b.getQuantità());
  81. totalAmount += b.distance * quantitaInt;
  82. // a=String.valueOf(totalAmount);
  83. }
  84. }
  85. /* for (Planet p : plAdapter.getBox()) {
  86. if (p.isSelected()) {
  87. result += "\n" + p.getName() + " " + p.getDistance() + "€" + "q.tà :" + p.getQuantità();
  88. int quantitaInt = Integer.parseInt(p.getQuantità());
  89. //totalAmount2+=p.distance * quantitaInt;
  90. //z=String.valueOf(totalAmount2);
  91. }
  92. }*/
  93. Toast.makeText(getActivity(), result + "\n" +myInt + "\n" + "Total Amount:=" + totalAmount + "€", Toast.LENGTH_LONG).show();
  94. TwoFragment fgsearch2 = TwoFragment.getInstance(result,totalAmount,"test");
  95. android.support.v4.app.FragmentTransaction transaction2 = getActivity().getSupportFragmentManager().beginTransaction();
  96. transaction2.replace(R.id.content_main, fgsearch2);
  97. transaction2.commit();
  98. }
  99. }
展开查看全部

相关问题