将String []转换为integer []

时间:2019-10-09 05:52:04

标签: java android

我正在尝试在列表视图内的SD卡文件夹中显示图像文件名称。我尝试了很多适配器 该适配器使用     图像名称的字符串生成器 和     图像的整数 我使用此代码从文件夹中以字符串生成器的形式获取图像     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();
}

我想将此图像的字符串转换为整数,以便可以在适配器中使用它。 因为我遇到以下错误

 Listadapter(String[], integer[]) can not applied to
(String[], String[])

如何以整数形式从文件夹中获取图像? 或者我可以编辑我的listadapter以应用String吗?

这是我的列表适配器

公共类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;

};

这是我的MainActivity

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

1 个答案:

答案 0 :(得分:3)

问题是您严重误解了ImageView.setImageResource的工作方式。通过将文件名传递给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));