我正在尝试从drawable获取位图我使用此代码,但它无法正常工作,因为我无法在静态中使用上下文
这是我这堂课的完整代码:
package com.onazifi.shelf;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import org.antlr.runtime.BitSet;
import java.io.IOException;
import java.io.InputStream;
public class BookItem {
private String author;
private String title;
private Bitmap image;
Context context;
public BookItem() {
// TODO Auto-generated constructor stub
}
public BookItem(String _author, String _title , Bitmap _image) {
this.author = _author;
this.title = _title;
this.image = _image;
}
public String getAuthor() {
return this.author;
}
public void setAuthor(String _author) {
this.author = _author;
}
public String getTitle() {
return this.title;
}
public void setTitle(String _title) {
this.title = _title;
}
public Bitmap getImage() {
return this.image;
}
public void setImage(Bitmap _image) {
this.image = _image;
}
Bitmap _image = BitmapFactory.decodeResource(context.getResources(),
R.drawable.gbrankhalil2);
public static final BookItem[] ALL_BOOKS={
new BookItem("1","test" ,BitmapFactory.decodeResource(context.getResources(),
R.drawable.gbrankhalil2) )
};
}

问题出在这里
BitmapFactory.decodeResource(context.getResources(),
R.drawable.gbrankhalil2)

我无法在静态内调用上下文,如何使用位图从静态内部调用drawable
更新:
这是数组列表存在的地方
public class Library {
private ArrayList<BookItem> arrayBookItem;
public static final int AUTHOR = 1;
public static final int TITLE = 2;
public static final int IMAGE = 3;
public static final int RATE = 4;
public static final int DOWNLOAD_DATE =5;
public Library() {
arrayBookItem = new ArrayList<BookItem>();
}
public void setColectionBookItem(ArrayList<BookItem> _array) {
this.arrayBookItem = _array;
}
public void addBookItem(BookItem _bi) {
this.arrayBookItem.add(_bi);
}
public ArrayList<ArrayList<BookItem>> groupbyArrayBookItem(int type) {
BookItem[] books = BookItem.ALL_BOOKS;
ArrayList<ArrayList<BookItem>> groupList = new ArrayList<ArrayList<BookItem>>();
String getType = "";
switch (type) {
case AUTHOR:
getType = "bookitem.getAuthor()";
break;
case TITLE:
getType = "bookitem.getTitle()";
break;
case IMAGE:
getType = "bookitem.getImage";
break;
case DOWNLOAD_DATE:
getType = "bookitem.getDownloadDate()";
break;
case RATE:
getType = "bookitem.getRate()";
break;
default:
return groupList;
}
/*
* books is a object of BookItem
* bookitem is item for point to list
* getType is a string value for set type of grouping
* groupbyArrayBookItem return back array of array of items
*/
Iterable<Group> groups =
from("bookitem").in(books).group("bookitem")
.by(getType).into("g").select("g");
for (Group group : groups) {
ArrayList<BookItem> obj = new ArrayList<BookItem>();
for (Object Item : group.getGroup()) {
obj.add((BookItem) Item);
}
groupList.add(obj);
}
return groupList;
}
}
&#13;
答案 0 :(得分:0)
将context
传递到constructor
,然后使用它。
public BookItem(Context context, String _author, String _title , Bitmap _image) {
this.context = context;
this.author = _author;
this.title = _title;
this.image = _image;
}
OR
public BookItem(Context context) {
this.context = context;
}
然后使用它。
BitmapFactory.decodeResource(context.getResources(),
R.drawable.gbrankhalil2)
在任何活动中调用它。
BookItem item = new BookItem(MainActivity.this, _author, _title, _image);
OR 如果是片段
BookItem item = new BookItem(getContext(), _author, _title, _image);