2015-05-13 142 views
0

我需要使用RecyclerView將圖像設置爲壁紙。在我的適配器我使用這個代碼:使用RecyclerView設置壁紙

@Override 
    public void onBindViewHolder(ViewHolder viewHolder, final int i) { 
     final GridItem nature = mItems.get(i); 
     viewHolder.tvspecies.setText(nature.getName()); 
     viewHolder.imgThumbnail.setImageResource(nature.getThumbnail()); 

     viewHolder.imgThumbnail.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Log.i("CLick",nature.toString()); 
       try { 
        wallpaper.setResource(mItems.get(i)); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

這樣的onClick我應該設置圖片作爲壁紙,但在「setResource」字我有此錯誤:

The method setResource(int) in the type WallpaperManager is not applicable for the arguments (GridItem) 

我怎麼可以設置的項目從適配器中選擇作爲壁紙?

+0

這將是有益的,如果你能爲的GridItem類粘貼代碼 –

回答

0

您的程序不能正常工作的原因,方法setResource(int)需要int類型作爲參數,而不是GridItem類型。

閱讀更多從documentation

Change the current system wallpaper to the bitmap in the given resource. The resource is opened as a raw data stream and copied into the wallpaper; it must be a valid PNG or JPEG image.

還要確保你的應用程序有權限設置壁紙在清單

This method requires the caller to hold the permission SET_WALLPAPER.