[英]Convert String[] to integer[]
我正在尝试在列表视图中使用图像文件的名称显示 SD 卡中文件夹中的图像。 我尝试了很多适配器这个适配器使用字符串生成器作为图像的名称,使用 Integer 作为图像的名称我使用此代码从文件夹作为字符串生成器获取图像 File file = new File("sdcard/images");
if (file.isDirectory())
{
File[] listFile = file.listFiles();
mFileStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++)
{
mFileStrings[i] = listFile[i].getAbsolutePath();
}
我想将此图像的字符串转换为 integer 以便我可以在适配器中使用它。 因为我收到以下错误
Listadapter(String[], integer[]) can not applied to
(String[], String[])
如何从文件夹中获取图像 Integer? 或者我可以编辑我的列表适配器以应用字符串吗?
这是我的列表适配器
公共 class ListAdapter 扩展 ArrayAdapter {
private final Activity Context;
private final String[] ListItemsName;
private final Integer[] ImageName;
public ListAdapter(Activity context, String[] content,
Integer[] ImageName) {
super(context, R.layout.list_items, content);
// TODO Auto-generated constructor stub
this.Context = context;
this.ListItemsName = content;
this.ImageName = ImageName;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = Context.getLayoutInflater();
View ListViewSingle = inflater.inflate(R.layout.list_items, null, true);
TextView ListViewItems = (TextView) ListViewSingle.findViewById(R.id.textView1);
ImageView ListViewImage = (ImageView) ListViewSingle.findViewById(R.id.imageView1);
ListViewItems.setText(ListItemsName[position]);
ListViewImage.setImageResource(ImageName[position]);
return ListViewSingle;
};
这是我的主要活动
File dir = new File("sdcard/images");
File[] filelist = dir.listFiles();
theNamesOfFiles = new String[filelist.length];
for (int ii = 0; ii < theNamesOfFiles.length; ii++) {
theNamesOfFiles[ii] = filelist[ii].getName();
}
File file = new File("sdcard/images");
if (file.isDirectory())
{
File[] listFile = file.listFiles();
mFileStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++)
{
mFileStrings[i] = listFile[i].getAbsolutePath();
result = Integer.parseInt(mFileStrings[i]);
}
}
list = (ListView) findViewById(R.id.list);
ListAdapter la= new ListAdapter(this,theNamesOfFiles,result);
list.setAdapter(la);
}}
问题是您严重误解了ImageView.setImageResource
工作原理。 通过将文件名传递给 Integer.parseInt ,您不会获得图像资源Integer.parseInt
。
mFileStrings[i] = listFile[i].getAbsolutePath();
result = Integer.parseInt(mFileStrings[i]);
我真的不确定你为什么认为这会奏效。 图片资源整数只存在于您的应用程序中的资源。 对于文件系统中的图像,您可以使用BitmapFactory
,但我真的建议只使用 Glide 以使其更容易。
将以下内容添加到app/build.gradle
依赖项。
implementation 'com.github.bumptech.glide:glide:4.9.0'
同步您的项目,然后将您的Listadapter
更改为此
public class ImagesAdapter extends BaseAdapter {
private final List<File> imageFiles;
public ImagesAdapter(List<File> imageFiles) {
// making a defensive copy of the list
this.imageFiles = new ArrayList<>(imageFiles);
}
public ImagesAdapter(File[] imageFiles) {
this(Arrays.asList(imageFiles));
}
@Override public File getItem(int i) { return imageFiles.get(i); }
@Override public long getItemId(int position) { return position; }
@Override public int getCount() { return imageFiles.size(); }
@Override
public View getView(int position, View oldView, ViewGroup parent) {
View view;
if (oldView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.list_items, parent, false);
} else {
view = oldView;
}
TextView nameView = (TextView) view.findViewById(R.id.textView1);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);
File file = imageFiles.get(position);
nameView.setText(file.getName());
Uri uri = Uri.fromFile(file);
Glide.with(view).load(uri).into(imageView);
return view;
}
}
然后将您的MainActivity
代码更改为
File imagesDir = new File("sdcard/images");
File[] files = imagesDir.listFiles();
list = (ListView) findViewById(R.id.list);
list.setAdapter(new ImagesAdapter(files));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.