2016-10-08 4 views
-1

私はAdharカードからQRコードをスキャンしていると私は私がtoast.Iに表示していたパラメータのデータを取得していますが、私が受け取ったXMLデータの形式の下に示されています。今私が欲しいのは、これらすべてのフィールドをテキストビューで表示したいのです。どうすればこれを達成できますか?XMLからテキストビューにデータを設定するには?

XMLデータ -

<?xml version="1.0" encoding="UTF-8"?> 
<PrintLetterBarcodeData uid="123456789" name="xyz" gender="M" yob="1978" co="S/O abc" loc="vgvgjv" vtc="jkl" po="gfvygvyv" dist="jkl" state="hbbuh" pc="12345"/> 

以下は私のコードであると私はrawResultにデータが供給しています。

public class SimpleScannerActivity extends BaseScannerActivity implements ZXingScannerView.ResultHandler { 
    private ZXingScannerView mScannerView; 

    @Override 
    public void onCreate(Bundle state) { 
     super.onCreate(state); 
     setContentView(R.layout.activity_simple_scanner); 
     setupToolbar(); 

     ViewGroup contentFrame = (ViewGroup) findViewById(R.id.content_frame); 
     mScannerView = new ZXingScannerView(this); 
     contentFrame.addView(mScannerView); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     mScannerView.setResultHandler(this); 
     mScannerView.startCamera(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     mScannerView.stopCamera(); 
    } 

    @Override 
    public void handleResult(Result rawResult) { 
     Toast.makeText(this, "Contents = " + rawResult.getText() + ", Format = " + rawResult.getBarcodeFormat().toString(), Toast.LENGTH_LONG).show(); 
     //in1.txtMeterSerialNo.setText(rawResult.getText().toString()); 
     //finish(); 
     // Note: 
     // * Wait 2 seconds to resume the preview. 
     // * On older devices continuously stopping and resuming camera preview can result in freezing the app. 
     // * I don't know why this is the case but I don't have the time to figure out. 
     Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       mScannerView.resumeCameraPreview(SimpleScannerActivity.this); 
      } 
     }, 2000); 
    } 
} 

答えて

0

は、次のコードを使用し、あなたのJavaファイルでは、特定のID今

android:id="@+id/myText" 

であなたのXMLでのTextViewを作成します。 TextViewのテキストをrawResultのString値に変換します。

+0

おかげブロ。これらの値を別のtextViewsに表示するには、別のtextviewのuidのようにして、このような別のtextviewの名前を付けたいと思います。 –

+0

そのためには、文字列を複数のサブストリング(https://www.tutorialspoint.com/java/java_string_substring.htm)に分割し、それぞれを複数のTextViewに個別に表示する必要があります。私は助けてくれることを願っています:Dあなたが答えに満足しているのであれば、アップヴォートか受け入れられた答えとしてマークしてください。 :D – Rippr

0

xml解析を使用してデータを解析し、フィールド値を取得する必要があります。このリンクをチェックしてください:これは、ディスプレイを設定します

TextView myTextView = (TextView) findViewById(R.id.myText); 
myTextView.setText("Contents = " + rawResult.getText().toString()); 

https://www.tutorialspoint.com/android/android_xml_parsers.htm

0
InputStream is = null; 
    try { 
     HashMap<String, String> map = new HashMap<>(); 
     AssetManager assetManager = this.getAssets(); 
     is = assetManager.open("data.xml");//if your file is under "assets" 
     XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
     factory.setNamespaceAware(true); 
     XmlPullParser xpp = factory.newPullParser(); 
     xpp.setInput(is, null); 
     if (xpp.getName() != null&&xpp.getName().equals("PrintLetterBarcodeData")) { 
      String uid= xpp.getAttributeValue(null, "uid"); 
      map.put("uid", uid); 
      String name = xpp.getAttributeValue(null,"name"); 
      map.put("name", name); 
     } 
    } catch (Exception e) { 
     Log.w(TAG, "error", e); 
    } finally { 
     Utils.closeSilently(is); 
    } 
関連する問題