目前我正在使用字符串数组来存储cardView中textview的'title'和'content',但是realm数据库听起来更适合这个目的。我怎么能做到这一点?这是我的回收器适配器:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private ClipboardManager myClipboard;
private ClipData myClip;
private Context context;
public List<CardItemModel> cardItems;
public RecyclerAdapter(List<CardItemModel> cardItems){
this.cardItems = cardItems;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
ImageView copyButton;
ImageView shareButton;
ToggleButton favButton;
TextView title;
TextView content;
public ViewHolder(View itemView) {
super(itemView);
this.title = (TextView)itemView.findViewById(R.id.card_title);
this.content = (TextView)itemView.findViewById(R.id.card_content);
this.copyButton= (ImageView) itemView.findViewById(R.id.copyButton);
this.shareButton=(ImageView) itemView.findViewById(R.id.shareButton);
this.favButton=(ToggleButton) itemView.findViewById(R.id.favButton);
favButton.setChecked(false);
favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(), R.mipmap.ic_launcher));
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.title.setText(cardItems.get(position).title);
holder.content.setText(cardItems.get(position).content);
holder.copyButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
myClipboard = (ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
myClip = ClipData.newPlainText("label", holder.content.getText().toString());
myClipboard.setPrimaryClip(myClip);
Toast.makeText(v.getContext(), "Copied to clipboard" , Toast.LENGTH_SHORT ).show();
}
});
holder.shareButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, holder.content.getText().toString());
v.getContext().startActivity(Intent.createChooser(share, "Share Text"));
}
});
holder.favButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton favButton, boolean isChecked){
if (isChecked)
favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(),R.mipmap.ic_launcher));
else
favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(), R.mipmap.ic_cart));
}
});
}
@Override
public int getItemCount() {
return cardItems.size();
}
}
答案 0 :(得分:1)
public class RecyclerAdapter extends RealmRecyclerViewAdapter<CardItemModel, RecyclerAdapter.ViewHolder> {
private ClipboardManager myClipboard;
private ClipData myClip;
private Context context;
public RecyclerAdapter(OrderedRealmCollection<CardItemModel> cardItems){
super(cardItems, true); // this is `realm.where(CardItemModel.class).findAllSortedAsync("someField");
}
public static class ViewHolder extends RecyclerView.ViewHolder{
ImageView copyButton;
ImageView shareButton;
ToggleButton favButton;
TextView title;
TextView content;
public ViewHolder(View itemView) {
super(itemView);
this.title = (TextView)itemView.findViewById(R.id.card_title);
this.content = (TextView)itemView.findViewById(R.id.card_content);
this.copyButton= (ImageView) itemView.findViewById(R.id.copyButton);
this.shareButton=(ImageView) itemView.findViewById(R.id.shareButton);
this.favButton=(ToggleButton) itemView.findViewById(R.id.favButton);
favButton.setChecked(false);
favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(), R.mipmap.ic_launcher));
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
CardItemModel cardItem = getData().get(position);
holder.title.setText(cardItem.title);
holder.content.setText(cardItem.content);
holder.copyButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
myClipboard = (ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
myClip = ClipData.newPlainText("label", holder.content.getText().toString());
myClipboard.setPrimaryClip(myClip);
Toast.makeText(v.getContext(), "Copied to clipboard" , Toast.LENGTH_SHORT ).show();
}
});
holder.shareButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, holder.content.getText().toString());
v.getContext().startActivity(Intent.createChooser(share, "Share Text"));
}
});
holder.favButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton favButton, boolean isChecked){
if (isChecked) favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(),R.mipmap.ic_launcher));
else favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(), R.mipmap.ic_cart));
}
});
}
}
而CardItemModel
将extends RealmObject
。适配器将显示Realm中的任何内容。