gpt4 book ai didi

java - RecyclerView 什么都不显示

转载 作者:搜寻专家 更新时间:2023-11-01 09:35:35 25 4
gpt4 key购买 nike

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

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;
}
}

回收 View 适配器:

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);
}
}
}
}

主 Activity .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));
}
}

最佳答案

1. 确保您已在 AndroidManifest.xml 中添加读取外部存储的权限。

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

2.getPhotos()方法中放入一些logs,检查filenameListTempphotosList 以了解其工作与否。

关于java - RecyclerView 什么都不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43553350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com