Search code examples
androidlistviewqr-codezxing

QR code reader thrown from an image of an item of a ListView


I want that when the image of the item is pressed launches QR code reader provided by the library zxing.

I tested two options. The first option has been my custom adapter trying to launch the intent.

The second option has been following the first and a topic of this forum, and I got kind of my custom adapter in the main class and I created two methods.

This last option gives me syntax error, but if you run: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.google.zxing.client.android.SCAN (has extras) } at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1659)

Actually, this is my code:

public class Inventario extends Activity implements OnItemClickListener {
    private ArrayList<Obra> obras;
    private ListView lvObras;
    private ObrasAdapter adapter;
    private TextView num, iden,ubi,hombres,material;
    private int pulsado = -1;
    Toast toast1 ;

    private int operacion = -1;

    List<String> equiDisp;
    ArrayList<String> marcado;

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

        equiDisp = new ArrayList<String>();
        marcado = new ArrayList<String>();

        for(int i=1; i<11; i++)
        {
            equiDisp.add(String.valueOf(i));
        }

        // Inicializamos las variables.
        obras = new ArrayList<Obra>();

        rellenarArrayList();

        actualizarDisplay();
    }

        ...
    public void abrirLector(){
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        startActivityForResult(intent, 0);
    }

    public void actualizarDisplay()
    {
       adapter = new ObrasAdapter(this, obras);
       lvObras = (ListView) findViewById(R.id.lvItems);
       lvObras.setAdapter(adapter);
       lvObras.setOnItemClickListener(this);
    }

    public class ObrasAdapter extends ArrayAdapter<Obra> {
       public Context context;
       private ArrayList<Obra> datos;

       public void DisplayProjectListAdapter(Context c) {
       context = c;
     }

     public ObrasAdapter(Context context, ArrayList<Obra> datos) {
        super(context, R.layout.listview_item, datos);
        this.context = context;
        this.datos = datos;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View item = convertView;
        ObrasHolder holder;

        if (item == null) {
           item = LayoutInflater.from(context).inflate(R.layout.listview_item,
                null);

            holder = new ObrasHolder();
            holder.qr = (ImageView) item.findViewById(R.id.qr);

            item.setTag(holder);
    }
    holder = (ObrasHolder) item.getTag();

    holder.qr.setOnClickListener(new OnClickListener() {           
        @Override
        public void onClick(View v) {

            holder.qr.setOnClickListener(new OnClickListener(){           
                 @Override
                 public void onClick(View v) {

                     abrirLector();
            }
         });
        }
     });

    return item;
}

Can anyone help me out? Thank you


Solution

  • The problem you are getting related to ActivityNotFoundException can be fixed by the steps you can find in this link. This is the official documentation for zxing that tells you how to open the scanner in the yet provided dialog they have. Replace it by your code:

     public void abrirLector(){
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
            startActivityForResult(intent, 0);
     }