wiki
wiki

Reputation: 289

implement a method startActivityForResult in a view

I have a ListView and each item with two pictures and want you to click on one of them I read QR codes with zxing.

I get an error when invoking the method startActivityForResult and do not know which method could replace it or how. I leave some code: MY HOLDER CLASS

public class ObrasHolder {
    public ImageView foto;
    public TextView num, iden, ubi,hombres,material,equipo;
    public RelativeLayout fondo;
    public TextView eq1, eq2, eq3, eq4;
    public TextView g1,g2,g3,g4;
    public ImageView cam,qr;
}

MY CUSTOM ADAPTER:

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.foto = (ImageView) item.findViewById(R.id.imgAnimal);
            holder.num = (TextView) item.findViewById(R.id.numC);
            holder.iden = (TextView) item.findViewById(R.id.idenC);
            holder.ubi = (TextView) item.findViewById(R.id.ubiC);
            holder.hombres = (TextView) item.findViewById(R.id.homC);
            holder.material = (TextView) item.findViewById(R.id.matC);
            holder.eq1 = (TextView) item.findViewById(R.id.eq1);
            holder.eq2 = (TextView) item.findViewById(R.id.eq2);
            holder.eq3 = (TextView) item.findViewById(R.id.eq3);
            holder.eq4 = (TextView) item.findViewById(R.id.eq4);
            holder.g1 = (TextView) item.findViewById(R.id.g1);
            holder.g2 = (TextView) item.findViewById(R.id.g2);
            holder.g3 = (TextView) item.findViewById(R.id.g3);
            holder.g4 = (TextView) item.findViewById(R.id.g4);
            holder.fondo = (RelativeLayout) item.findViewById(R.id.fondobra);
            holder.cam = (ImageView) item.findViewById(R.id.cam);
            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) {

                Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                startActivityForResult(intent, 0);   //Error because is undefined for the type new View.OnClickListener(){}     
            }
         });

        return item;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
       if (requestCode == 0) {
          if (resultCode == RESULT_OK) {
             String contents = intent.getStringExtra("SCAN_RESULT");
             // Handle successful scan
          } else if (resultCode == RESULT_CANCELED) {
             // Handle cancel
          }
       }
    }

}

Any idea to solve it?

i give an other erro too. In methdod onActivityResult. Thank you!!

Upvotes: 0

Views: 573

Answers (2)

Ankush Sharma
Ankush Sharma

Reputation: 933

Its undefined because, the system is looking for the method in the setOnClickListenercontext. You should provide the global context to it. You have the context variable but that too is not sufficient you should try to wrap the context with the Activity type.

((Activity)context).startActivityForResult(intent, 0);

This will do for you but just make sure when you pass context to constructor you pass it the activity context using this keyword.

Upvotes: 0

SuperFrog
SuperFrog

Reputation: 7674

Try adding this to the Activity holding the adapter:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
       if (requestCode == 0) {
          if (resultCode == RESULT_OK) {
             String contents = intent.getStringExtra("SCAN_RESULT");
             // Handle successful scan
          } else if (resultCode == RESULT_CANCELED) {
             // Handle cancel
          }
       }
    }

Upvotes: 1

Related Questions