我有一个ListView和每个项目有两张图片,并希望你点击其中一个我用zxing读取二维码。
我在调用方法startActivityForResult时遇到错误,并且不知道哪个方法可以替换它或者如何替换它。 我留下一些代码: 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
}
}
}
}
有什么想法解决它?
我也给了另一个错误。在methdod onActivityResult中。 谢谢!!
答案 0 :(得分:1)
尝试将此添加到持有适配器的活动:
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
}
}
}
答案 1 :(得分:0)
未定义因为,系统正在setOnClickListener
上下文中查找方法。您应该为它提供全局上下文。
你有上下文变量,但是你也应该尝试用Activity类型包装上下文。
((Activity)context).startActivityForResult(intent, 0);
这将为您做,但只需确保将上下文传递给构造函数时,使用this
关键字将活动上下文传递给它。