0

サードパーティのアプリからデータをエクスポートしようとしていて、エクスポートされたファイルのパスをメイン アクティビティのテキストビューに設定しようとしていますが、うまくいきません。匿名クラスを使用しますが、まだ修正できません。誰か教えてください。

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

まず、パス値にテスト文字列を入れてみましたか? それがあなたのデータではないことを確認するためだけですか?次に、ウィジェットを onCreate メソッドの final 変数ではなく、Activity のクラス変数として常に定義してきたと思います。

于 2012-07-05T23:05:19.687 に答える