如何更改仅满足条件的文本视图的颜色?

huangapple go评论69阅读模式
英文:

How to change the color of only textviews that meet a condition?

问题

基本上,我有一个列表,我想要的是,对于满足条件的文本视图,它们的颜色会有所不同。

public class CustomTempAdapter extends ArrayAdapter<String> {
  private Context mContext;
  private int id;
  private List<String> items;

  public CustomTempAdapter(Context context, int textViewResourceId, List<String> list) {
    super(context, textViewResourceId, list);
    mContext = context;
    id = textViewResourceId;
    items = list;
  }

  @Override
  public View getView(int position, View v, ViewGroup parent) {
    View mView = v;
    if (mView == null) {
      LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      mView = vi.inflate(id, null);
    }

    TextView text = (TextView) mView.findViewById(R.id.tempView);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
      items.forEach(item -> {
        if (Double.parseDouble(item) > 100.00) {

          text.setBackgroundColor(Color.RED);
          int color = Color.argb(200, 255, 64, 64);
          text.setBackgroundColor(color);
        }
      });
    }

    return mView;
  }
}
CustomTempAdapter adapter = new CustomTempAdapter(this, R.layout.customTextView, normalStrings);
ListView list = findViewById(R.id.customListView);
list.setAdapter(adapter);

目前只显示空白的文本视图。我尝试了不同的方法。这次我使用了一个普通的 ArrayAdapter<String>,然后每当添加一个项目时:

View v;
for (int i = 0; i < list.getChildCount(); i++) {
  v = list.getChildAt(i);
  TextView tv = v.findViewById(R.id.tempView);
  if (tv != null && Double.parseDouble(tv.getText().toString()) > 100)
    tv.setTextColor(Color.parseColor("#FF0000"));
}

在这个方法中,我遍历所有文本视图,并尝试改变它们的颜色。但这并不起作用。

英文:

Well basically I have a list and what I want is that for textviews that meet a condition, their color would be different.

public class CustomTempAdapter extends ArrayAdapter&lt;String&gt; {
  private Context mContext;
  private int id;
  private List&lt;String&gt; items;

  public CustomTempAdapter(Context context, int textViewResourceId, List&lt;String&gt; list) {
    super(context, textViewResourceId, list);
    mContext = context;
    id = textViewResourceId;
    items = list;
  }

  @Override
  public View getView(int position, View v, ViewGroup parent) {
    View mView = v;
    if (mView == null) {
      LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      mView = vi.inflate(id, null);
    }

    TextView text = (TextView) mView.findViewById(R.id.tempView);

    if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.N) {
      items.forEach(item -&gt; {
        if (Double.parseDouble(item) &gt; 100.00) {

          text.setBackgroundColor(Color.RED);
          int color = Color.argb(200, 255, 64, 64);
          text.setBackgroundColor(color);
        }
      });
    }

    return mView;
  }
}
 CustomTempAdapter adapter = new CustomTempAdapter(this, R.layout.customTextView, normalStrings);
    ListView list = findViewById(R.id.customListView);
    list.setAdapter(adapter);

Currently this is only displaying blank textviews. I tried a different approach. This time using a normal ArrayAdapter&lt;String&gt;, then whenever an item is added:

View v;
for (int i = 0; i &lt; list.getChildCount(); i++) {
  v = list.getChildAt(i);
  TextView tv = v.findViewById(R.id.tempView);
  if (tv != null &amp;&amp; Double.parseDouble(tv.getText().toString()) &gt; 100)
    tv.setTextColor(Color.parseColor(&quot;#FF0000&quot;));
}

In this approach, I iterate through all the textviews and try to change their color. It does not work.

答案1

得分: 0

你可以将getView方法重写如下:

public View getView(int position, View v, ViewGroup parent) {
    View mView = v;
    if (mView == null) {
        LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = vi.inflate(id, null);
    }

    TextView text = (TextView) mView.findViewById(R.id.tempView);
    Double value = Double.parseDouble(items.get(position)); 
    if (value > 100.00) {
        int color = Color.argb(200, 255, 64, 64);
        text.setBackgroundColor(color);
    } else {
        text.setBackgroundColor(Color.GREEN);
    }
    return mView;
}
英文:

You could rewrite getView method as following:

public View getView(int position, View v, ViewGroup parent) {
        View mView = v;
        if (mView == null) {
            LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mView = vi.inflate(id, null);
        }

        TextView text = (TextView) mView.findViewById(R.id.tempView);
        Double value = Double.parseDouble(items.get(position)); 
        if (value &gt; 100.00) {
            int color = Color.argb(200, 255, 64, 64);
            text.setBackgroundColor(color);
        } else {
            text.setBackgroundColor(Color.GREEN);
        }
        return mView;
    }

huangapple
  • 本文由 发表于 2020年8月27日 08:54:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63607659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定