简体   繁体   English

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

[英]Setting outer class variable from anonymous inner class

Hey guys i am trying to export data from a third party app and then set the path of resulting exported file to my textview in main activity.But it isn't working.I searched all over and came to a conclusion that it has something to do with anonymous class,but i am still not able to fix it.Can anyone please guide me. 大家好,我试图从第三方应用程序导出数据,然后将导出的文件的路径设置为主要活动中的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

First, have you tried putting a test string in for the path value? 首先,您是否尝试过将测试字符串放入路径值? Just to be sure it's not your data? 只是确定不是您的数据? Second, guess I've always defined my widgets as class variables of the Activity instead of final variables in the onCreate method. 其次,猜想我一直将小部件定义为Activity的类变量,而不是onCreate方法中的最终变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM