2016-03-06 42 views
-2

得到位圖我試圖得到繪製我使用此代碼位,但其沒有工作,因爲我不能使用內部上下文中的靜態我試圖從一個繪製

這是我的全部代碼FO這類:

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 { 
 

 
\t private String author; 
 
\t private String title; 
 
\t private Bitmap image; 
 

 
\t Context context; 
 

 
\t public BookItem() { 
 
\t \t // TODO Auto-generated constructor stub 
 
\t } 
 

 
\t public BookItem(String _author, String _title , Bitmap _image) { 
 
\t \t this.author = _author; 
 
\t \t this.title = _title; 
 
\t \t this.image = _image; 
 
\t } 
 

 
\t public String getAuthor() { 
 
\t \t return this.author; 
 
\t } 
 

 
\t public void setAuthor(String _author) { 
 
\t \t this.author = _author; 
 
\t } 
 

 
\t public String getTitle() { 
 
\t \t return this.title; 
 
\t } 
 

 
\t public void setTitle(String _title) { 
 
\t \t this.title = _title; 
 
\t } 
 

 
\t public Bitmap getImage() { 
 
\t \t return this.image; 
 
\t } 
 

 
\t public void setImage(Bitmap _image) { 
 
\t \t this.image = _image; 
 
\t } 
 

 

 
\t Bitmap _image = BitmapFactory.decodeResource(context.getResources(), 
 
\t \t \t R.drawable.gbrankhalil2); 
 

 

 

 
\t public static final BookItem[] ALL_BOOKS={ 
 

 
     new BookItem("1","test" ,BitmapFactory.decodeResource(context.getResources(), 
 
\t \t \t \t R.drawable.gbrankhalil2)) 
 
     
 

 
    }; 
 
}

的問題是在這裏

BitmapFactory.decodeResource(context.getResources(), 
 
\t \t \t \t R.drawable.gbrankhalil2)

我不能叫上下文中靜態的,我怎麼能叫繪製從內部靜態使用位圖

UPDATE:

這正是數組列表中存在

public class Library { 
 

 
\t private ArrayList<BookItem> arrayBookItem; 
 
\t public static final int AUTHOR = 1; 
 
\t public static final int TITLE = 2; 
 
\t public static final int IMAGE = 3; 
 

 
\t public static final int RATE = 4; 
 
\t public static final int DOWNLOAD_DATE =5; 
 

 
\t public Library() { 
 
\t \t arrayBookItem = new ArrayList<BookItem>(); 
 
\t } 
 

 
\t public void setColectionBookItem(ArrayList<BookItem> _array) { 
 
\t \t this.arrayBookItem = _array; 
 
\t } 
 

 
\t public void addBookItem(BookItem _bi) { 
 
\t \t this.arrayBookItem.add(_bi); 
 
\t } 
 

 
\t public ArrayList<ArrayList<BookItem>> groupbyArrayBookItem(int type) { 
 

 
\t \t BookItem[] books = BookItem.ALL_BOOKS; 
 
\t \t ArrayList<ArrayList<BookItem>> groupList = new ArrayList<ArrayList<BookItem>>(); 
 
\t \t String getType = ""; 
 
\t \t 
 
\t \t switch (type) { 
 
\t \t case AUTHOR: 
 
\t \t \t getType = "bookitem.getAuthor()"; 
 
\t \t \t break; 
 
\t \t case TITLE: 
 
\t \t \t getType = "bookitem.getTitle()"; 
 
\t \t \t break; 
 
\t \t \t case IMAGE: 
 
\t \t \t \t getType = "bookitem.getImage"; 
 
\t \t \t \t break; 
 
\t \t case DOWNLOAD_DATE: 
 
\t \t \t getType = "bookitem.getDownloadDate()"; 
 
\t \t \t break; 
 
\t \t case RATE: 
 
\t \t \t getType = "bookitem.getRate()"; 
 
\t \t \t break; 
 
\t \t default: 
 
\t \t \t return groupList; 
 
\t \t } 
 

 
\t \t /* 
 
\t \t * books is a object of BookItem 
 
\t \t * bookitem is item for point to list 
 
\t \t * getType is a string value for set type of grouping 
 
\t \t * groupbyArrayBookItem return back array of array of items 
 
\t \t */ 
 
\t \t Iterable<Group> groups = 
 
\t \t \t \t from("bookitem").in(books).group("bookitem") 
 
\t \t \t \t .by(getType).into("g").select("g"); 
 

 
\t \t for (Group group : groups) { 
 
\t \t \t ArrayList<BookItem> obj = new ArrayList<BookItem>(); 
 
\t \t \t for (Object Item : group.getGroup()) { 
 
\t \t \t \t obj.add((BookItem) Item); 
 
\t \t \t } 
 
\t \t \t groupList.add(obj); 
 
\t \t } 
 

 
\t \t return groupList; 
 
\t } 
 
}

回答

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); 
+0

我有完全試圖爲u說,但我仍然得到錯誤的非靜態字段背景不能從靜態字段 –

+0

引用填充的項目數組中你的活動或片段。更多關於你的錯誤和理解檢查這個帖子http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context – Omkar

+0

它的工作謝謝 –