Android ListView Background Color Change Dependent on Value (Logic) not position or selected

StackOverflow https://stackoverflow.com/questions/23613633

  •  20-07-2023
  •  | 
  •  

Question

I am trying to display a list of information that I would like to have different background colors for each item/line in the ListView. I can change the background color of every line, but not each line to a different color. I have tried using multiple non-nested if statements to say set the background color and I does not work.

public View getView(int position, View convertView, ViewGroup parent) 
    {
        ViewGroup listItem;
        if(convertView == null)
        {
            listItem = (ViewGroup) getLayoutInflator().inflate(R.layout.list_item, null);
        }
        else
        {
            listItem = (ViewGroup) convertView;
        }

        //listItem.setClickable(true);
        //listItem.setOnClickListener(ViewBeerCL);
        listItem.setTag(getParent());
        String[] gradeInfo = gradeList.get(position).split(":");
        TextView gradeTV = (TextView) listItem.findViewById(R.id.gradeTV);
        TextView highGradeTV = (TextView) listItem.findViewById(R.id.highGradeTV);
        TextView lowGradeTV = (TextView) listItem.findViewById(R.id.lowGradeTV);

        String GradeLayoutStyle = gradeInfo[0];
        String aStr = gradeList.get(position).substring(1, 1);
        String bStr = "A";

        gradeTV.setText(gradeInfo[0]);
        highGradeTV.setText(gradeInfo[1]);
        lowGradeTV.setText(gradeInfo[2]);
        gradeTV.setBackgroundColor((gradeInfo[0] == bStr)? Color.TRANSPARENT:getResources().getColor(R.color.GradeA));
        highGradeTV.setBackgroundColor((gradeInfo[0] == bStr)? Color.TRANSPARENT:getResources().getColor(R.color.GradeA));
        lowGradeTV.setBackgroundColor((gradeInfo[0] == bStr)? Color.TRANSPARENT:getResources().getColor(R.color.GradeA));

        if(gradeTV.getText() == bStr)
        {
            SetColor(gradeTV, highGradeTV, lowGradeTV, getResources().getColor(R.color.GradeA), gradeInfo, "A");
        }
        if(GradeLayoutStyle == "B")
        {
            SetColor(gradeTV, highGradeTV, lowGradeTV, getResources().getColor(R.color.GradeB), gradeInfo, "B");
        }
        if(GradeLayoutStyle == "C")
        {
            SetColor(gradeTV, highGradeTV, lowGradeTV, getResources().getColor(R.color.GradeC), gradeInfo, "C");
        }
        if(GradeLayoutStyle == "D")
        {
            SetColor(gradeTV, highGradeTV, lowGradeTV, getResources().getColor(R.color.GradeD), gradeInfo, "D");
        }
        if(GradeLayoutStyle == "F")
        {
            SetColor(gradeTV, highGradeTV, lowGradeTV, getResources().getColor(R.color.GradeF), gradeInfo, "F");
        }

        return listItem;
    }

    private void SetColor(TextView grade, TextView high, TextView low, int color, String[] gradeInfo, String letter)
    {
        grade.setBackgroundColor((gradeInfo[0] == letter)? Color.TRANSPARENT:color);
        high.setBackgroundColor((gradeInfo[0] == letter)? Color.TRANSPARENT:color);
        low.setBackgroundColor((gradeInfo[0] == letter)? Color.TRANSPARENT:color);
    }

Yes, this is for homework and I am not looking for someone to do the work for me, I am looking for someone to help/show me how to figure this out. If someone could point me in the right direction, or let me know if this is even possible, that would be great!

Thanks,

Shawn

Was it helpful?

Solution

String comparisons need to be made using string.eqals(otherString). Using == will not work, as it compares the objects themselves, not the content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top