Android - Barcode Fragment result not displaying

蓝咒 提交于 2021-02-10 13:36:29

问题


[SOLVED] The apps work fine without crash, but it should be update the resultView text from "Hasil Scan" to the scan result, but it doesnt. The problem are the textview (resultView) are not updated after scan. Im using DM77 Zxing barcode scanner. Here is the code i have done so far.

MainActivity:

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

import sc.smklabor.rpl.labsscanner.sc.smklabor.rpl.labsscanner.fragments.ScanFragment;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        Fragment selectedFragment = null;
                        switch (item.getItemId()) {
                            case R.id.scannav:
                                selectedFragment = ScanFragment.instance();
                                break;
                        }
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.frame_layout, selectedFragment);
                        transaction.commit();
                        return true;
                    }
                });
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, ScanFragment.instance());
        transaction.commit();

    }
}

and here is for the scanner fragment, i only have done one fragment, other fragment just will be about and website ScanFragment:

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.TextView;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import sc.smklabor.rpl.labsscanner.R;

/**
 * Created by Lenovo on 21/03/2018.
 */

public class ScanFragment extends Fragment implements View.OnClickListener {

    private IntentIntegrator scanner;
    private Button startScan,btnGoUrl;
    private TextView resultView;
    private String scanResult;

    public static ScanFragment instance() {
        ScanFragment f = new ScanFragment();
        return f;
    }

    public void onCreate(Bundle b) {
        super.onCreate(b);
        scanner = new IntentIntegrator(this.getActivity()).forSupportFragment(this);
    }

    public void onActivityResult(int rc,int res, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(rc, res, data);
        if(result!=null) {
            scanResult = result.getContents().toString();
            if (scanResult.contains("--L--:")) {
                String postId = scanResult.split("://")[1];
                btnGoUrl.setVisibility(View.INVISIBLE);
                resultView.setText("Post ID: " + postId);
            } else if (URLUtil.isHttpUrl(scanResult) || isValidURL(scanResult)) {
                btnGoUrl.setVisibility(View.VISIBLE);
                resultView.setText(result.getContents().toString());
            } else {
                btnGoUrl.setVisibility(View.INVISIBLE);
                resultView.setText(result.getContents().toString());
            }
        }
    }

    public View onCreateView(LayoutInflater inf, ViewGroup con, Bundle b) {
        View view = inf.inflate(R.layout.scanlayout, con, false);
        startScan = (Button)view.findViewById(R.id.button);
        btnGoUrl = (Button)view.findViewById(R.id.btnGoUrl);
        scanner = new IntentIntegrator(getActivity());
        startScan.setOnClickListener(this);
        resultView = (TextView)view.findViewById(R.id.textView);

        btnGoUrl.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View e) {
                Uri URL = Uri.parse(scanResult);
                Intent i = new Intent(Intent.ACTION_VIEW,URL);
                startActivity(i);
            }
        });
        return view;
    }
    public boolean isValidURL(String s) {
        return (s.contains("http://") || s.contains("https://")) && (s.contains(".com") || s.contains(".org") || s.contains(".net") || s.contains(".id") || s.contains(".sch.id") || s.contains(".me"));
    }

    public void onClick(View e) {
        scanner.setPrompt("Arahkan kamera kepada barcode tepat pada kotak.");
        scanner.setOrientationLocked(false);
        scanner.initiateScan();
    }
}

回答1:


I think the problem is the onActivityResult is not called in the ScanFragment

First you should override the onActivityResult(int rc,int res, Intent data) on the MainActivity

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame_layout);
    if (fragment != null) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

This code it will help you call the onActivityResult on the ScanFragment.



来源:https://stackoverflow.com/questions/49407408/android-barcode-fragment-result-not-displaying

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!