如何解决在 RecyclerView 中插入 Base64 中的照片
我正在制作一个 Recycler View,其中每个单元格都有一张照片,该照片应该从数据库提供的 Base64 字符串转换而来。问题是,我不知道如何设置。
我的用户类:
class userProfileModel
{
public int userId { get; set; }
public string username { get; set; }
public string userPhotobase64 { get; set; }
public Image userPhoto { get; set; }
我的回收站单元模板:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="190dp"
app:cardElevation="2dp"
android:layout_marginTop="20dp"
android:layout_height="190dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Refractored.Controls.CircleImageView
android:id="@+id/user_photo_in_interested_choice"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/photoplaceholder"
app:civ_border_color="#FF0199"
app:civ_border_width="2dp"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:id="@+id/username_text_in_interested_choice"
android:layout_marginTop="10dp"
android:gravity="center"
android:textStyle="bold"
android:text="Nazwa użytkownika"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_marginTop="10dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/deny_in_interested_choice"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"
android:src="@drawable/accept_user_photo"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:layout_width="40dp"
android:layout_height="40dp"/>
<ImageView
android:id="@+id/accept_in_interested_choice"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:src="@drawable/deny_user_photo"
android:layout_gravity="center_vertical"
android:layout_marginRight="20dp"
android:layout_width="40dp"
android:layout_height="40dp"/>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
我不知道如何在适配器中引用图像。我怎么能这样做?
适配器:
namespace DatingAppLicencjat.Adapters
{
class InterestedUsersAdapter : RecyclerView.Adapter
{
public event EventHandler<InterestedUsersAdapterClickEventArgs> ItemClick;
public event EventHandler<InterestedUsersAdapterClickEventArgs> ItemLongClick;
List<userProfileModel> userList;
public InterestedUsersAdapter(List<userProfileModel> data)
{
userList = data;
}
// Create new views (invoked by the layout manager)
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent,int viewType)
{
//Setup your layout here
View itemView = null;
itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.user_interested_selection_template,parent,false);
var vh = new InterestedUsersAdapterViewHolder(itemView,OnClick,OnLongClick);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
public override void OnBindViewHolder(RecyclerView.ViewHolder viewHolder,int position)
{
var user = userList[position];
// Replace the contents of the view with that element
var holder = viewHolder as InterestedUsersAdapterViewHolder;
holder.username_in_interested_choice.Text = user.username;
//holder.TextView.Text = items[position];
}
public override int ItemCount => userList.Count;
void OnClick(InterestedUsersAdapterClickEventArgs args) => ItemClick?.Invoke(this,args);
void OnLongClick(InterestedUsersAdapterClickEventArgs args) => ItemLongClick?.Invoke(this,args);
}
public class InterestedUsersAdapterViewHolder : RecyclerView.ViewHolder
{
public TextView username_in_interested_choice;
public CircleImageView user_photo_in_interested_choice;
public ImageView accept_user_in_interested_choice;
public ImageView deny_user_in_interested_choice;
public InterestedUsersAdapterViewHolder(View itemView,Action<InterestedUsersAdapterClickEventArgs> clickListener,Action<InterestedUsersAdapterClickEventArgs> longClickListener) : base(itemView)
{
username_in_interested_choice = (TextView)itemView.FindViewById(Resource.Id.username_text_in_interested_choice);
user_photo_in_interested_choice = (CircleImageView)itemView.FindViewById(Resource.Id.user_photo_in_interested_choice);
accept_user_in_interested_choice = (ImageView)itemView.FindViewById(Resource.Id.accept_in_interested_choice);
deny_user_in_interested_choice = (ImageView)itemView.FindViewById(Resource.Id.deny_in_interested_choice);
itemView.Click += (sender,e) => clickListener(new InterestedUsersAdapterClickEventArgs { View = itemView,Position = AdapterPosition });
itemView.LongClick += (sender,e) => longClickListener(new InterestedUsersAdapterClickEventArgs { View = itemView,Position = AdapterPosition });
}
}
public class InterestedUsersAdapterClickEventArgs : EventArgs
{
public View View { get; set; }
public int Position { get; set; }
}
}
解决方法
您可以在 Get/Set 中完成。我刚刚做了从/到 base64string 到 string 的第一步。您需要实际转换为图像。
class userProfileModel
{
public int userId { get; set; }
public string username { get; set; }
public string userPhotobase64 { get; set; }
private Image _userPhoto { get; set; }
public string userPhoto
{
get { return Convert.ToBase64String(_userPhoto); }
set { userPhoto = Convert.FromBase64String(value); }
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。