我打算保存此ImageView:
到我的RealmDatabase,但是当我想检索它时该图像没有出现。它应该显示在这里:
这是我的onSaveExpense代码
public void onSaveExpense() {
//TODO - BITMAP EXPENSE ICONS
if (mCategoriesSpinnerAdapter.getCount() > 0 ) {
if (!Util.isEmptyField(etTotal)) {
Category currentCategory = (Category) spCategory.getSelectedItem();
String total = etTotal.getText().toString();
String description = etDescription.getText().toString();
Bitmap bitmap = BitmapFactory.decodeByteArray(currentCategory.getImgico(),0,currentCategory.getImgico().length);
ByteArrayOutputStream imgbyte = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, imgbyte);
byte[] expenseIcons = imgbyte.toByteArray();
if (mUserActionMode == IUserActionsMode.MODE_CREATE) {
//TODO - CONSTRUCTOR expenseIcons
RealmManager.getInstance().save(new Expense(description, selectedDate,expenseIcons,mExpenseType, currentCategory, Float.parseFloat(total)), Expense.class);
} else {
Expense editExpense = new Expense();
editExpense.setId(mExpense.getId());
editExpense.setTotal(parseFloat(total));
editExpense.setDescription(description);
editExpense.setIconViewer(expenseIcons);
editExpense.setCategory(currentCategory);
editExpense.setDate(selectedDate);
RealmManager.getInstance().update(editExpense);
}
// update widget if the expense is created today
if (DateUtils.isToday(selectedDate)) {
Intent i = new Intent(getActivity(), ExpensesWidgetProvider.class);
i.setAction(ExpensesWidgetService.UPDATE_WIDGET);
getActivity().sendBroadcast(i);
}
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
dismiss();
} else {
DialogManager.getInstance().showShortToast(getString(string.error_total));
}
} else {
DialogManager.getInstance().showShortToast(getString(string.no_categories_error));
}
}
这是我的CategoriesSpinnerAdapter代码
public class CategoriesSpinnerAdapter extends ArrayAdapter<Category> {
Category[] categoriesList = null;
LayoutInflater inflater;
public CategoriesSpinnerAdapter(Activity context, Category[] categoriesList) {
super(context, R.layout.spinner_ui, categoriesList);
this.categoriesList = categoriesList;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.spinner_ui, parent, false);
Category category = categoriesList[position];
Bitmap bitmap = BitmapFactory.decodeByteArray(category.getImgico(),0,category.getImgico().length);
ImageView imgview = (ImageView)row.findViewById(R.id.spinnerimg);
TextView title = (TextView)row.findViewById(R.id.spinnertext);
title.setText(category.getName());
imgview.setImageBitmap(bitmap);
return row;
}
这是我的BaseExpenseAdapter代码(Cardview结果)
public class BaseExpenseAdapter<VH extends RecyclerView.ViewHolder> extends BaseExpenseRecyclerViewAdapter<BaseExpenseAdapter.BaseExpenseViewHolder> {
protected List<Expense> mExpensesList;
protected int lastPosition = -1;
protected int colorExpense;
protected int colorIncome;
protected String prefixExpense;
protected String prefixIncome;
private String titleTransitionName;
protected BaseViewHolder.RecyclerClickListener onRecyclerClickListener;
public BaseExpenseAdapter(Context context, BaseViewHolder.RecyclerClickListener onRecyclerClickListener) {
this.mExpensesList = ExpensesManager.getInstance().getExpensesList();
this.onRecyclerClickListener = onRecyclerClickListener;
this.colorExpense = ExpenseTrackerApp.getContext().getResources().getColor(R.color.colorAccentRed);
this.colorIncome = ExpenseTrackerApp.getContext().getResources().getColor(R.color.colorAccentGreen);
this.prefixExpense = ExpenseTrackerApp.getContext().getResources().getString(R.string.expense_prefix);
this.prefixIncome = ExpenseTrackerApp.getContext().getResources().getString(R.string.income_prefix);
this.titleTransitionName = ExpenseTrackerApp.getContext().getString(R.string.tv_title_transition);
}
@Override
public BaseExpenseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_expense_item, parent, false);
return new BaseExpenseViewHolder(v, onRecyclerClickListener);
}
@Override
public void onBindViewHolder(BaseExpenseViewHolder holder, int position) {
holder.itemView.setSelected(isSelected(position));
final Expense expense = mExpensesList.get(position);
Bitmap bitmaps = BitmapFactory.decodeByteArray(expense.getIconViewer(), 0, expense.getIconViewer().length);
holder.iconExpenses.setImageBitmap(bitmaps);
String prefix = "";
switch (expense.getType()) {
case IExpensesType.MODE_EXPENSES:
holder.tvTotal.setTextColor(colorExpense);
prefix = String.format(prefixExpense, Util.getFormattedCurrency(expense.getTotal()));
break;
case IExpensesType.MODE_INCOME:
holder.tvTotal.setTextColor(colorIncome);
prefix = String.format(prefixIncome, Util.getFormattedCurrency(expense.getTotal()));
break;
}
if (expense.getCategory() != null)holder.tvCategory.setText(expense.getCategory().getName());
if (expense.getDescription() != null && !expense.getDescription().isEmpty()) {
holder.tvDescription.setText(expense.getDescription());
holder.tvDescription.setVisibility(View.VISIBLE);
} else {
holder.tvDescription.setVisibility(View.GONE);
}
holder.tvTotal.setText(prefix);
holder.itemView.setTag(expense);
holder.iconExpenses.setImageBitmap(bitmaps);
ViewCompat.setTransitionName(holder.tvTotal, titleTransitionName);
}
@Override
public int getItemCount() {
return mExpensesList.size();
}
public void updateExpenses(List<Expense> mExpensesList) {
this.mExpensesList = mExpensesList;
notifyDataSetChanged();
}
protected void setAnimation(BaseExpenseViewHolder holder, int position) {
if (position > lastPosition) {
Animation animation = AnimationUtils.loadAnimation(ExpenseTrackerApp.getContext(), R.anim.push_left_in);
holder.itemView.startAnimation(animation);
lastPosition = position;
}
}
public static class BaseExpenseViewHolder extends BaseViewHolder {
public TextView tvCategory;
public TextView tvDescription;
public TextView tvTotal;
public ImageView iconExpenses;
public BaseExpenseViewHolder(View v, RecyclerClickListener onRecyclerClickListener) {
super(v, onRecyclerClickListener);
iconExpenses = (ImageView)v.findViewById(R.id.expenseico);
tvCategory = (TextView)v.findViewById(R.id.tv_category);
tvDescription = (TextView)v.findViewById(R.id.tv_description);
tvTotal = (TextView)v.findViewById(R.id.tv_total);
}
}
这是我的费用实体
public class Expense extends RealmObject {
@PrimaryKey
private String id;
private byte[] iconViewer;
private String description;
private Date date;
private @IExpensesType int type;
private Category category;
private float total;
public Expense() {
}
public Expense(String description, Date date ,byte[] iconViewer, @IExpensesType int type, Category category, float total) {
this.description = description;
this.iconViewer = iconViewer;
this.date = date;
this.type = type;
this.category = category;
this.total = total;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public byte[] getIconViewer() {
return iconViewer;
}
public void setIconViewer(byte[] iconViewer) {
this.iconViewer = iconViewer;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public float getTotal() {
return total;
}
public void setTotal(float total) {
this.total = total;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}