2011-01-09 88 views
1

我有兩個活動:一個顯示圖像和一個按鈕,另一個顯示照片庫。我希望能夠選擇圖庫中的任何圖像,然後在第一個活動中顯示它們以代替默認圖像。相對簡單的任務:如何將所選項目從一項活動傳回給另一項活動?

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gallery); 

    Gallery g = (Gallery) findViewById(R.id.gallery); 
    g.setAdapter(new ImageAdapter(this)); 

    g.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position, long id) { 
      Toast.makeText(PhotoGallery.this, "Position: " + id, Toast.LENGTH_SHORT).show(); 

      Intent intent=new Intent(); 
      intent.putExtra("PictureID", position); 
      setResult(RESULT_OK, intent); 
      finish(); 


     } 
    }); 
} 

我還在這裏嗎?我不太確定如何處理任何字符串或int我將附加到意圖 - 我可以附加對象本身?我寧願至少回傳資源(圖片)的字符串名稱,但我現在唯一能想出的唯一事情就是如何傳回圖片的位置......這不是一個好的解決方案。如有必要,我可以澄清更多 - 謝謝。

編輯:

任何人誰在將來遇到此,這裏是我的應用程序的結果。簡單,但工作! http://www.youtube.com/watch?v=WJjEvwy8yDc

+0

你叫用startActivityForResult第二個活動,而不僅僅是startActivity?爲了將對象放入意圖中,它們需要可以parcelable(http://developer.android.com/reference/android/os/Parcelable.html),但我寧願只傳遞圖像索引/位置或資源ID 。 – 2011-01-09 06:29:28

+0

是的,我確實用startActivityForResult調用它。我寧願通過resourceid ..我如何訪問? (這是我第一個android應用程序,因此大量的noobery:]) – 2011-01-09 06:31:50

回答

1

由於您是Android新手,您應該查看隨SDk提供的Android示例。您想要做的事情可以通過ImageSwitcher實現。您可以看看Android ApiDemos示例中的ImageSwitcher示例。從Android的樣本

樣品:

ImageSwitcher1.java

/* 
* Copyright (C) 2007 The Android Open Source Project 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 

package com.example.android.apis.view; 

import com.example.android.apis.R; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.Window; 
import android.view.animation.AnimationUtils; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.Gallery.LayoutParams; 
import android.widget.ImageSwitcher; 
import android.widget.ImageView; 
import android.widget.ViewSwitcher; 


public class ImageSwitcher1 extends Activity implements 
     AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 

     setContentView(R.layout.image_switcher_1); 

     mSwitcher = (ImageSwitcher) findViewById(R.id.switcher); 
     mSwitcher.setFactory(this); 
     mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, 
       android.R.anim.fade_in)); 
     mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, 
       android.R.anim.fade_out)); 

     Gallery g = (Gallery) findViewById(R.id.gallery); 
     g.setAdapter(new ImageAdapter(this)); 
     g.setOnItemSelectedListener(this); 
    } 

    public void onItemSelected(AdapterView parent, View v, int position, long id) { 
     mSwitcher.setImageResource(mImageIds[position]); 
    } 

    public void onNothingSelected(AdapterView parent) { 
    } 

    public View makeView() { 
     ImageView i = new ImageView(this); 
     i.setBackgroundColor(0xFF000000); 
     i.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, 
       LayoutParams.MATCH_PARENT)); 
     return i; 
    } 

    private ImageSwitcher mSwitcher; 

    public class ImageAdapter extends BaseAdapter { 
     public ImageAdapter(Context c) { 
      mContext = c; 
     } 

     public int getCount() { 
      return mThumbIds.length; 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      ImageView i = new ImageView(mContext); 

      i.setImageResource(mThumbIds[position]); 
      i.setAdjustViewBounds(true); 
      i.setLayoutParams(new Gallery.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
      i.setBackgroundResource(R.drawable.picture_frame); 
      return i; 
     } 

     private Context mContext; 

    } 

    private Integer[] mThumbIds = { 
      R.drawable.sample_thumb_0, R.drawable.sample_thumb_1, 
      R.drawable.sample_thumb_2, R.drawable.sample_thumb_3, 
      R.drawable.sample_thumb_4, R.drawable.sample_thumb_5, 
      R.drawable.sample_thumb_6, R.drawable.sample_thumb_7}; 

    private Integer[] mImageIds = { 
      R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, 
      R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, 
      R.drawable.sample_6, R.drawable.sample_7}; 

} 

image_switcher_1.xml

<?xml version="1.0" encoding="utf-8"?> 
<!-- Copyright (C) 2007 The Android Open Source Project 

    Licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
    You may obtain a copy of the License at 

      http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License. 
--> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageSwitcher android:id="@+id/switcher" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
    /> 

    <Gallery android:id="@+id/gallery" 
     android:background="#55000000" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 

     android:gravity="center_vertical" 
     android:spacing="16dp" 
    /> 

</RelativeLayout> 
相關問題