来自文件夹的Xamarin图像网格

时间:2017-03-28 14:36:20

标签: c# android image xamarin xamarin.android

我正在尝试使用文本显示图像网格。使用文件夹中的图像。我尝试了各种方法和教程但没有成功。我对内部和外部存储都有READ和WRITE权限。每当我调试应用程序时,我得到的都是一个空白屏幕。我试过在Adapters GetView()方法上设置断点,它永远不会被击中。

这是我的MainActivity OnCreate()方法:

protected override void OnCreate(Bundle bundle)
{
    if (!_dir.Exists()) { _dir.Mkdir(); }

    base.OnCreate(bundle);
    SetContentView(R.Layout.Main);

    string[] FilePath = null;
    string[] FileName = null;

    if (_dir.IsDirectory)
    {
        DroidIO.File[] imageList = _dir.ListFiles();
        FilePath = new string[imageList.Length];
        FileName = new string[imageList.Length];

        for (int i = 0; i < imageList.Length; i++)
        {
            FilePath[i] = imageList[i].AbsolutePath;
            FileName[i] = imageList[i].Name;
        }
    }

    var gvImageList = FindViewById<GridView>(R.Id.gvImageList);
    gvImageList.SetAdapter(new ImagesFromFolderAdapter(this, FilePath, FileName));
    gvImageList.ItemClick += delegate { gvImageList_ItemClick(); };
}

我的图像适配器类:

public class ImagesFromFolderAdapter : BaseAdapter
{
    private Activity context;
    private string[] filePath;
    private string[] fileName;

    public  ImagesFromFolderAdapter(Activity c, string[] fp, string[] fn)
    {
        this.context = c;
        this.fileName = fn;
        this.filePath = fp;
    }

    public override int Count { get; }

    public int GetCount()
    {
        return filePath.Length;
    }

    public override Java.Lang.Object GetItem(int position)
    {
        return position;
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        if (view == null)
            view = context.LayoutInflater.Inflate(R.Layout.gallery_item_layout, null);

        view.FindViewById<ImageView>(R.Id.gitemImage).SetImageBitmap(BitmapFactory.DecodeFile(filePath[position]));
        view.FindViewById<TextView>(R.Id.gitemText).Text = fileName[position];

        return view;

    }
}

我出错的任何想法?我目前正在使用LG G3作为调试设备,如果这有帮助的话。谢谢你提前提供任何帮助。

1 个答案:

答案 0 :(得分:0)

您为网格适配器返回了零计数。

更改

public override int Count { get; }

 public override int Count
 {
      get { return filePath.Length; }
 }