Android 根据从 firebase 接收到的数据加载带有可绘制对象的 imageView
Android load imageView with drawable depending on data received from firebase
根据从 Firebase 数据库接收到的字符串数据,我无法找到将图像从可绘制文件夹设置为 imageView 的方法。
我像这样从 Firebase 数据库获取值:
DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("Persons");
Query personsQuery = personsRef.orderByKey();
FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<Adapter>().setQuery(personsQuery, Adapter.class).build();
mAdapter = new FirebaseRecyclerAdapter<Adapter, Tab1.MyViewHolder>(personsOptions) {
@Override
protected void onBindViewHolder(Tab1.MyViewHolder holder, final int position, final Adapter model) {
holder.setTitle(model.getTitle());
holder.setDate(model.getDate());
}
@Override
public Tab1.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_tab1, parent, false);
return new Tab1.MyViewHolder(view);
}
};
目标是根据从 Firebase 加载的数据,使用可绘制文件夹中的图像填充 RecyclerView 中的图像视图。类似于:如果从 firebase = 1 加载“title”,则将 imageView 设置为 R.drawable.thisPerson1.
如果标题是 0..10 这样的数字,那么您可以尝试创建 res 数组:
如果 1..10 则尝试使用 model.getTitle() - 1
等
<integer-array name="some_name">
<item>@drawable/thisPerson1</item>
<item>@drawable/thisPerson2</item>
<item>@drawable/thisPerson3</item>
<item>@drawable/thisPerson4</item>
</integer-array>
private TypedArray imagesArray = getResources().obtainTypedArray(R.array.some_name);
private int image = imagesArray.getResourceId(imagesArray.getIndex(title), defaultValue)
ImageView.setBackgroundResource(image)
imagesArray.recycle()
您可以将收到的字符串附加到 ImageView 资源 ID
DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("Persons");
Query personsQuery = personsRef.orderByKey();
FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<Adapter>().setQuery(personsQuery, Adapter.class).build();
mAdapter = new FirebaseRecyclerAdapter<Adapter, Tab1.MyViewHolder>(personsOptions) {
@Override
protected void onBindViewHolder(Tab1.MyViewHolder holder, final int position, final Adapter model) {
holder.setTitle(model.getTitle());
holder.setDate(model.getDate());
holder.yourImageView.setImageResource(R.drawable+"."+model.getImage())
}
@Override
public Tab1.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_tab1, parent, false);
return new Tab1.MyViewHolder(view);
}
};
根据从 Firebase 数据库接收到的字符串数据,我无法找到将图像从可绘制文件夹设置为 imageView 的方法。
我像这样从 Firebase 数据库获取值:
DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("Persons");
Query personsQuery = personsRef.orderByKey();
FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<Adapter>().setQuery(personsQuery, Adapter.class).build();
mAdapter = new FirebaseRecyclerAdapter<Adapter, Tab1.MyViewHolder>(personsOptions) {
@Override
protected void onBindViewHolder(Tab1.MyViewHolder holder, final int position, final Adapter model) {
holder.setTitle(model.getTitle());
holder.setDate(model.getDate());
}
@Override
public Tab1.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_tab1, parent, false);
return new Tab1.MyViewHolder(view);
}
};
目标是根据从 Firebase 加载的数据,使用可绘制文件夹中的图像填充 RecyclerView 中的图像视图。类似于:如果从 firebase = 1 加载“title”,则将 imageView 设置为 R.drawable.thisPerson1.
如果标题是 0..10 这样的数字,那么您可以尝试创建 res 数组:
如果 1..10 则尝试使用 model.getTitle() - 1
等
<integer-array name="some_name">
<item>@drawable/thisPerson1</item>
<item>@drawable/thisPerson2</item>
<item>@drawable/thisPerson3</item>
<item>@drawable/thisPerson4</item>
</integer-array>
private TypedArray imagesArray = getResources().obtainTypedArray(R.array.some_name);
private int image = imagesArray.getResourceId(imagesArray.getIndex(title), defaultValue)
ImageView.setBackgroundResource(image)
imagesArray.recycle()
您可以将收到的字符串附加到 ImageView 资源 ID
DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("Persons");
Query personsQuery = personsRef.orderByKey();
FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<Adapter>().setQuery(personsQuery, Adapter.class).build();
mAdapter = new FirebaseRecyclerAdapter<Adapter, Tab1.MyViewHolder>(personsOptions) {
@Override
protected void onBindViewHolder(Tab1.MyViewHolder holder, final int position, final Adapter model) {
holder.setTitle(model.getTitle());
holder.setDate(model.getDate());
holder.yourImageView.setImageResource(R.drawable+"."+model.getImage())
}
@Override
public Tab1.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_tab1, parent, false);
return new Tab1.MyViewHolder(view);
}
};