Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.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 如何从Json结果为textview着色_Android_Json - Fatal编程技术网

Android 如何从Json结果为textview着色

Android 如何从Json结果为textview着色,android,json,Android,Json,我想从Json结果(下面的代码)设置颜色文本视图,如果statusspp是SPP textcolor是红色,如果statusspp是SP2D textcolor是绿色。你能帮我吗 这是我的完整代码: 适配器类 public class AdapterSpp extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private LayoutInflater in

我想从Json结果(下面的代码)设置颜色文本视图,如果statusspp是SPP textcolor是红色,如果statusspp是SP2D textcolor是绿色。你能帮我吗

这是我的完整代码: 适配器类

public class AdapterSpp extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    List<DataSpp> data= Collections.emptyList();
    DataSpp current;
    int currentPos=0;

    public AdapterSpp(Context context, List<DataSpp> data){
        this.context=context;
        inflater= LayoutInflater.from(context);
        this.data=data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=inflater.inflate(R.layout.container_spp, parent,false);
        MyHolder holder=new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        MyHolder myHolder= (MyHolder) holder;
        DataSpp current=data.get(position);
        myHolder.textSppName.setText(current.getTextSppName());
        myHolder.textType.setText(current.getTextType());
        myHolder.textSize.setText("Uraian : " + current.getTextSize());
        myHolder.textPrice.setText("Rp. " + current.getTextPrice());
        myHolder.textTgl.setText("Tgl.SPP : " + current.getTextTgl());
        myHolder.textPrice.setTextColor(ContextCompat.getColor(context, android.R.color.holo_red_light));
        if(current.getTextType().equalsIgnoreCase("SPP")){
            myHolder.textType.setTextColor(android.R.color.holo_red_light);
        } else if(current.getTextType().equalsIgnoreCase("SP2D")){
            myHolder.textType.setTextColor(android.R.color.holo_blue_light);
        }
    }

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


    class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView textSppName;
        TextView textSize;
        TextView textType;
        TextView textPrice;
        TextView textTgl;

        public MyHolder(View itemView) {
            super(itemView);
            textSppName = (TextView) itemView.findViewById(R.id.textFishName);
            textSize = (TextView) itemView.findViewById(R.id.textSize);
            textType = (TextView) itemView.findViewById(R.id.textType);
            textPrice = (TextView) itemView.findViewById(R.id.textPrice);
            textTgl = (TextView) itemView.findViewById(R.id.textTgl);

        }

        @Override
        public void onClick(View v) {

            Toast.makeText(context, "You clicked an item", Toast.LENGTH_SHORT).show();

        }

    }
}

}我假设您正在文本视图中显示文本

 TextView textView = (TextView)findViewById(R.id.your_textview);
    if(sppData.statusName.equalsIgnoreCase("SPP"))
    {textView.setTextColor(Color.RED)}
    elseif(sppData.statusName.equalsIgnoreCase("SP2D"))
    {textView.setTextColor(Color.GREEN)}

我假设您正在文本视图中显示文本

 TextView textView = (TextView)findViewById(R.id.your_textview);
    if(sppData.statusName.equalsIgnoreCase("SPP"))
    {textView.setTextColor(Color.RED)}
    elseif(sppData.statusName.equalsIgnoreCase("SP2D"))
    {textView.setTextColor(Color.GREEN)}

在适配器中,尝试添加以下内容:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

  MyHolder myHolder= (MyHolder) holder;
  DataSpp current=data.get(position);
  myHolder.textSppName.setText(current.SppName);
  myHolder.textSize.setText("Status : " + current.sizeName);
  myHolder.textType.setText("Uraian : " + current.catName);
  myHolder.textPrice.setText("Rp. " + current.price);
  myHolder.textTgl.setText("Tgl.SPP : " + current.tgl);
  myHolder.textPrice.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));

  if(current.statusName.equalsIgnoreCase("SPP"))
  {
    myHolder.textType.setTextColor(Color.RED);
  } else if(current.statusName.equalsIgnoreCase("SP2D"))
  {
    myHolder.textType.setTextColor(Color.GREEN);
  }

}

在适配器中,尝试添加以下内容:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

  MyHolder myHolder= (MyHolder) holder;
  DataSpp current=data.get(position);
  myHolder.textSppName.setText(current.SppName);
  myHolder.textSize.setText("Status : " + current.sizeName);
  myHolder.textType.setText("Uraian : " + current.catName);
  myHolder.textPrice.setText("Rp. " + current.price);
  myHolder.textTgl.setText("Tgl.SPP : " + current.tgl);
  myHolder.textPrice.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));

  if(current.statusName.equalsIgnoreCase("SPP"))
  {
    myHolder.textType.setTextColor(Color.RED);
  } else if(current.statusName.equalsIgnoreCase("SP2D"))
  {
    myHolder.textType.setTextColor(Color.GREEN);
  }

}

创建这样的模型类

public clas DataSpp{
  private String textSppName;
  private String textSize;
  private String textType;
  private String textPrice;
  private String textTgl;

 //getters and setters
  public String getTextSppName() {
    return textSppName;
  }

  public void setTextSppName(String textSppName) {
    this.textSppName = textSppName;
  }

  public String getTextSize() {
    return textSize;
  }

  public void setTextSize(String textSize) {
    this.textSize = textSize;
  }

  public String getTextType() {
    return textType;
  }

  public void setTextType(String textType) {
    this.textType = textType;
  }

  public String getTextPrice() {
    return textPrice;
  }

  public void setTextPrice(String textPrice) {
    this.textPrice = textPrice;
  }

  public String getTextTgl() {
    return textTgl;
  }

  public void setTextTgl(String textTgl) {
    this.textTgl = textTgl;
  }
}
在json响应中

DataSpp SppData = new DataSpp();
sppData.setTextSppName(json_data.getString("namapenerima");
sppData.setTextSize(json_data.getString("uraianspp");
sppData.setTextType(json_data.getString("statusspp");
sppData.setTextPrice(formatter.format(json_data.getInt("jumlahtotal")));
sppData.setTextTgl(json_data.getString("tglspp");
在设置值后,在BindViewHolder中

if(current.textSppName.equalsIgnoreCase("SPP")){
    myHolder.textType.setTextColor(Color.RED);
} else if(current.textSppName.equalsIgnoreCase("SP2D")){
    myHolder.textType.setTextColor(Color.GREEN);
}

创建这样的模型类

public clas DataSpp{
  private String textSppName;
  private String textSize;
  private String textType;
  private String textPrice;
  private String textTgl;

 //getters and setters
  public String getTextSppName() {
    return textSppName;
  }

  public void setTextSppName(String textSppName) {
    this.textSppName = textSppName;
  }

  public String getTextSize() {
    return textSize;
  }

  public void setTextSize(String textSize) {
    this.textSize = textSize;
  }

  public String getTextType() {
    return textType;
  }

  public void setTextType(String textType) {
    this.textType = textType;
  }

  public String getTextPrice() {
    return textPrice;
  }

  public void setTextPrice(String textPrice) {
    this.textPrice = textPrice;
  }

  public String getTextTgl() {
    return textTgl;
  }

  public void setTextTgl(String textTgl) {
    this.textTgl = textTgl;
  }
}
在json响应中

DataSpp SppData = new DataSpp();
sppData.setTextSppName(json_data.getString("namapenerima");
sppData.setTextSize(json_data.getString("uraianspp");
sppData.setTextType(json_data.getString("statusspp");
sppData.setTextPrice(formatter.format(json_data.getInt("jumlahtotal")));
sppData.setTextTgl(json_data.getString("tglspp");
在设置值后,在BindViewHolder中

if(current.textSppName.equalsIgnoreCase("SPP")){
    myHolder.textType.setTextColor(Color.RED);
} else if(current.textSppName.equalsIgnoreCase("SP2D")){
    myHolder.textType.setTextColor(Color.GREEN);
}

你在哪里得到颜色以及你的文本视图在哪里?上面我给出了我的适配器和主活动代码..请帮助你在哪里得到颜色以及你的文本视图在哪里?上面我给出了我的适配器和主活动代码..请帮助如果你是对的,我会向文本视图显示它,但是文本视图代码在适配器类中,因此,数据无法读取。有什么解决方案吗,先生?您需要使用模型类来获取包含textView的类中的数据,因此,每当您有一个值时,它将在模型类中设置,然后您可以让对象从textView类调用getter方法来设置文本并实现上面为textView int给定的条件textView类只需创建一个带有字段的类并生成setter和getter方法(在android studio中可以轻松完成),然后在适配器类和JSON对象类中有两个模型类的对象,执行上述操作实际上我是java代码android的新手,你能给我一个示例代码吗。是的,我将它显示给textview,但textview代码在适配器类中,因此无法读取sppData。有什么解决方案吗,先生?你需要使用mod吗el类用于获取包含textView的类中的数据,因此每次您有一个值时,它都将在模型类中设置,然后您可以让对象从textView类调用getter方法来设置文本并实现上面为textView int给定的条件textView类只需创建一个带有字段和ge的类nerate setter和getter方法(可以在android studio中轻松完成),然后在adapter类和JSON对象类中有两个模型类的对象,执行上述操作实际上我是java代码android的新手,请给我一个示例代码。我已经完成了Jack,但仍然没有出现textSppName,这里我的代码:首先检查您的响应。同时发布您的JSON响应。@Rizkidedysusanto:可以吗e发布您在调用此函数后得到的响应我已经完成了Jack,但仍然没有显示textSppName,我的代码是:首先检查您的响应。同时发布您的Json响应。@RizkiDeddySusanto:您可以发布您在调用此函数后得到的响应吗