0

嘿伙计们,我正在尝试从第三方应用程序导出数据,然后在主要活动中将生成的导出文件的路径设置为我的 textview。但它不起作用。我搜索了一遍,得出的结论是它有一些需要做匿名课,但我仍然无法修复它。任何人都可以指导我。

public class MyActivity extends Activity
{

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button btRecieve = (Button) findViewById(R.id.btRecieve);

    final Context context = this.getApplicationContext();

    final TextView tvFilePath = (TextView) findViewById(R.id.tvFilepath);
    final TextView tvFeedBack = (TextView) findViewById(R.id.tvFeedBack);

    final String pDateFrom = "2012-07-01";
    final String pDateTo = "2012-07-06";
    final String pExportType = "e5";
    final String pExportFormat = "csv";

    btRecieve.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TimeRecordingExport exporter = new TimeRecordingExport(pDateFrom,pDateTo,pExportType,pExportFormat,tvFilePath);
            exporter.Export(context);
            String path = exporter.getFilePath();

            tvFilePath.setText(path);


        }
    });
}
}







public class TimeRecordingExport{
   //private variables
   String mDateFrom;
   String mDateTo;
   String mExportType;
   String mExportFormat;

   private String mFilepath;                     //path to the output file
   String feedback;

   TextView mTv;

   File file;

   Context mContext;

   final String KEY_RESULT_FILE = "com.dynamicg.timerecording.FILE";



//Constructor
   public TimeRecordingExport(String pDateFrom,String pDateTo,String pExportType,String pExportFormat,TextView tv){

     //Initialize private variables
     mDateFrom = pDateFrom;
     mDateTo = pDateTo;
     mExportFormat = pExportFormat;
     mExportType = pExportType;
     mTv = tv;

   }    //End constructor

   //Export function
   public void Export(Context pContext){
       mContext = pContext;
       //create a new intent with action export
       Intent intent = new Intent("com.dynamicg.timerecording.DATA_EXPORT");

       //Add extra values or you could say parameters to this intent.
       intent.putExtra("com.dynamicg.timerecording.DATE_FROM",mDateFrom);
       intent.putExtra("com.dynamicg.timerecording.DATE_TO",mDateTo);
       intent.putExtra("com.dynamicg.timerecording.EXPORT_TYPE",mExportType);
       intent.putExtra("com.dynamicg.timerecording.EXPORT_FORMAT",mExportFormat);

       //make a broadcast reciever
       BroadcastReceiver resultReceiver = new BroadcastReceiver() {
           @Override
           public void onReceive(Context context, Intent resultIntent) {

               Bundle bundle = this.getResultExtras(true);
               TimeRecordingExport.this.mFilepath = bundle.getString(KEY_RESULT_FILE);               //Path to the created file
               //mTv.setText(mFilepath[0]);
               file = new File(mFilepath);                                                           //New Created file

               feedback = "File=["+file+"], canRead=["+file.canRead()                                //Info about the created file
                       +"], sizeKB=["+(file.length()/1024)+"]";
               //Toast.makeText(mContext, feedback, Toast.LENGTH_LONG).show();

               Toast.makeText(context, feedback, Toast.LENGTH_LONG).show();
               System.out.println(feedback);
           }
       };
       mContext.sendOrderedBroadcast(intent, null, resultReceiver, null, Activity.RESULT_OK, null, null);
   } //End function export

   public String getFilePath(){

       return mFilepath;
   }

   public String getFileInfo(){
       return feedback;
   }

}   //End of class
4

1 回答 1

0

首先,您是否尝试过为路径值输入一个测试字符串?只是为了确定这不是您的数据?其次,猜想我一直将我的小部件定义为 Activity 的类变量,而不是 onCreate 方法中的最终变量。

于 2012-07-05T23:05:19.687 回答