我有一个包含textview,复选框和imageview的自定义列表视图。如果选中复选框,则textview和imageview将会显示。当我点击textView时,我会拍照并在imageview中显示。因为我不能在适配器中调用onActivityResult,所以我在Activity中调用它,并且我希望将ListView中的对象的位置从适配器发送到活动,但它发送的错误是我在MainActivity中收到的位置为null。那么,我该如何解决这个问题?如何在自定义列表视图中捕获和显示图像
这里是我的.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingRight="10dp"
android:paddingLeft="16dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_instore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:layout_centerVertical="true"
android:text="Product"
android:textSize="14sp"
android:layout_weight="1"/>
<CheckBox
android:id="@+id/chk_instore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_weight="1"
android:checked="false"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:paddingTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Capture"
android:visibility="invisible"
android:id="@+id/txtCapture"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img1"
android:layout_weight="1"
android:paddingTop="5dp"
android:visibility="invisible"/>
</LinearLayout>
这里是适配器:
txtCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mContext.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra("position",position);
((Activity) mContext).startActivityForResult(i, 100);
}
}
});
这是我在MainActivity onActivityResult():
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==100&&resultCode==RESULT_OK){
Bitmap b = (Bitmap)data.getExtras().get("data");
int position = (int)data.getExtras().get("position");
POSMQty p = arrPOSMInstore.get(position);
p.setBitmap(b);
}
}
那么,我可以在我的活动中获得位置吗? – sss1
为你编辑我的答案。 Upvote并标记正确,如果有帮助。 –