RecyclerView不显示

问题描述:

我正在制作一个应用程序,理论上它必须从我的内部存储器加载图像并将其显示在RecyclerView上。我想我正确地遵循了所有步骤,并且在我尝试了很多不成功的尝试之后。所以我认为将数据追赶到适配器的代码是错误的,但我想不出任何事情来验证。任何帮助,观察,建议非常感谢。这是我的文件:RecyclerView不显示

Photos.java(如果我得到的数据):

public class Photos { 
static ArrayList<String> filenameListTemp=new ArrayList<>(); 
static ArrayList<File> photosList=new ArrayList<>(); 

public static ArrayList<String> getPhotos(File internal){ 
    File listFolers[]=internal.listFiles(); 
    if (listFolers!=null && listFolers.length>0){ 
     for (File file:listFolers) { 
      if (file.isDirectory()){ 
       getPhotos(file); 
      } 
      else { 
       if (file.getName().endsWith(".png")||file.getName().endsWith(".jpg")||file.getName().endsWith(".gif")||file.getName().endsWith(".bmp")){ 
        String temp =file.getPath().substring(0,file.getPath().lastIndexOf('/')); 

        if (!filenameListTemp.contains(temp)){ 
         filenameListTemp.add(temp); //This add names 
         photosList.add(file);// This add Files 
        } 
       } 
      } 
     } 
    } 
     return filenameListTemp; 
    } 
} 

RecyclerViewAdapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { 

Context context; 
ArrayList<String> photos=new ArrayList<>(); 
LayoutInflater layoutinflater; 



public RecyclerViewAdapter(Context context, ArrayList<String> photos) { 
    this.context=context; 
    this.photos=photos; 
    layoutinflater=LayoutInflater.from(context); 
} 



@Override 
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view=layoutinflater.inflate(R.layout.recyclerview_item_list,parent,false); 
    RecyclerViewAdapter.ViewHolder holder=new RecyclerViewAdapter.ViewHolder(view); 
    return holder; 
} 

@Override 
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) { 

     holder.image.setImageBitmap(BitmapFactory.decodeFile(photos.get(position))); 

} 

@Override 
public int getItemCount() { 
    return photos.size(); 
} 

public class ViewHolder extends RecyclerView.ViewHolder { 
    ImageView image; 

    public ViewHolder(View itemView) { 
     super(itemView); 
     image= (ImageView) itemView.findViewById(R.id.image); 
    } 
    } 
    } 
} 

MainActivity.java

public class MainActivity extends AppCompatActivity { 
RecyclerView recyclerView; 
RecyclerViewAdapter adapter; 
File expath= Environment.getExternalStorageDirectory(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    recyclerView= (RecyclerView) findViewById(R.id.recyclerview); 
    adapter=new RecyclerViewAdapter(this, Photos.getPhotos(expath)); 
    recyclerView.setAdapter(adapter); 
    recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)); 
    } 
} 
+0

如果您可以从内部存储装载数据,您是否记录了数据?因为没有在您的回收站查看任何数据可能意味着没有可用的数据源。 – uncannyj

确保您已在AndroidManifest.xml中添加权限以供阅读外置储存。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

2.把一些logsgetPhotos()方法,来检查的filenameListTempphotosList大小就知道天气它的工作与否。

+0

谢谢!!,我做了,结果是传递给适配器的ArrayList是空的。我忘了添加运行时权限 – Kevin

+0

我的荣幸....如果我的回答看起来有用,请给出一个upvote。仅供参考,我已经提出您的问题来帮助您获得upvote的特权。在此先感谢:) – FAT

请确保您传递适配器ArrayList中有大小大于0如果大小为0,那么它会在适配器显示任何

谢谢!

+0

是的,就是那个,数组列表毕竟是空的。我现在开始工作了 – Kevin