我有CardView。当我点击卡片时,会打开一个新的“活动”,并显示适当的图像和一些文本。
我使用XML数组存储可绘制图像的路径,并使用Glide库上传图像。
当我在另一个活动中获得意图时,我有几个与上载图像有关的例外。
当我为包含CardView的main_xms充气时,所有作品都起作用。但是现在我不明白是什么样的问题。
XML数组
<array name="sports_images">
<item>@drawable/img_baseball</item>
<item>@drawable/img_badminton</item>
<item>@drawable/img_basketball</item>
<item>@drawable/img_bowling</item>
<item>@drawable/img_cycling</item>
<item>@drawable/img_golf</item>
<item>@drawable/img_running</item>
<item>@drawable/img_soccer</item>
<item>@drawable/img_swimming</item>
<item>@drawable/img_tabletennis</item>
<item>@drawable/img_tennis</item>
</array>
代表数据的类
class Sport {
//Member variables representing the title and information about the sport
private String title;
private String info;
private int imageResourse;
/**
* Constructor for the Sport data model
*
* @param title The name if the sport.
* @param info Information about the sport.
*/
Sport(String title, String info, int imageResourse) {
this.title = title;
this.info = info;
this.imageResourse = imageResourse;
}
public Sport() {
}
/**
* Gets the title of the sport
*
* @return The title of the sport.
*/
String getTitle() {
return title;
}
/**
* Gets the info about the sport
*
* @return The info about the sport.
*/
String getInfo() {
return info;
}
/**
* Gets sport image
*
* @return The image of the spors.
*/
public int getImageResourse() {
return imageResourse;
}
适配器类,该类实现将数据传递到另一个Activity的onClick方法
class SportsAdapter extends RecyclerView.Adapter<SportsAdapter.ViewHolder> {
//Member variables
private ArrayList<Sport> mSportsData;
private Context mContext;
/**
* Constructor that passes in the sports data and the context
*
* @param sportsData ArrayList containing the sports data
* @param context Context of the application
*/
SportsAdapter(Context context, ArrayList<Sport> sportsData) {
this.mSportsData = sportsData;
this.mContext = context;
}
/**
* Required method for creating the viewholder objects.
*
* @param parent The ViewGroup into which the new View will be added after it is bound to an adapter position.
* @param viewType The view type of the new View.
* @return The newly create ViewHolder.
*/
@NonNull
@Override
public SportsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false));
}
/**
* Required method that binds the data to the viewholder.
*
* @param holder The viewholder into which the data should be put.
* @param position The adapter position.
*/
@Override
public void onBindViewHolder(SportsAdapter.ViewHolder holder, int position) {
//Get current sport
Sport currentSport = mSportsData.get(position);
//Populate the textviews with data
holder.bindTo(currentSport);
}
/**
* Required method for determining the size of the data set.
*
* @return Size of the data set.
*/
@Override
public int getItemCount() {
return mSportsData.size();
}
/**
* ViewHolder class that represents each row of data in the RecyclerView
*/
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//Member Variables for the TextViews
private TextView mTitleText;
private TextView mInfoText;
private ImageView mSportImage;
/**
* Constructor for the ViewHolder, used in onCreateViewHolder().
*
* @param itemView The rootview of the list_item.xml layout file
*/
ViewHolder(View itemView) {
super(itemView);
//Initialize the views
mTitleText = itemView.findViewById(R.id.title);
mInfoText = itemView.findViewById(R.id.subTitle);
mSportImage = itemView.findViewById(R.id.sportImage);
//Set the listener to the entire view
itemView.setOnClickListener(this);
}
void bindTo(Sport currentSport) {
//Populate the textviews with data
mTitleText.setText(currentSport.getTitle());
mInfoText.setText(currentSport.getInfo());
Glide.with(mContext).load(currentSport.getImageResourse()).into(mSportImage);
}
@Override
public void onClick(View v) {
//Get the object position which was clicked
Sport currentSport = mSportsData.get(getAdapterPosition());
Intent detailIntent = new Intent(mContext,DetailActivity.class);
detailIntent.putExtra("title", currentSport.getTitle());
detailIntent.putExtra("image_resource", currentSport.getImageResourse());
mContext.startActivity(detailIntent);
}
}
接收数据的类
public class DetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
TextView sportText = findViewById(R.id.header_detail);
ImageView sportImage = findViewById(R.id.sportImage);
sportText.setText(getIntent().getStringExtra("title"));
TypedArray sportsImageResources = getResources().obtainTypedArray(R.array.sports_images);
int position = getIntent().getIntExtra("image_resource",0);
Glide.with(this).load(sportsImageResources.getResourceId(position,0)).into(sportImage);
sportsImageResources.recycle();
}
异常涉及接收数据的类
答案 0 :(得分:0)
在内部适配器中,您使用currentSport.getImageResourse()
Glide.with(mContext).load(currentSport.getImageResourse()).into(mSportImage);
很好。
但是在您的活动中,您尝试通过位置来加载它,而从意图中获取它
int position = getIntent().getIntExtra("image_resource",0);
但是您没有将位置设置为intent,而是设置了resourceid本身。因此image_resource
不是位置,它是可绘制的资源ID
detailIntent.putExtra("image_resource", currentSport.getImageResourse());
您应该使用故意发送的值来调用滑行
Glide.with(mContext).load(getIntent().getIntExtra("image_resource",0)).into(mSportImage);