从匿名内部类设置外部类变量

时间:2012-07-05 22:17:45

标签: java android

嘿伙计们我正在尝试从第三方应用程序导出数据,然后将生成的导出文件的路径设置为主要活动中的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

1 个答案:

答案 0 :(得分:0)

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