2016-11-08 50 views
0

我想通過我的view在其他活動中更新我的view。這是我通過view的代碼。以意圖傳遞視圖

emp_photo_edit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       i.putExtra("container", (Serializable) viewDialog); 
       ((EmployeeActivity)context).startActivityForResult(i, 2017); 
      } 
     }); 

然後我想更新我的觀點在其他活動

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 2017 && resultCode == RESULT_OK && null != data) { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null); 
     cursor.moveToFirst(); 
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 
     View apa = (View) data.getSerializableExtra("content"); 
     //View dialog = View.inflate(getApplicationContext(),R.layout.dialog_employee_edit,null); 
     ImageView imageView = (ImageView) apa.findViewById(R.id.emp_photo_edit); 
     imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
    } 
} 

但它顯示出異常。

FATAL EXCEPTION: main 
    java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to java.io.Serializable 
    at com.fingerspot.hz07.revocloud.adapter.EmployeeAdapter$MyViewHolder$5.onClick(EmployeeAdapter.java:334) 
    at android.view.View.performClick(View.java:4084) 
    at android.view.View$PerformClick.run(View.java:16966) 
    at android.os.Handler.handleCallback(Handler.java:615) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4745) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    at dalvik.system.NativeStart.main(Native Method) 
+0

意見不能從一個'Activity'傳遞到另一個。 –

回答

3

參數傳遞給捆應該實現SerializableParcelable接口。 LinearLayout沒有。最好的解決方案是將視圖內的數據傳遞給意圖並將其應用於接收中的視圖Activity

+0

但是,我想在另一個活動中更新該視圖,我可以這樣做嗎? – Huzain

+0

是的,你應該通過你需要的屬性,並將它們應用到其他活動的視圖 –

+0

我有imageview,我應該在另一個活動更新,如何做到這一點? – Huzain

0

您無法意圖傳遞視圖(圖像視圖)。

您應該像下面一樣將圖像位圖傳遞爲ByteArrayParcelable

通行證位圖的ByteArray:

首先將圖像轉換成ByteArray然後通入意向書,並在接下來的活動得到BundleByteArray並轉換爲圖像(位圖),並設置成ImageView

轉換BitmapByteArray並通入Intent: -

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] byteArray = stream.toByteArray(); 

Intent intent = new Intent(this, NextActivity.class); 
intent.putExtra("picture", byteArray); 
startActivity(intent); 

Bundle獲取ByteArray並轉換爲Bitmap圖像: -

Bundle extras = getIntent().getExtras(); 
byte[] byteArray = extras.getByteArray("picture"); 

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
ImageView image = (ImageView) findViewById(R.id.imageView1); 

image.setImageBitmap(bmp); 

OR

P屁股位圖Parcelable:

Bitmap直接進入意向爲Parcelable額外的,並得到位圖作爲Parcelable在接下來的活動額外從Bundle,但問題是,如果你的位圖/圖像尺寸是大當時的圖像不加載在下一個活動中。

結帳 get ImageView's image and send it to an activity with Intent