Reading a QR code and add data into a ListView

2:40 PM

Within last post we discussed about how to scan a QR code through a QR scanning application created using Zxing Bar Code Scanner Library. So today i'm gonna tell you guys how to get the data that scanned using QR scanner application to a ListView.

QRScanMenu.java

import android.app.Activity;  
 import android.app.AlertDialog;  
 import android.content.ActivityNotFoundException;  
 import android.content.DialogInterface;  
 import android.content.Intent;  
 import android.net.Uri;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.EditText;  
 import android.widget.ListView;  
 import android.widget.SimpleAdapter;  
 import android.widget.TextView;  
 import java.util.ArrayList;  
 import java.util.HashMap;  
 /**  
  * Created by Miuranga Salgado on 9/29/2015.  
  */  
 public class QRScanMenu extends Activity {  
   ListView listView;  
   TextView budgetView;  
   ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();  
   private int reqCode= 0;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.qrscannermenu);  
     HandleClick handleClick = new HandleClick();  
     findViewById(R.id.QRButton).setOnClickListener(handleClick);  
     findViewById(R.id.ExitButton).setOnClickListener(handleClick);  
     listView = (ListView)findViewById(R.id.qrItmList);  
     budgetView = (TextView) findViewById(R.id.budgetV);  
     AlertDialog.Builder alert = new AlertDialog.Builder(this);  
     alert.setTitle("Budget Amount");  
     alert.setMessage("Enter your Budget Amount");  
     final EditText budgetIn = new EditText(this);  
     alert.setView(budgetIn);  
     alert.setPositiveButton("Enter", new DialogInterface.OnClickListener() {  
       @Override  
       public void onClick(DialogInterface dialog, int which) {  
         String budgetValue = budgetIn.getText().toString();  
         int budValue = Integer.parseInt(budgetValue);  
         budgetView.setText("YourBudget: "+String.valueOf(budValue));  
       }  
     });  
     alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {  
       @Override  
       public void onClick(DialogInterface dialog, int which) {  
         //Enter Code  
       }  
     });  
     alert.show();  
   }  
   private class HandleClick implements View.OnClickListener{  
     public void onClick(View arg0){  
       try{  
         Intent intent = new Intent("com.google.zxing.client.android.SCAN");  
         switch (arg0.getId()){  
           case R.id.QRButton:  
             intent.putExtra("SCAN_MODE", "QR_CODE_MODE");  
             break;  
           case R.id.ExitButton:  
             Intent i = new Intent(QRScanMenu.this, MainActivity.class);  
             startActivity(i);  
             break;  
         }  
         startActivityForResult(intent, reqCode);  
       }  
       catch (ActivityNotFoundException ex){  
         showDialog(QRScanMenu.this, "No Scanner Application Found", "Download Scanner Application?", "Yes", "No").show();  
       }  
     }  
   }  
   private static AlertDialog showDialog(final Activity activity, CharSequence title, CharSequence message, CharSequence btnYes, CharSequence btnNo){  
     AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity);  
     downloadDialog.setTitle(title);  
     downloadDialog.setTitle(message);  
     downloadDialog.setPositiveButton(btnYes, new DialogInterface.OnClickListener() {  
       @Override  
       public void onClick(DialogInterface dialog, int which) {  
         Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");  
         Intent intent = new Intent(Intent.ACTION_VIEW, uri);  
         try {  
           activity.startActivity(intent);  
         } catch (ActivityNotFoundException ex) {  
         }  
       }  
     });  
     downloadDialog.setNegativeButton(btnNo, new DialogInterface.OnClickListener() {  
       @Override  
       public void onClick(DialogInterface dialog, int which) {  
       }  
     });  
     return downloadDialog.show();  
   }  
   public void onActivityResult(int reqCode, int resCode, Intent intent){  
     super.onActivityResult(reqCode, resCode, intent);  
     if (reqCode == 0){  
       //TextView qrStatus = (TextView) findViewById(R.id.statusText);  
       TextView qrResult = (TextView) findViewById(R.id.resultText);  
       if(resCode == RESULT_OK){  
         //qrStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));  
         qrResult.setText(intent.getStringExtra("SCAN_RESULT"));  
         String scanRes = intent.getStringExtra("SCAN_RESULT");  
         String[]result = scanRes.split(":");  
         HashMap<String, String> resItems = new HashMap<String, String>();  
         if (result.length>0){  
           resItems.put("itmName", result[0]);  
           getItmPrice(result[1], resItems);  
         }  
         list.add(resItems);  
         addListView();  
       }  
       else if (resCode == RESULT_CANCELED){  
         //qrStatus.setText("Press QR Scan button to Scan Item");  
         qrResult.setText("Scan Cancelled");  
       }  
     }  
   }  
   private String getItmPrice(String value, HashMap<String, String> resItems) {  
     return resItems.put("itmPrice", value);  
   }  
   private void addListView(){  
     SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.itemlistview, new String[]{"itmName", "itmPrice"}, new int[]{R.id.text1, R.id.text2});  
     listView.setAdapter(simpleAdapter);  
   }  
 }  

This is a simple layout with a ListView that will useful to implement this,

qrscannermenu.xml

<?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical" android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:background="@drawable/backimg">  
   <LinearLayout  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:id="@+id/linLayout1">  
     <Button  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:id="@+id/QRButton"  
       android:text="QR Scan"  
       android:textSize="18sp"/>  
     <Button  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:id="@+id/ExitButton"  
       android:text="Back Main"  
       android:textSize="18sp"/>  
   </LinearLayout>  
   <TextView  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:id="@+id/budgetV"  
     android:background="#5e5e5e"  
     android:textColor="#FFFFFF"  
     android:layout_below="@+id/linLayout1"  
     android:textSize="18sp"/>  
   <TextView  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:id="@+id/resultText"  
     android:text="Ready to Scan"  
     android:textSize="18sp"  
     android:layout_below="@+id/budgetV"  
     android:background="@android:color/white"  
     android:textColor="@android:color/black"/>  
     <ListView  
       android:layout_width="match_parent"  
       android:layout_height="match_parent"  
       android:layout_below="@+id/resultText"  
       android:id="@+id/qrItmList">  
     </ListView>  
 </RelativeLayout>  






















Thanks for Reading.. See you guys again..
If you have any problems with the code. Please comment out below.. :)


You Might Also Like

1 comments

  1. cual es su wasap oi telegram no puedo hacer que funcione por favor

    ReplyDelete

Popular Posts

Like us on Facebook

Flickr Images

Sharing adds a value faster than it adds cost