如何将值从非活动传递到片段?

时间:2016-04-20 14:06:08

标签: android

我想将Activity中的值传递给Fragment,但我的Activity没有布局。 我使用了条形码扫描仪,因此我想将扫描数据发送到Fragment类,但我收到空值错误,所以请帮帮我。

public class ScanCasepaperActivity extends AppCompatActivity implements ZBarScannerView.ResultHandler,DiagnosisFragment.OnFragmentInteractionListener {
    private ZBarScannerView mScannerView;
    private String TAG = ScanCasepaperActivity.class.getName();
    private static ScanCasepaperActivity context;
    private TextView mCasePaperNo, mDateTime, mpatientName, mgender, mage, mrefBy;
    private int CASEPAPERID;
    DiagnosisFragment diagnosisFragment;

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);

        // Programmatically initialize the scanner view
        mScannerView = new ZBarScannerView(this);

        setContentView(mScannerView);
        //DiagnosisFragment DiagnosisFragment = new DiagnosisFragment(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume
        mScannerView.setAutoFocus(true);
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause
    }

    @Override
    public void handleResult(Result rawResult) {
        Log.e(TAG, "handleResult");
        DiagnosisFragment diagnosisFragment = new DiagnosisFragment();
        Table_Barcode_Methods barcodeMethods = new Table_Barcode_Methods(getApplicationContext());

        // Do something with the result here
        long bar = Long.parseLong(rawResult.getContents());
        int id = 0;
        id = barcodeMethods.ISBarCodeNoAvailable(bar);
        int s1 = 0;
        s1 = (int) Long.parseLong(String.valueOf(id));
        showFragment(s1);

        //tableNewCasePaperMethods.getbar(s1);
        // DiagnosisFragment.(s1);
        // android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
        // fragmentManager.beginTransaction().replace(R.id.content1, diagnosisFragment).commit();

        // DiagnosisFragment.newInstance(args);
        // diagnosisFragment = new DiagnosisFragment();
        // DiagnosisFragment.newInstance(s1);
        // Bundle bundle = new Bundle();
        // bundle.putInt("id",s1);
        // diagnosisFragment.setArguments(bundle);
        // DiagnosisFragment.UpDateUI(s1);

        Log.e(TAG + "barcodescanid", String.valueOf(id));
        Log.e(TAG + "Contents", String.valueOf(bar)); // Prints scan results
        Intent i1=new Intent(ScanCasepaperActivity.this,HomeActivity.class);
        startActivity(i1);
    }
}

1 个答案:

答案 0 :(得分:0)

如果您希望您的活动显示在ScanCasepaperActivity中,只需在您的片段中添加一个方法并在您的活动中调用它。如果您想在HomeActivity中显示片段,请通过Intent或SharedPrefrece传递结果。