Hi in the below getting the response from server win_probs getting value 30%,50%,80%.Based on the text percentage changing the background color.
but it is not working with my below code
can any one help me
layout:
<TextView
android:id="@+id/win_prob"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="30%"
android:textColor="@color/black"
android:layout_weight="1"
android:layout_marginLeft="150dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginRight="7dp"
/>
Code:
public void onBindViewHolder(OpportunityAdapter.MyViewHolder holder, final int position) {
holder.account_name.setText(account_names.get(position));
holder.potential_name.setText(potential_names.get(position));
holder.contact_name.setText(contact_names.get(position));
holder.potential_no.setText(potential_nos.get(position));
holder.location.setText(locations.get(position));
holder.item_name.setText(item_names.get(position));
holder.amount.setText(amounts.get(position));
holder.qty.setText(qtys.get(position));
holder.win_prob.setText(win_probs.get(position));
holder.mobile.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:123456789"));
mContext.startActivity(callIntent);
}
});
String winProbsPercent = win_probs.get(position);
if(winProbsPercent.equals("30 %")){
holder.win_prob.setBackgroundColor(ContextCompat.getColor(mContext, R.color.red));
} else if(winProbsPercent.equals("50 %")){
holder.win_prob.setBackgroundColor(ContextCompat.getColor(mContext, R.color.orange));
} else if(winProbsPercent.equals("80 %")){
holder.win_prob.setBackgroundColor(ContextCompat.getColor(mContext, R.color.green));
}
change your if like below. you are not comparing string. holder.win_prob
is view. you have to get text first.
if(holder.win_prob.getText().toString().equals("30%")){
holder.win_prob.setBackgroundColor(R.color.red);
}
Simply use win_probs.get(position)
to get the percentage that you get from server instead of holder.win_prob
which is a View
like below:
if(win_probs.get(position).equals("30%")){
holder.win_prob.setBackgroundColor(ContextCompat.getColor(mContext, R.color.red));
}
And you should check the conditions in if-else
fashion like this:
String winProbsPercent = win_probs.get(position);
if(winProbsPercent.equals("30%")){
holder.win_prob.setBackgroundColor(ContextCompat.getColor(mContext, R.color.red));
} else if(winProbsPercent.equals("50%")){
holder.win_prob.setBackgroundColor(ContextCompat.getColor(mContext, R.color.orange));
} else if(winProbsPercent.equals("80%")){
holder.win_prob.setBackgroundColor(ContextCompat.getColor(mContext, R.color.green));
}
Update: You can also use setBackgroundResource
to set background
if(winProbsPercent.equals("30%")){
holder.win_prob.setBackgroundResource(R.color.red);
} else if(winProbsPercent.equals("50%")){
holder.win_prob.setBackgroundResource(R.color.orange);
} else if(winProbsPercent.equals("80%")){
holder.win_prob.setBackgroundResource(R.color.green);
}
You need to change a little bit of code to work -
if(win_probs.get(position).trim().equals("30%")){
holder.win_prob.setBackgroundColor(ContextCompat.getColor(mContext, R.color.red));
}
Use getText().toString() to get the string from the textview and then compare.
if(holder.win_prob.getText().toString().equals("30%"))
holder.win_prob.setBackgroundColor(ContextCompat.getColor(mContext,R.color.red));
else if(holder.win_prob.getText().toString().equals("50%"))
holder.win_prob.setBackgroundColor(ContextCompat.getColor(mContext,R.color.orange));
else if(holder.win_prob.getText().toString().equals("80%"))
holder.win_prob.setBackgroundColor(ContextCompat.getColor(mContext,R.color.green));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.