我想通过适配器传递资产/可绘制文件夹中的图像,然后在新活动中显示它们。我曾尝试进行研究以提供指导,但没有得到我想要的。我想在将文本从字符串数组传递到新活动时传递图像。
这是我的适配器
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;
}
}
这是模型类
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
NewActivity2 XML
<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>
答案 0 :(得分:0)
这完全取决于您要如何传递数组。 如果要在单击任何适配器映像时传递数组列表,则可以为每个项目编写一个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对象的列表。因此,您可以像在适配器类中一样加载图像。
您还可以在此视频中获得详细的解释:https://youtu.be/OMCctaE66b8
希望能回答您的问题。