【问题标题】:Change color of the textview while comparing with a string which is in array与数组中的字符串进行比较时更改文本视图的颜色
【发布时间】:2023-03-28 23:25:01
【问题描述】:

这里 daysofInterest 为 ["Tuesday","Thursday","Friday","Friday"] 像这样,我使用 daysArray 将该字符串拆分为 "," 并检查数组,但我没有得到我想要的结果,请你修复它。并得到这样的 daysArray [Ljava.lang.String;@e856c3b

       String daysOfInterest=map.get("daysOfinterest");
       Log.d("DAYS OF INTEREST",daysOfInterest);
       String[] daysArray = daysOfInterest.split(",");
        Log.d("DAYS",daysArray.toString());
        for(int i=0;i<daysArray.length;i++){
            if(daysArray[i].equalsIgnoreCase("sunday")){
                tvSunday.setBackground(ContextCompat.getDrawable(ActivitySavedCareAlerts.this,R.drawable.style_circle_filled_green));
                tvSunday.setTextColor(ContextCompat.getColorStateList(getApplicationContext(),R.color.white));
            }
            if(daysArray[i].equalsIgnoreCase("monday")){
                tvMonday.setBackground(ContextCompat.getDrawable(ActivitySavedCareAlerts.this,R.drawable.style_circle_filled_green));
                tvMonday.setTextColor(ContextCompat.getColorStateList(getApplicationContext(),R.color.white));
            }
            if(daysArray[i].equalsIgnoreCase("tuesday")){
                tvTuesday.setBackground(ContextCompat.getDrawable(ActivitySavedCareAlerts.this,R.drawable.style_circle_filled_green));
                tvTuesday.setTextColor(ContextCompat.getColorStateList(getApplicationContext(),R.color.white));
            }
            if(daysArray[i].equalsIgnoreCase("wednesday")){
                tvWednesDay.setBackground(ContextCompat.getDrawable(ActivitySavedCareAlerts.this,R.drawable.style_circle_filled_green));
                tvWednesDay.setTextColor(ContextCompat.getColorStateList(getApplicationContext(),R.color.white));
            }
            if(daysArray[i].equalsIgnoreCase("thursday")){
                tvThursday.setBackground(ContextCompat.getDrawable(ActivitySavedCareAlerts.this,R.drawable.style_circle_filled_green));
                tvThursday.setTextColor(ContextCompat.getColorStateList(getApplicationContext(),R.color.white));
            }
            if(daysArray[i].equalsIgnoreCase("friday")){
                tvFriday.setBackground(ContextCompat.getDrawable(ActivitySavedCareAlerts.this,R.drawable.style_circle_filled_green));
                tvFriday.setTextColor(ContextCompat.getColorStateList(getApplicationContext(),R.color.white));
            }
            if(daysArray[i].equalsIgnoreCase("saturday")){
                tvSaturday.setBackground(ContextCompat.getDrawable(ActivitySavedCareAlerts.this,R.drawable.style_circle_filled_green));
                tvSaturday.setTextColor(ContextCompat.getColorStateList(getApplicationContext(),R.color.white));

            }
        }

        return convertView;
    }

【问题讨论】:

  • 使用 else if 而不是 if
  • 有两个错误
  • 请问你能指出我的错误吗?
  • 我说的你试过了吗

标签: java android arrays string textview


【解决方案1】:

有两个错误

1) 你没有在任何地方打破循环。所以它每次都会从 i=o 变为 i=length 并且分配给颜色的值将是最后一个值。从 if 语句返回

2) 在 setClorText 方法中,您使用白色作为参数。如果你想改变颜色,试试不同的颜色

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多