如何在recyclelerView中将base64转换为图像

时间:2016-09-13 12:18:35

标签: android android-recyclerview

我正在开发一个应用程序,我正在使用recyclerView.In我正在调用我的服务来显示数据。但我的图像是Base64格式。我知道如何在没有recyclerview的情况下将Base64转换为图像,但我知道我不知道如何做到这一点。以下是我的主要活动代码

         if (null != data && status.getCode() != -101) {

                String StringData = "" + data;
                try {
                    JSONArray rootArray = new JSONArray(StringData);
                    int len = rootArray.length();
                    for (int i = 0; i < len; ++i) {
                        JSONObject json = rootArray.optJSONObject(i);

                        final DataJobs b1 = new DataJobs();
                        b1.ComapnayName = json.optString("CompanyName");
                        b1.DegreeName = json.optString("DegreeName");
                        b1.JobTitle = json.optString("JobTitle");
                        b1.Eventdate = json.optString("Eventdate");
                        b1.Comp_Req_Id=json.optInt("COMP_REQ_ID");
                          String img=json.optString("Image");
                        byte[] decodedString = Base64.decode(img, Base64.DEFAULT);
                        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                        image.setImageBitmap(decodedByte);

                        datajobs.add(b1);


                    }
                } catch (JSONException e) {
                    Toast.makeText(getContext(), e.toString(), Toast.LENGTH_LONG).show();

                }
            }
        }
    });

适配器代码

  public class EventsAdapter extends    RecyclerView.Adapter<EventsAdapter.MyViewHolder> {
public List<DataJobs> datajobs;
private ClickListener clicklistener = null;
public  class MyViewHolder extends RecyclerView.ViewHolder {

    public TextView JobTitle,DegreeName,ComapnayName,EventDate,Comp_Req_Id;
    public MyViewHolder(View view){
        super(view);

        ComapnayName=(TextView) view.findViewById(R.id.ComapnayName);
        DegreeName=(TextView) view.findViewById(R.id.DegreeName);
        JobTitle=(TextView) view.findViewById(R.id.JobTitle);
        EventDate=(TextView) view.findViewById(R.id.Eventdate);
        Comp_Req_Id=(TextView) view.findViewById(R.id.Comp_Req_Id);
    }

}

public EventsAdapter(List<DataJobs>datajobs){
    this.datajobs=datajobs;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.data_list, parent, false);

    return new MyViewHolder(itemView);
    }
     @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
     final DataJobs event = datajobs.get(position);
    holder.ComapnayName.setText(event.getComapnayName());
    holder.DegreeName.setText(event.getDegreeName());
    holder.JobTitle.setText(event.getJobTitle());
    holder.EventDate.setText(event.getEventdate());
    holder.Comp_Req_Id.setText(String.valueOf(event.getComp_Req_Id()));
    holder.itemView.setOnClickListener (new View.OnClickListener() {
    @Override
   public void onClick(View v)
    {

    int a=event.getComp_Req_Id();
    Intent i = new Intent(v.getContext(), SecondActivity.class);
    i.putExtra("Comp_Req_Id" , a);
    v.getContext().startActivity(i);
    //getActivity().finish();
    }
    });
    }

    }

datajobs的代码:

     public class DataJobs {
public String JobTitle,DegreeName,ComapnayName,Eventdate;
int Comp_Req_Id;

public DataJobs()
{

}
public DataJobs(String JobTitle,String PositionName,String   ComapnayName,String Eventdate,int Comp_Req_Id)
{
    this.JobTitle=JobTitle;
    this.DegreeName=PositionName;
    this.ComapnayName=ComapnayName;
    this.Eventdate=Eventdate;
    this.Comp_Req_Id=Comp_Req_Id;
}
public String getJobTitle(){
    return JobTitle;
}
public void setJobTitle(String Jobname){
    this.JobTitle=JobTitle;
}
public String getDegreeName(){
    return DegreeName;
}
public void setDegreeNamee(String DegreeName){
    this.DegreeName=DegreeName;
}
public String getComapnayName(){
    return ComapnayName;
}
public void setCompanyName(String CompanyName){
    this.ComapnayName=CompanyName;
}
public String getEventdate()
{
    return Eventdate;
}
public void setEventdate(String Eventdate)
{
    this.Eventdate=Eventdate;
}
public int getComp_Req_Id()
{
    return Comp_Req_Id;
}
public void setComp_Req_Id(int Comp_Req_Id)
{
    this.Comp_Req_Id=Comp_Req_Id;
}

     }

1 个答案:

答案 0 :(得分:0)

  1. 您已向DataJobs对象添加了Bitmap或String字段。
  2. 在Main中创建DataJobs列表时保存image / base64字符串。
  3. 您可以将ImageView添加到持有者中,并将Bitmap / String传递给它。 (就像你用其他值做的那样)