【问题标题】:display selected image on another page from image slider从图像滑块在另一页上显示选定的图像
【发布时间】:2013-11-07 21:38:33
【问题描述】:

有人可以帮我编写代码吗?我有包含多个图像的图像滑块,如果我选择任何特定图像并单击按钮,则所选图像应在另一个页面/活动上打开。 我尝试了很多,但无法在另一个页面/活动上显示选定的图像。 所以,请帮助我。 提前致谢。

**Main.java**   

包 com.example.imagesliderdemo;

import android.annotation.SuppressLint;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

@SuppressWarnings("deprecation")
public class Main extends Activity {

    private Gallery topsgallery;
    private ImageView imgView;
    int GalItemBg;

    private Integer[] Imgid = {
            R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5 };



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

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        imgView = (ImageView)findViewById(R.id.ImageViewTops);  
        imgView.setImageResource(Imgid[0]);

        Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
        btnNextScreen.setOnClickListener(new View.OnClickListener() {


            private int position;


            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Intent nextScreen = new Intent(Main.this, OpenImage.class);
                Intent nextScreen=new Intent(getApplicationContext(),OpenImage.class);                       
                                 nextScreen.putExtra("image",R.drawable.ic_launcher);
          startActivity(nextScreen);
            }

        });


        topsgallery = (Gallery) findViewById(R.id.gallery1);
        topsgallery.setAdapter(new AddImgAdp (this));

        topsgallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) {


            imgView = (ImageView)findViewById(R.id.ImageViewTops);

            }

        }); 

     }  

   public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;

        public AddImgAdp(Context c) {
            cont = c;
            TypedArray typArray = obtainStyledAttributes(R.styleable.Gallery1);
            GalItemBg = typArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
            typArray.recycle();
        }


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

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

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

        public View getView(final int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(cont);

            imageView.setImageResource(Imgid[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(80, 70));
            imageView.setBackgroundResource(GalItemBg);      



            return imageView;             


          }
   }
}

**Other activity file OpenImage.java**
package com.example.imagesliderdemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class OpenImage extends Activity
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);
        ImageView image = (ImageView) findViewById(R.id.imageview);
        int image_link = getIntent().getIntExtra("image_url", R.drawable.ic_launcher);
        image.setImageResource(image_link);

        Button btnClose = (Button) findViewById(R.id.btnClose);
         btnClose.setOnClickListener(new View.OnClickListener() {

                public void onClick(View arg0) {
                    //Closing SecondScreen Activity
                    finish();
                }
            });

    }
}


**activity_main.xml**


<?xml version="1.0" encoding="utf-8"?>

<ScrollView 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:overScrollMode="always" 
        android:isScrollContainer="true" 
        android:scrollbarAlwaysDrawVerticalTrack="true" 
        android:scrollbarStyle="outsideInset" 
        android:scrollbars="vertical">

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:scrollbars="vertical">

<LinearLayout 
    android:id="@+id/LinearLayoutTops"
    android:layout_width="wrap_content" 
    android:layout_height="100dp"
    android:orientation="vertical">
    <TextView
        android:gravity="center"
        android:text="@string/tops"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <LinearLayout 
        android:id="@+id/LinearLayoutTopsGallery"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:orientation="horizontal">

    <Gallery 
        android:id="@+id/gallery1" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:spacing="10dp" />

    <ImageView 
        android:id="@+id/ImageViewTops"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:contentDescription="@string/imageslide"/>


    </LinearLayout>
</LinearLayout>



    <Button android:id="@+id/btnNextScreen"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Send to Next Screen"
            android:layout_marginTop="15dip"/>



</LinearLayout>    
</ScrollView>



**Another Activity screen2.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnClose"
        android:layout_width="151dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="15dip"
        android:text="Close" />

    <ImageView 
        android:id="@+id/imageview"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        />


</LinearLayout>

【问题讨论】:

  • 我假设您有一个带有图像的 viewPager 对吗?你是如何填充 viewPager 的?发布一些代码,也许有人可以帮助你;)
  • @RethinavelVelu 请检查..
  • @rui.mendes 我已经发布了我的代码,请检查
  • 我想你可能每次都得到相同的图像??

标签: android android-intent


【解决方案1】:

你可以使用片段, http://developer.android.com/guide/components/fragments.html 这个页面有一个例子,也许可以帮助你。

【讨论】:

    【解决方案2】:

    创建一个全局变量以获取 Slider 的位置(假设该变量为 pos)。现在在 getView 的滑块适配器代码中像这样

    public View getView(final int position, View convertView, ViewGroup parent) {
                ImageView imageView = new ImageView(cont);
    
                pos=position;
    
                imageView.setImageResource(Imgid[position]);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setLayoutParams(new Gallery.LayoutParams(80, 70));
                imageView.setBackgroundResource(GalItemBg);      
    
    
    
                return imageView;
    
    
    
              }
    

    现在按钮点击代码如下:

    btnNextScreen.setOnClickListener(new View.OnClickListener() {
    
    
                private int position;
    
    
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //Intent nextScreen = new Intent(Main.this, OpenImage.class);
                    Intent nextScreen=new Intent(getApplicationContext(),OpenImage.class);
    
                    //Sending data to another Activity
                     nextScreen.putExtra("id",Imgid[pos]);
                    Log.e("n", Imgid[pos]);
                    startActivity(nextScreen);
                }
    
            });
    

    【讨论】:

    • 在执行其捐赠后,不幸的是应用程序已停止。
    • 什么类型的异常。能否请您在此处仅将 Logcat 详细信息粘贴到 cmets 中
    • 11-07 10:13:26.819: E/AndroidRuntime(2722): 致命异常: main 11-07 10:13:26.819: E/AndroidRuntime(2722): java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.imagesliderdemo/com.example.imagesliderdemo.OpenImage}:java.lang.NullPointerException
    【解决方案3】:

    只需在点击侦听器上传递图库中选定的图像 id 值。

        topsgallery.setOnItemClickListener(new OnItemClickListener()
       {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
        {
            // TODO Auto-generated method stub
         Intent fullImage = new Intent(YourActivity.this,FullImageView.class)
         fullImage.putExtra("image_url",R.drawable.image);
         startActivity(fullImage);
    
        }
    });
    

    考虑以下步骤:

    #1 使用以下视图创建一个新活动,例如:FullImageView,并在清单中声明。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/android" />
     </ImageView>
    </LinearLayout>
    

    3 : 在 FullImageView 活动的 onCreate 中接收意图

      ImageView image = (ImageView) findViewById(R.id.image);
      int image_link = getIntent().getIntExtra("image_url", R.drawable.default);
    

    4 : 将 URL 设置为您的 ImageView。

    imageView.setImageResource(image_link);
    

    如果您对此有任何疑问,请随时联系。

    【讨论】:

    • 我按照你的步骤去做了,但是这行 mImageLoader.displayImage("Your URL",ImageView);
    • 请检查我编辑的答案,你正在使用drawable。它是 int,所以你必须使用 getIntExtra();如果有任何问题,请告诉我
    • 选择图片并点击按钮后..其他页面上没有显示图片
    • 哦,亲爱的,把日志登录到 FullImageView 活动中,您是否通过意图收到了额外的价值?请发布两个课程的更新代码,我会检查并通知您
    • Main.java public void onClick(View v) { Intent nextScreen=new Intent(getApplicationContext(),OpenImage.class); nextScreen.putExtra("image_url",R.drawable.image);开始活动(下一个屏幕); } }); FullImageView.java ImageView myImage = (ImageView) findViewById(R.id.imageview); Log.e("image_url", myImage);
    【解决方案4】:

    您可以将意图放入适配器中...然后执行以下操作(请注意,您需要将上下文传递给适配器):

    public View getView(final int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(cont);
    
            imageView.setImageResource(Imgid[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(80, 70));
            imageView.setBackgroundResource(GalItemBg);
    
            imageView.setOnClickListener(new View.OnClickListener() {
    
           @Override
           public void onClick(View v) {
                   Intent nextScreen=new Intent(context,OpenImage.class);                       
                nextScreen.putExtra("image",ImgId[pos]);
          startActivity(nextScreen);
            }
        });
    
    
            return imageView;             
    
    
          }
    

    【讨论】:

    • 我试过这个,但不幸的是应用程序在执行后停止了
    猜你喜欢
    • 1970-01-01
    • 2016-09-01
    • 2016-04-11
    • 2013-01-25
    • 2011-09-28
    • 2021-10-01
    • 2020-12-31
    • 2020-12-12
    相关资源
    最近更新 更多