简体   繁体   English

在onActivityResult片段上TextView不能setText

[英]TextView not setText on onActivityResult Fragment

This is my code to scan barcode and set content to TextView : 这是我的代码,用于扫描条形码并将内容设置为TextView

public class scanCodeFragment extends android.support.v4.app.Fragment implements View.OnClickListener{

  Button btnScan;
  TextView contentTxt;
  String substrCode, scanContent;
  View v;

  public scanCodeFragment(){
  }

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

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    v = inflater.inflate(R.layout.fragment_scan_code, container, false);
    btnScan = (Button) v.findViewById(R.id.btnScan);
    contentTxt = (TextView) v.findViewById(R.id.scan_content);

    btnScan.setOnClickListener(this);

    return v;
  }

  public void onClick(View v){
    if (v.getId() == R.id.btnScan){
        IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
        scanIntegrator.initiateScan();
    }
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (scanningResult != null) {
        scanContent = scanningResult.getContents();
        substrCode = scanContent.substring(2, 10);
        contentTxt.setText("NIS: " + substrCode);
    }
    else {
        Toast toast = Toast.makeText(getActivity(), "Kode barcode salah", Toast.LENGTH_SHORT);
        toast.show();
    }
  }
}

In onActivityResult method , I can't get the result after scan the barcode code. onActivityResult方法中,扫描条形码后无法获得结果。 Before this code fragment, I can get the result from onActivityResult. 在此代码片段之前,我可以从onActivityResult获取结果。

i don't know what your initiateScan() so keep it if you need it 我不知道您的initialScan()是什么,所以如果需要可以保留它

final int requestCode = 100;

public void onClick(View v){
    if (v.getId() == R.id.btnScan){
      IntentIntegrator intent = new IntentIntegrator(getContext(), MainActivity.class);
            intent.putExtra(RECIPE_EXTRA, extraObject);
            startActivityForResult(intent, requestCode);
    }
}

From the code snippet you have provided, I suggest you to create a separate Activity for scanning, instead of a Class file, in your case which is IntentIntegrator , and start it when your Scan Button is clicked. 根据您提供的代码片段,建议您创建一个单独的Activity进行扫描,而不是Class文件(对于IntentIntegrator而言是Class文件),并在单击“扫描” Button时启动它。

So, create IntentIntegratorActivity and start it like this. 因此,创建IntentIntegratorActivity并像这样启动它。

public void onClick(View v){
    if (v.getId() == R.id.btnScan){
        IntentIntegratorActivity intent = new Intent(getActivity(), IntentIntegratorActivity.class);
        startActivityForResult(intent, SCAN_INTENT_CODE);//Remember this code, which is an Integer value
    }
}

Then, in OnCreate method of IntentIntegratorActivity , do the scanning and return the scan data via an Intent . 然后,在IntentIntegratorActivity OnCreate方法中,执行扫描并通过Intent返回扫描数据。

Intent returnIntent = new Intent();
returnIntent.putExtra("SCAN_DATA_KEY", scanData);//Send scan data from here and remember the key SCAN_DATA_KEY
setResult(RESULT_OK, returnIntent);
finish();

Then this data should be caught inside onActivityResult method in the Fragment Class . 然后,应该在Fragment Class onActivityResult方法中捕获此数据。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case ConstList.SCAN_INTENT_CODE:
        String scanData = data.getStringExtra("SCAN_DATA_KEY");//Use this key to get the data. 
        //I have assumed that the data is String
        contentTxt.setText("NIS: " + scanData);//Set data to the Text field
        break;     
    }
}

Try to change this line IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity()); 尝试更改此行IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity()); to this IntentIntegrator scanIntegrator = new IntentIntegrator(this); 到此IntentIntegrator scanIntegrator = new IntentIntegrator(this); .

The reason is that if you pass an Activity as parameter inside IntentIntegrator then on initiateScan will be called activity.startActivityForResult(intent, code) - so result won't be passed to fragment (try to check onActivityResult for parent Activity, it will be called). 原因是,如果你传递一个活动的参数中IntentIntegrator然后initiateScan将被称为activity.startActivityForResult(intent, code) -这样的结果将不会被传递到片段(尝试检查onActivityResult父活动,它会被称为)。 But if you pass Fragment as parameter inside IntentIntegrator - will be called fragment.startActivityForResult(intent, code) , so Android framework will be able to call onActivityResult inside your fragment for result. 但是,如果您将Fragment作为IntentIntegrator内部的参数传递-将被称为fragment.startActivityForResult(intent, code) ,因此Android框架将能够在片段内部调用onActivityResult作为结果。

OnActivityResult() called only if you call startActivityForResult() 仅当您调用startActivityForResult()时才调用OnActivityResult()

I call the startActivityForResult() in Fragment and implement the onActivityResult in Fragment, the onActivityResult() will be called correctly. 我在Fragment中调用startActivityForResult()并在Fragment中实现onActivityResult,onActivityResult()将被正确调用。

you can not call startActivityForResult() in activity, otherwise the onActivityResult() in Fragment will not be called 您不能在活动中调用startActivityForResult(),否则Fragment中的onActivityResult()不会被调用

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

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