简体   繁体   English

如何从处理程序更改ListView项目的文本颜色和图像颜色

[英]How to change listview item text color and image color from handler

I am new for android, I have ListView with custom adapter, I pass one string if matches in ListView item want to change list item text color from Activity . 我是Android新手,我具有带自定义适配器的ListView ,如果ListView项目中的匹配项想要更改Activity列表项目文本颜色,则传递一个字符串。

Here my code: 这是我的代码:

MyActivity: MyActivity:

 public void handleResult(String rawResult) { if(Utility.isNotNull(rawResult.getText().toString())) { for(int i=0;i<listView.getAdapter().getCount();i++){ if(rawResult.equals(listItems.get(i).getStockItems())){ // listView.getChildAt(i).setBackgroundColor(ContextCompat.getColor(context, R.color.hint)); /* Here I want to change list item text color*/ adapter.notifyDataSetChanged(); } } } } 

Thanks in advance! 提前致谢!

UI tasks can only be done on the UI Thread. UI任务只能在UI线程上完成。 If u want to run it from the handler, you have to define a runOnUiThread method. 如果要从处理程序中运行它,则必须定义一个runOnUiThread方法。 Take a look at this ans 看看这个ans

how to use runOnUiThread 如何使用runOnUiThread

try this code: 试试这个代码:

for(int i=0;i<listView.getChildCount();i++) {
  // yours code
  View item = listView.getChildAt(i);
  item.setBackgroundResource(R.drawable.your_image);  //change image
    ((TextView)item.findViewById(R.id.text1)).setTextColor(Color.RED);   //text1 is your cusotm listview item's  text id
    adapter.notifyDataSetChanged();?   
}

I assume you use TextView , To change his text color, first you need to get him, when you create your Item, add id to TextView 我假设您使用TextView ,要更改他的文本颜色,首先需要得到他,在创建Item时,将id添加到TextView

with xml 与XML

<TextView
    android:id="@+id/myId"...

Or if you use java 或者如果您使用Java

textView.setId(R.id.myId)

and in your code : 并在您的代码中:

((TextView)listView.getChildAt(i).findViewById(R.id.myId)).setTextColor(ContextCompat.getColor(context, R.color.hint));

If you want to set the Item Background with your Drawble Image you can use .setBackground(getResources().getDrawable(R.drawable.yourDrawble)); 如果要使用Drawble图像设置项目背景,则可以使用.setBackground(getResources()。getDrawable(R.drawable.yourDrawble));

In your model class add one paramter like this 在模型类中添加一个这样的参数

      public class DataHolder{

        private String StockItems;
        private int isSelected;

        public DataHolder(String StockItems, int isSelected) {
            this.StockItems = StockItems;
            this.isSelected = isSelected;

        }

        public String getStockItems() {
            return StockItems;
        }

        public void setStockItems(String StockItems) {
            this.StockItems = StockItems;
        }

        public int getiIsSelected() {
            return isSelected;
        }

        public void setIsSelected(String isSelected) {
            this.isSelected = isSelected;
        }
    }

initialise IsSelected zero 初始化IsSelected零

    public void handleResult(String rawResult) {    
            if(Utility.isNotNull(rawResult.getText().toString())) {
                for(int i=0;i<listView.getAdapter().getCount();i++){
                    if(rawResult.equals(listItems.get(i).getStockItems())){   
                        listItems.get(i).setIsSelected(1);
                        adapter.notifyDataSetChanged();                                                      
                    }
                }
            }
        }

In your cusom adapter class check 在cusom适配器类中检查

      if(listItems.get(i).getiIsSelected()==1)
      {
            //set red text color
      }
      else
      {
            //set black text color
      }

Make one method in your Adapter class to update text color and create one flag in Adapter that is initially false ,Use below method to do this 在Adapter类中创建一个方法来更新文本颜色,并在Adapter中创建一个最初为false的标志,请使用以下方法来执行此操作

boolean isChangeColor = false;
String  colorCode = "#FFFFFF";

private void updateTextColor(boolean isChangeColor , String colorCode) {
    this.isChangeColor=isChangeColor;
    this.colorCode=colorCode;
    notifyDataSetChanged();
        
}

And in getView() 并在getView()中

if(isChangeColor) {
    textView.setTextColor(Color.parseColor(colorCode));
} else {
    colorCode = "#FFFFFF";
    textView.setTextColor(Color.parseColor(colorCode));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM