2015-05-13 113 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.