gpt4 book ai didi

java - 在 ViewAdapter 上加载图像资源

转载 作者:行者123 更新时间:2023-11-29 03:07:30 24 4
gpt4 key购买 nike

我有一个 ReciclerView,它使用名为“SimpleItemRecyclerViewAdapter”的适配器。 ReciclerView 中的每个项目都位于项目的“drawable”文件夹中。我能够在两个 TextView 上设置测试,但我不确定如何对图像执行此操作。这是我得到的输出...图像应该出现在带有 ImageView 小部件的黑色方 block 上。

enter image description here

这是我的适配器的代码:

package uk.co.example.calculator.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import uk.co.example.calculator.R;
import uk.co.example.calculator.ui.activities.Currency;

public class SimpleItemRecyclerViewAdapter extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.RecyclerViewSimpleTextViewHolder> {

// The items to display in your RecyclerView
private List<Currency> items;

// Provide a suitable constructor (depends on the kind of dataset)
public SimpleItemRecyclerViewAdapter(List<Currency> items) {
this.items = items;
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return this.items.size();
}

// Replace the contents of a view (invoked by the layout manager)
// Involves populating data into the item through holder
@Override
public void onBindViewHolder(RecyclerViewSimpleTextViewHolder holder, int position) {
Currency currency = items.get(position);
holder.getName().setText(currency.name);
holder.getSymbol().setText(currency.symbol);
holder.getAsset().?????????; // WHAT SHOULD I PUT HERE TO LOAD THE IMAGE FROM THE RAW FOLDER
}

// Create new items (invoked by the layout manager)
// Usually involves inflating a layout from XML and returning the holder
@Override
public RecyclerViewSimpleTextViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView = LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.currency_list_card_layout, viewGroup, false);
return new RecyclerViewSimpleTextViewHolder(itemView);
}


public class RecyclerViewSimpleTextViewHolder extends RecyclerView.ViewHolder {
private TextView name;
private TextView symbol;
private ImageView asset;

public RecyclerViewSimpleTextViewHolder(View v) {
super(v);
name = (TextView) v.findViewById(R.id.currency_name);
symbol = (TextView) v.findViewById(R.id.currency_symbol);
asset = (ImageView) v.findViewById(R.id.currency_asset);
}

public TextView getName() {
return name;
}

public TextView getSymbol() {
return symbol;
}

public ImageView getAsset() {
return asset;
}

}
}

如能提供有关如何显示可绘制文件夹中包含的图像的任何详细帮助,我们将不胜感激。

最佳答案

您可以从 onBindViewHolder 中访问您的 ImageView

例如,放入onBindViewHolder:

Resources resources = getResources();
holder.getAsset().setImageDrawable(resources.getDrawable(R.drawable.myimage));

关于java - 在 ViewAdapter 上加载图像资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31371584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com