2011-01-09 91 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> 
相关问题