如果条件在RecyclerView onBindViewHolder中

时间:2019-01-03 06:20:46

标签: java android android-recyclerview

我正在尝试在onBindViewHolder()上设置if else语句,以便它检查状态,然后根据找到的状态设置文本和文本颜色。

我已经尝试过,但这只是为我提供了RecyclerView中每个项目的第一个条件的结果。

这是我的Adapter代码:

    public class PreviousSettingsAdapter extends RecyclerView.Adapter<PreviousSettingsAdapter.PreviousSettingsViewHolder> {
private List<Setting> SettingsModelList;
private Context context;

public PreviousSettingsAdapter(Context context, List<Setting> SettingsModelList) {
    this.SettingsModelList = SettingsModelList;
    this.context = context;
}

public class PreviousSettingsViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.date)
    TextView date;
    @BindView(R.id.Setting_id)
    TextView SettingId;
    @BindView(R.id.status)
    TextView status;

    public PreviousSettingsViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
}

@NonNull
@Override
public PreviousSettingsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.placed_Setting_items, parent, false);
    return new PreviousSettingsViewHolder(itemView);

}

@Override
public void onBindViewHolder(@NonNull PreviousSettingsViewHolder holder, int position) {
    Setting singleSetting = SettingsModelList.get(position);
    holder.date.setText(singleSetting.getCreatedAt());
    holder.SettingId.setText("Setting Id: " + String.valueOf(singleSetting.getId()));


    boolean paymentStatus = singleSetting.getConfirmed();
    boolean fulfilledStatus = singleSetting.getProcessed();

    if (paymentStatus && !fulfilledStatus ) {
        holder.status.setText(R.string.processed);
        holder.status.getResources().getColor(R.color.colorOrange);

    } else
     if (!paymentStatus && !fulfilledStatus) {
        holder.status.setText(R.string.unchanged);
        holder.status.getResources().getColor(R.color.colorRed);

    } else if(paymentStatus && fulfilledStatus){
        holder.status.setText(R.string.delivered);
        holder.status.getResources().getColor(R.color.colorGreen);


    }else{
        holder.status.setText(" ");
    }


    }


@Override
    public int getItemCount () {
        return SettingsModelList.size();
    }
}

1 个答案:

答案 0 :(得分:0)

调试并友好地看到paymentStatusfulfilledStatus的值。正如您所说,它可能正在满足这些条件的情况下进入if循环。