Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android setTextColor可使用颜色值进行编程_Android_Android Layout_Android Xml - Fatal编程技术网

Android setTextColor可使用颜色值进行编程

Android setTextColor可使用颜色值进行编程,android,android-layout,android-xml,Android,Android Layout,Android Xml,我需要在适配器类中设置文本颜色,而不是在活动中,并使用colors.xml文件中记录的颜色值 代码是这样的:对于丢失的东西,我深表歉意 代码是这样的:对于丢失的东西,我深表歉意 代码是这样的:对于丢失的东西,我深表歉意 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myViewHolder> { private final LayoutInflater inflater; ArrayList<Hash

我需要在适配器类中设置文本颜色,而不是在活动中,并使用colors.xml文件中记录的颜色值

代码是这样的:对于丢失的东西,我深表歉意 代码是这样的:对于丢失的东西,我深表歉意 代码是这样的:对于丢失的东西,我深表歉意

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myViewHolder> {
private final LayoutInflater inflater;
ArrayList<HashMap<String, String>> productsHashMapList;

/*int[] images = {
        R.drawable.ic_launcher_foreground,
        R.drawable.ic_launcher_background,
        R.drawable.ic_launcher_foreground
};*/

public MyAdapter(Context context, ArrayList<HashMap<String, String>> productsJsonList){
    inflater = LayoutInflater.from(context);
    this.productsHashMapList = productsJsonList;

}

@Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = inflater.inflate(R.layout.list_row,parent,false);
    myViewHolder holder = new myViewHolder(view);

    return holder;
}

@Override
public void onBindViewHolder(myViewHolder holder, int position) {
    holder._productName.setText(productsHashMapList.get(position).get("name"));
    holder._productName.setTextColor(getResources().getColor(R.color.colorRed));

}

@Override
public int getItemCount() {
    return productsHashMapList.size();
}

public class myViewHolder extends RecyclerView.ViewHolder {

    ImageView   _imgview;
    TextView    _productName;


    public myViewHolder(View itemView) {

        super(itemView);
        _imgview            = (ImageView) itemView.findViewById(R.id.logo);
        _productName           = (TextView) itemView.findViewById(R.id.productName);
    }
}
公共类MyAdapter扩展了RecyclerView.Adapter{ 私人充气机; ArrayList ProductsShashMapList; /*int[]图像={ R.drawable.ic_发射器_前景, R.drawable.ic_启动器_背景, R.drawable.ic_发射器_前景 };*/ 公共MyAdapter(上下文上下文,ArrayList productsJsonList){ 充气器=充气器。从(上下文); this.productsHashMapList=productsJsonList; } @凌驾 公共myViewHolder onCreateViewHolder(视图组父级,int-viewType){ 视图=充气机。充气(右布局。列表行,父项,false); myViewHolder=新的myViewHolder(视图); 报税表持有人; } @凌驾 公共无效onBindViewHolder(myViewHolder,int位置){ holder.\u productName.setText(productsHashMapList.get(position.get(“名称”)); holder.\u productName.setTextColor(getResources().getColor(R.color.colorRed)); } @凌驾 public int getItemCount(){ 返回productsHashMapList.size(); } 公共类myViewHolder扩展了RecyclerView.ViewHolder{ ImageView _imgview; TextView(产品名称); 公共myViewHolder(查看项目视图){ 超级(项目视图); _imgview=(ImageView)itemView.findViewById(R.id.logo); _productName=(TextView)itemView.findViewById(R.id.productName); } } }

尝试使用

textViewOBJ.setTextColor(ContextCompat.getColor(context, R.color.your_color_code));
FYI

你应该通过

怎么做

通过适配器类#Contsructor

 ArrayList<HashMap<String, String>> productsHashMapList;
   Context context;

public MyAdapter(Context contextOBJ, ArrayList<HashMap<String, String>> productsJsonList){
    this.context=contextOBJ;
    inflater = LayoutInflater.from(context);
    this.productsHashMapList = productsJsonList;

}
arraylistproductsShashMapList;
语境;
公共MyAdapter(上下文contextOBJ、ArrayList productsJsonList){
this.context=contextOBJ;
充气器=充气器。从(上下文);
this.productsHashMapList=productsJsonList;
}

textView.setTextColor(getResources().getColor(R.color.color))


根据prashanth verma anser,在contsructor中初始化上下文。 然后在代码中使用它


textView.setTextColor(context.getResources().getColor(R.color.color))

首先,在活动中初始化适配器时,通过活动中适配器的构造函数传递上下文,并在适配器中设置上下文:

this.context = context; // set this in the constructor.
然后,设置文本颜色:

textView.setTextColor(context.getResources().getColor(R.color.white)); //whatever your color

始终在
RecyclerView.ViewHolder
类中设置文本颜色,而不是在适配器类的
onBindViewHolder
方法中,查看以下解决方案

class MyViewHolder extends RecyclerView.ViewHolder {
 private TextView YOUR_TEXT_VIEW;
        MyViewHolder(View view) {
            super(view);
        YOUR_TEXT_VIEW = view.findViewById(R.id.YOUR_TEXT_VIEW);

        YOUR_TEXT_VIEW.setTextColor(ContextCompat.getColor(YOUR_APP_CONTEXT, R.color.your_color_code));

        }


    }

在这里添加您的适配器代码@阿纳斯,请接受或支持以下任何可行的解决方案,适用于u。出现此消息:无法解析符号“上下文”。我想这是因为在一节课上没有activity@Anas检查我编辑的答案。无法根据prashanth verma anser解析方法“getResources(),请在contsructor中初始化上下文。