0

誰かがコードを手伝ってくれますか?複数の画像を含む画像スライダーがあり、特定の画像を選択してボタンをクリックすると、選択した画像が別のページ/アクティビティで開くはずです。いろいろ試しましたが、選択した画像を別のページ/アクティビティに表示できません。だから、私を助けてください。前もって感謝します。

**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>
4

4 に答える 4

0

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);
            }

        });
于 2013-11-07T09:40:20.243 に答える
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);

        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;             


      }
于 2013-11-07T13:38:33.703 に答える
0

フラグメントを使用でき ます。 http://developer.android.com/guide/components/fragments.html このページに例があり、役立つかもしれません。

于 2013-11-07T09:14:22.970 に答える