如何通过适配器将图像数组传递给 NewActivity

How to pass array of images through Adapter to a NewActivity

我想通过适配器传递 asset/drawable 文件夹中的图像,然后在新的 activity 中显示它们。我曾尝试进行研究以获取指导,但没有得到我想要的。我想在将文本从字符串数组传递到新 activity.

时传递图像

这是我的适配器

public class MyAdapter1 extends RecyclerView.Adapter<MyHolder1> implements Filterable {

    Context mContext;
    ArrayList<Model1> models1, filterList1;  // this array list create a list of array which parameter define in our class
    CustomFilter1 filter1;


    public MyAdapter1(Context context, ArrayList<Model1> models) {
        this.mContext = context;
        this.models1 = models;
        this.filterList1 = models;
    }

    @NonNull
    @Override
    public MyHolder1 onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row1, null); //this line inflate our row


        return new MyHolder1(view); //this will return our view to holder class
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder1 myHolder, int i) {

        myHolder.mTitle.setText(models1.get(i).getTitle()); //here is position
        myHolder.mDesc.setText(models1.get(i).getDesc());
        myHolder.mImageView.setImageResource(models1.get(i).getIcon()); // here we used imge resource

        myHolder.setItemCLickListener(new ItemClickListener1() {
            @Override
            public void onItemClickListener(View v, int position) {

                String gTitle = models1.get(position).getTitle();
                String gDesc = models1.get(position).getDesc();
                BitmapDrawable bitmapDrawable = (BitmapDrawable)myHolder.mImageView.getDrawable();
                Bitmap bitmap = bitmapDrawable.getBitmap();

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] bytes = stream.toByteArray();

                //get our data with intent
                Intent intent = new Intent(mContext, NewActivity2.class);
                intent.putExtra("actionBarTitle1", models1.get(position).getTitle());
                intent.putExtra("brandNewDesc1", models1.get(position).getBrandNewDesc());
                intent.putExtra("soundfile", models1.get(position).getSoundfile());
                intent.putExtra("iImage", bytes);
                mContext.startActivity(intent);
                return;
            }
        });

    }

    @Override
    public int getItemCount() {
        return models1.size();
    }

    @Override
    public Filter getFilter() {

        if (filter1 == null){
            filter1 = new CustomFilter1(filterList1, this);
        }

        return filter1;
    }
}

这是模型Class

public class Model1 {

    String title;
    String desc;
    int icon;
    int soundfile;
    String brandNewDesc;

    //constructor
    public Model1(String title, String desc, String description, int icon, int music) {
        this.title = title;
        this.desc = desc;
        this.icon = icon;
        this.soundfile = music;
        this.brandNewDesc = description;
    }

    //getters


    public String getTitle() {
        return title;
    }

    public String getDesc() {
        return desc;
    }

    public String getBrandNewDesc() {

        return brandNewDesc;
    }

    public int getIcon() {
        return icon;
    }

    public int getSoundfile() {
        return soundfile;
    }

}

这是我要显示图像的 NewActivity2

private static final String URL="file:///android_asset/html_files/chant2.pdf";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new2);


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        //Keeps android screen on while reading through
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        ActionBar actionBar = getSupportActionBar();

        ImageView imageView = (ImageView)findViewById(R.id.ImPage1);
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.c1_1));



        //get data from previous activity when item of activity is clicked using intent
        Intent intent = getIntent();
        String mActionBarTitle = intent.getStringExtra("actionBarTitle");

        //setctionBar Title
        actionBar.setTitle(mActionBarTitle);

        //ok we are done, lets run the project

NewActivity2XML

<ScrollView
        android:id="@+id/scroller"
        android:layout_width="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">



        <ImageView
            android:id="@+id/ImPage1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@drawable/page1_1" />

        <ImageView
            android:id="@+id/ImPage2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/ImPage1"
            android:scaleType="fitXY"
            android:src="@drawable/page1_2" />

            <ImageView
            android:id="@+id/ImPage3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/ImPage2"
            android:scaleType="fitXY"
            android:src="@drawable/page1_3" />

            <ImageView
            android:id="@+id/ImPage4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/ImPage3"
            android:scaleType="fitXY"
            android:src="@drawable/page1_4" />

        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

这完全取决于你想如何传递数组。 如果您想在单击任何适配器图像时传递数组列表,您可以为每个项目编写一个 onclicklistener。 代码在这里。 如果你想以按钮点击的形式传递也使用相同的按钮点击监听器。

Intent intent=new Intent(getApplicationContext(),NewActivity2 .class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("VAR1", models1);
intent.putExtras(bundle);
this.startActivity(intent);

当你在 onCreate 方法中到达 Activity2 时使用下面的代码片段

Bundle bundle = getIntent().getExtras();
ArrayList<Model1> arraylist = bundle.getParcelableArrayList("VAR1");

现在您将拥有 Model1 对象的所有列表。因此,您可以像在适配器中那样加载图像 class。

您还可以在这个视频中得到详细的解释:https://youtu.be/OMCctaE66b8

希望能回答您的问题。