从edittext获取数据到我的create pdf文件

时间:2014-03-14 10:08:04

标签: java android eclipse

我正在尝试从我的edittext中获取一个片段中的数据,将片段nr2转换为我的create pdf,但我需要一些帮助:)

片段1中的

,我试图保存编辑文本,但我不知道在片段2中要做什么来获取信息,并将其放入代码底部的pdf创建中。

片段1

import android.os.Bundle;
import android.support.v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;


public class Windows extends Fragment implements View.OnClickListener{

Spinner spinner2;
EditText edit2;
Button button1;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle    savedInstanceState) {

      View v = inflater.inflate(R.layout.windows_frag, container, false);

    edit2=(EditText) v.findViewById(R.id.editText2);
    button1 = (Button) v.findViewById(R.id.button1);

    button1.setOnClickListener((OnClickListener) this);
    loadSavedPreferences();



    spinner2 = (Spinner) v.findViewById(R.id.spinner2);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this.getActivity(), R.array.hall_wallus, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
    spinner2.setAdapter(adapter2); // Apply the adapter to the spinner
    return v;




    }

private void loadSavedPreferences() {
    // TODO Auto-generated method stub

}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

}



}

这是我的fragment2

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;



import com.itextpdf.text.Anchor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class Androidtest extends Fragment implements OnItemSelectedListener{

Spinner spinner, spinner1, spinner2;
Button pdfButton;
EditText edit;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      View v = inflater.inflate(R.layout.android_frag, container, false);
      pdfButton=(Button) v.findViewById(R.id.pdfButton);
        edit=(EditText) v.findViewById(R.id.editText1);
        //edit2=(EditText) v.findViewById(R.id.editText2);


    spinner = (Spinner) v.findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(), R.array.hall_wallus, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
    spinner.setAdapter(adapter); // Apply the adapter to the spinner


      pdfButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                generatePDF();


            }

        }); return v;

    }


    protected void generatePDF() {
        // TODO Auto-generated method stub
        if(edit.getText().toString().length()==0)
            return;

         String FILE = Environment.getExternalStorageDirectory( )+"/"+edit.getText().toString()+".pdf";

        File f=new File(FILE);
        if(!(f.exists()))
        {
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.e("FileCreated", "filsesss");
            }
        }
        try
        {
            //Document document = new Document();
            Document document = new Document(PageSize.A4, 20, 20, 20, 20);
            PdfWriter.getInstance(document, new FileOutputStream(FILE));
            document.open();

            addContent(document);
            //createImage();
            document.close();
            Log.e("Pdfmssgggg", "PDG created");
            Toast.makeText(Androidtest.this.getActivity(), "PDF Oprettet ", Toast.LENGTH_SHORT).show();
        }
        catch (Exception e)
        {
            Log.e("Errorrr", "fejl");
            //Log.e("Your description here", e;
            Toast.makeText(Androidtest.this.getActivity(), "Changed to page ", Toast.LENGTH_SHORT).show();
            //Toast.makeText(getApplicationContext(), "Error---in Pdf Created",Toast.LENGTH_LONG).show();
        }


    }


    public void onNothingSelected(AdapterView<?> parent) {

        Toast.makeText(Androidtest.this.getActivity(), "Changed to page ", Toast.LENGTH_SHORT).show();

    }

    /** Helper Functions */
    void showToast(CharSequence msg) {

        Toast.makeText(Androidtest.this.getActivity(), msg, Toast.LENGTH_SHORT).show();
    }





    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {


    }



    private  void addContent(Document document) throws DocumentException {
        Anchor anchor = new Anchor("ESTIMATING APP");
        anchor.setName("ESTIMATING APP");

        PdfPTable table = new PdfPTable(3);

        table.addCell("A");
        PdfPCell cell = new PdfPCell(new Paragraph("B"));
        cell.setColspan(2);
        table.addCell(cell);

        table.addCell("Spinner 1 value");
        table.addCell(spinner.getSelectedItem().toString());
        table.addCell("E");

        table.addCell("Spinner 2 value");
        table.addCell("Spinner 2 value");
        //table.addCell(spinner2.getSelectedItem().toString());
    //  table.addCell(edit2.getText().toString());

        document.add(table);
        document.close();



  }


}

1 个答案:

答案 0 :(得分:0)

您应该在第一个Interface中创建一个Fragment,以便将数据发送回Activity。在Eclipse中使用create对话框创建Fragment时,默认情况下会创建一个示例接口。

您的Activity应该同时拥有Fragments。当您将数据发送回Activity时,您同时拥有数据和第二个Fragment。然后,您可以在第二个Fragment中创建公共方法并发送数据。

或者更简单地说,您可以使用消息传递框架Otto https://github.com/square/otto