Android Studio/Firestore:Firestore Boolean 在 RecyclerView 中的 TextView 的机会颜色?
Android Studio/Firestore: Chance Color of TextView in RecyclerView by Firestore Boolean?
在我的 Firestore 云存储中,我在 Products/Productid/ 中得到了这个布尔字段:"isOffer".
我想更改我的 RecyclerView ProductItems 中的 TextView 字符串“mPrice”的颜色,关于“isOffer”是真还是假,但我不知道如何处理。
方法 createRecycler():
public void createRecycler(){
mRecyclerView = findViewById(R.id.product_selection_list);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
Query query = current_productRef.whereEqualTo("isAvailable", true);
FirestoreRecyclerOptions<product_selection_Item> options = new FirestoreRecyclerOptions.Builder<product_selection_Item>()
.setQuery(query, product_selection_Item.class)
.build();
mAdapter = new product_selection_Adapter(options);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new product_selection_Adapter.OnItemClickListener() {
@Override
public void OnItemClick(DocumentSnapshot documentSnapshot, int position) {
String id = documentSnapshot.getId();
Intent intent = new Intent(product_selection.this, product_fill.class);
intent.putExtra("Product_ID", id);
startActivity(intent);
}
});
}
我的 Recycler 适配器非常基础:
public class product_selection_Adapter extends FirestoreRecyclerAdapter<product_selection_Item, product_selection_Adapter.product_selection_ViewHolder> {
private OnItemClickListener mListener;
public interface OnItemClickListener {
void OnItemClick(DocumentSnapshot documentSnapshot, int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public class product_selection_ViewHolder extends RecyclerView.ViewHolder {
public TextView mProductname;
public TextView mFat_Value;
public TextView mPrice;
public product_selection_ViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
mProductname = itemView.findViewById(R.id.product_selection_name);
mFat_Value = itemView.findViewById(R.id.product_selection_fat);
mPrice = itemView.findViewById(R.id.product_selection_price);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.OnItemClick(getSnapshots().getSnapshot(position), position);
}
}
}
});
}
}
public product_selection_Adapter(@NonNull FirestoreRecyclerOptions<product_selection_Item> options) {
super(options);
}
@NonNull
@Override
public product_selection_ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_selection_item, parent, false);
product_selection_ViewHolder ViewHolder = new product_selection_ViewHolder(v, mListener);
return ViewHolder;
}
@SuppressLint("SetTextI18n")
@Override
protected void onBindViewHolder(@NonNull product_selection_ViewHolder holder, int position, @NonNull product_selection_Item model) {
holder.mProductname.setText(model.getmProductname());
holder.mFat_Value.setText(String.valueOf(model.getmFat_Value() + "% Fett"));
holder.mPrice.setText(String.valueOf(model.getmPrice().get(0) + " €"));
}
}
你能帮帮我吗?
提前致谢!
我解决了
我需要在我的 Product_Selection_Item - Class “isOffer” 中创建一个新属性 getter “getisOffer”。
之后,我在适配器内部简单地创建了一个新的布尔变量“isOffer”,它获取当前布尔值。就像:
protected void onBindViewHolder(@NonNull product_selection_ViewHolder holder, int position, @NonNull product_selection_Item model) {
holder.mProductname.setText(model.getmProductname());
holder.mFat_Value.setText(String.valueOf(model.getmFat_Value() + "% Fett"));
holder.mPrice.setText(String.valueOf(model.getmPrice().get(0) + " €"));
holder.isOffer = model.getisOffer();
if(holder.isOffer){
holder.mPrice.setTextColor(Color.parseColor("#D90000"));
}
else{
holder.mPrice.setTextColor(Color.parseColor("#000000"));
}
}
不知道这是否是最佳解决方案,但它对我有用! :)
编辑:
这只能这样工作,因为我正在使用 FirestoreUI 和 FirestoreRecyclerAdapter。
此处,Firestore 存储中具有相同名称的 Product_Selection_Item 属性的每个 getter 将自动连接到 Firestore。
这就是为什么我可以做到 "holder.isOffer = model.getisOffer();"
在我的 Firestore 云存储中,我在 Products/Productid/ 中得到了这个布尔字段:"isOffer".
我想更改我的 RecyclerView ProductItems 中的 TextView 字符串“mPrice”的颜色,关于“isOffer”是真还是假,但我不知道如何处理。
方法 createRecycler():
public void createRecycler(){
mRecyclerView = findViewById(R.id.product_selection_list);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
Query query = current_productRef.whereEqualTo("isAvailable", true);
FirestoreRecyclerOptions<product_selection_Item> options = new FirestoreRecyclerOptions.Builder<product_selection_Item>()
.setQuery(query, product_selection_Item.class)
.build();
mAdapter = new product_selection_Adapter(options);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new product_selection_Adapter.OnItemClickListener() {
@Override
public void OnItemClick(DocumentSnapshot documentSnapshot, int position) {
String id = documentSnapshot.getId();
Intent intent = new Intent(product_selection.this, product_fill.class);
intent.putExtra("Product_ID", id);
startActivity(intent);
}
});
}
我的 Recycler 适配器非常基础:
public class product_selection_Adapter extends FirestoreRecyclerAdapter<product_selection_Item, product_selection_Adapter.product_selection_ViewHolder> {
private OnItemClickListener mListener;
public interface OnItemClickListener {
void OnItemClick(DocumentSnapshot documentSnapshot, int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public class product_selection_ViewHolder extends RecyclerView.ViewHolder {
public TextView mProductname;
public TextView mFat_Value;
public TextView mPrice;
public product_selection_ViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
mProductname = itemView.findViewById(R.id.product_selection_name);
mFat_Value = itemView.findViewById(R.id.product_selection_fat);
mPrice = itemView.findViewById(R.id.product_selection_price);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.OnItemClick(getSnapshots().getSnapshot(position), position);
}
}
}
});
}
}
public product_selection_Adapter(@NonNull FirestoreRecyclerOptions<product_selection_Item> options) {
super(options);
}
@NonNull
@Override
public product_selection_ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_selection_item, parent, false);
product_selection_ViewHolder ViewHolder = new product_selection_ViewHolder(v, mListener);
return ViewHolder;
}
@SuppressLint("SetTextI18n")
@Override
protected void onBindViewHolder(@NonNull product_selection_ViewHolder holder, int position, @NonNull product_selection_Item model) {
holder.mProductname.setText(model.getmProductname());
holder.mFat_Value.setText(String.valueOf(model.getmFat_Value() + "% Fett"));
holder.mPrice.setText(String.valueOf(model.getmPrice().get(0) + " €"));
}
}
你能帮帮我吗?
提前致谢!
我解决了
我需要在我的 Product_Selection_Item - Class “isOffer” 中创建一个新属性 getter “getisOffer”。
之后,我在适配器内部简单地创建了一个新的布尔变量“isOffer”,它获取当前布尔值。就像:
protected void onBindViewHolder(@NonNull product_selection_ViewHolder holder, int position, @NonNull product_selection_Item model) {
holder.mProductname.setText(model.getmProductname());
holder.mFat_Value.setText(String.valueOf(model.getmFat_Value() + "% Fett"));
holder.mPrice.setText(String.valueOf(model.getmPrice().get(0) + " €"));
holder.isOffer = model.getisOffer();
if(holder.isOffer){
holder.mPrice.setTextColor(Color.parseColor("#D90000"));
}
else{
holder.mPrice.setTextColor(Color.parseColor("#000000"));
}
}
不知道这是否是最佳解决方案,但它对我有用! :)
编辑:
这只能这样工作,因为我正在使用 FirestoreUI 和 FirestoreRecyclerAdapter。
此处,Firestore 存储中具有相同名称的 Product_Selection_Item 属性的每个 getter 将自动连接到 Firestore。
这就是为什么我可以做到 "holder.isOffer = model.getisOffer();"