【发布时间】:2017-05-02 06:14:12
【问题描述】:
我有一个自定义列表视图,其中包括文本视图、复选框和图像视图。如果选中复选框,则 textview 和 imageview 将可见。当我单击 textView 时,我会拍照并在 imageview 中显示。因为我不能在适配器中调用 onActivityResult,所以我在 Activity 中调用它,我想将 listView 中对象的位置从适配器发送到活动,但它发送一个错误,我在 MainActivity 中收到的位置为空。那么,我该如何解决呢?
这是我的 .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);
}
}
【问题讨论】:
标签: android image android-intent bitmap capture