我正在製作一個應用程序,理論上它必須從我的內部存儲器加載圖像並將其顯示在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));
}
}
如果您可以從內部存儲裝載數據,您是否記錄了數據?因爲沒有在您的回收站查看任何數據可能意味着沒有可用的數據源。 – uncannyj