2017-05-02 41 views
0

我有一个包含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); 
    } 
} 

回答

0

意图你进去了活动结果与您在startActivityForResult()上传递的结果不同。这就是为什么你没有得到职位。

为此,您可以保存活动中的位置并在onActivityResult()中使用它。

0

问题是您发送的打开相机活动的意图与您的活动结果中的Intent完全不同。意图接收由相机活动创建,以将图像数据发送到您的活动。

下面是一个解决方案,你可以做到这一点。

声明一个变量在你的适配器位置点击

private int posClicked = -1; 

,并在您

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); 
       posClicked = position; 
       ((Activity) mContext).startActivityForResult(i, 100); 
      } 
     } 
}); 

然后在活动创建活动结果

public void afterActivityResult(Bitmap bmp,.. extra){ 
    //do the need full 
} 

后,在适配器的方法结果

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); 
     yourAdapter.afterActivityResult(...); 

}}

您也可以为clickedPosition方法getter方法。

在适配器

public int getClickedPosition(){ 
    return posClicked; 
} 

,并使用它像

adapter.getClickedPosition() 
在您的活动

+0

那么,我可以在我的活动中获得位置吗? – sss1

+0

为你编辑我的答案。 Upvote并标记正确,如果有帮助。 –

相关问题