扫描条形码并在将其存储在recyclerView中后从JSON中读取它

Posted

技术标签:

【中文标题】扫描条形码并在将其存储在recyclerView中后从JSON中读取它【英文标题】:Scan Bar code and read it from JSON after store it in recyclerView 【发布时间】:2020-05-15 18:21:00 【问题描述】:

//1.我正在尝试使用 ZXingScannerView 扫描仪传递条形码值

//2.然后,如果扫描的条形码等于 JSON 条形码对象显示 item_name 和来自 JSON //file in recyclerview 的成本,我的问题是扫描后什么都没有显示

@覆盖 public View onCreateView(LayoutInflater inflater, ViewGroup 容器, 捆绑保存的InstanceState) 查看视图 = inflater.inflate(R.layout.fragment_second, container, false);

    recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    scannerView = (ZXingScannerView) view.findViewById(R.id.zxscan);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    recyclerView.setLayoutManager(linearLayoutManager);


    CustomAdapter customAdapter = new CustomAdapter(getActivity(), products_name, cost);
    recyclerView.setAdapter(customAdapter); // set the Adapter to RecyclerView


    Dexter.withActivity(getActivity())
            .withPermission(Manifest.permission.CAMERA)
            .withListener(new PermissionListener() 
                @Override
                public void onPermissionGranted(PermissionGrantedResponse response) 
                    scannerView.setResultHandler(SecondFragment.this);
                    scannerView.startCamera();
                

                @Override
                public void onPermissionDenied(PermissionDeniedResponse response) 
                    Toast.makeText(getActivity(), "Please Accept The Permission", Toast.LENGTH_LONG).show();
                

                @Override
                public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) 
                
            )
            .check();
    return view;


@Override
public void onDestroy() 
    scannerView.stopCamera();
    super.onDestroy();



@Override
public void handleResult(Result rawResult) 
    processRawResult(rawResult.getText());
    if (Patterns.WEB_URL.matcher(rawResult.getText()).matches()) 
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(rawResult.getText()));
        startActivity(browserIntent);
    


private void processRawResult(String text) 
    if (text.startsWith("BEGIN:"))
        String[] tokens = text.split("\n");
        QRVCardModel qrvCardModel = new QRVCardModel();
        for (int i = 0; i < tokens.length; i++)
        
            if (tokens[i].startsWith("BEGIN:")) 
                qrvCardModel.setType(tokens[i].substring("BEGIN:".length()));
            

            else if (tokens[i].startsWith("N")) 
                qrvCardModel.setName(tokens[i].substring("N:".length()));
            

            else if (tokens[i].startsWith("ORG")) 
                qrvCardModel.setOrg(tokens[i].substring("ORG:".length()));
            

            else if (tokens[i].startsWith("TEL:")) 

                qrvCardModel.setTel(tokens[i].substring("TEL:".length()));
            

            else if (tokens[i].startsWith("URL:")) 
                qrvCardModel.setUrl(tokens[i].substring("URL:".length()));
            

            else if (tokens[i].startsWith("EMAIL:")) 
                qrvCardModel.setEmail(tokens[i].substring("EMAIL:".length()));
            

            else if (tokens[i].startsWith("ADS:")) 
                qrvCardModel.setEmail(tokens[i].substring("ADS:".length()));
            

            else if (tokens[i].startsWith("NOTE:")) 
                qrvCardModel.setNote(tokens[i].substring("NOTE:".length()));
            

            else if (tokens[i].startsWith("SUMMERY:")) 
                qrvCardModel.setSummer(tokens[i].substring("SUMMERY:".length()));
            
            else if (tokens[i].startsWith("DTSTART:")) 
                qrvCardModel.setDtstart(tokens[i].substring("DTSTART:".length()));
            
            else if (tokens[i].startsWith("DTEND:")) 
                qrvCardModel.setDtend(tokens[i].substring("DTEND:".length()));
            

        

    
    else if (text.startsWith("hhtp://")||
            text.startsWith("hhtps://")||
            text.startsWith("www."))

    
        QRURLMode qrurlMode = new QRURLMode(text);

    
    else if (text.startsWith("geo:"))
    
        QRGeoModel qrGeoModel= new QRGeoModel();
        String delims = "[ ,?q= ] +";
        String tokens[]= text.split(delims);
        for (int i=0; i< tokens.length;i++)
        
            if (tokens[i].startsWith("geo:"))
            
                qrGeoModel.setLat(tokens[i].substring("geo:".length()));
            
        
        qrGeoModel.setLat(tokens[0].substring("geo".length()));
        qrGeoModel.setLng(tokens[1]);
        qrGeoModel.setGeo_place(tokens[2]);

    
    else
    
        Toast.makeText(getActivity(),"QR CODE PASS", Toast.LENGTH_SHORT).show();


        String json;


        try 

            InputStream is = getActivity().getAssets().open("products.json");
            Toast.makeText(getActivity(), "JSON1", Toast.LENGTH_SHORT).show();
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            //Toast.makeText(getActivity(),"JSON2", Toast.LENGTH_SHORT).show();
            json = new String(buffer, "UTF-8");

            Log.d("JSONTestLogs", "JSON Raw Text: " + json);
            JSONObject object = new JSONObject(json);
            JSONArray productsArray = object.getJSONArray("products");
            Log.d("JSONTestLogs", "Fetching the Products array from JSON File. Size: " + productsArray.length());
            for (int i = 0; i < productsArray.length(); i++) 
                JSONObject item = productsArray.getJSONObject(i);
                Log.d("JSONTestLogs", "Item: " + i + "\n" + item.toString());

                if (text.equals(item.getString("bar_code"))) 
                    products_name.add(item.getString("item_name"));
                    cost.add(item.getString("cost"));
                
            

        
        catch
        (IOException e)
        

            e.printStackTrace();
        
        catch (JSONException e)
            e.printStackTrace(); 

    


    scannerView.resumeCameraPreview(SecondFragment.this);
    // Toast.makeText(getActivity(),"JSON3", Toast.LENGTH_SHORT).show();



【问题讨论】:

【参考方案1】:

扫描条形码后。我想在这里将条形码添加到您的列表中;

products_name.add(item.getString("item_name"));

添加操作后,没有刷新recyclerview数据。

此代码部分只运行一次。

CustomAdapter customAdapter = new CustomAdapter(getActivity(), products_name, cost);
    recyclerView.setAdapter(customAdapter); // set the Adapter to RecyclerView

你应该再次调用这部分,或者你需要在你的适配器内部有一个函数来通知recyclerview的数据。

【讨论】:

以上是关于扫描条形码并在将其存储在recyclerView中后从JSON中读取它的主要内容,如果未能解决你的问题,请参考以下文章

我需要一个条形码扫描仪将其包含在我的 phonegap 项目中

如何在其他地方使用条形码扫描仪的结果?

我需要在扫描后立即使用该条形码(没有用户“确定”按钮)

从服务中的活动引用文本视图,反之亦然

拦截条码扫描事件

在 EBS 卷上创建文件系统,将其挂载到 EC2 实例并在将实例替换为 CDK 时持久化数据