Dialogbox bug (Xamarin Android)

时光总嘲笑我的痴心妄想 提交于 2019-12-24 09:48:46

问题


I have the following code which passes the intent whether SELECT_IMAGE or REQUEST_CAMERA. In this case, after adding an image from the gallery, an item will be added on the listview.

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        // ................ //

        if (resultCode == Result.Ok)
        {
            if (requestCode == REQUEST_CAMERA)
            {
                // ...................//
            }
            else if ((requestCode == SELECT_FILE) && (data != null))
            {
                Android.Net.Uri uri = data.Data;
                string imgUri = Convert.ToString(uri.LastPathSegment);
                string sendUri = Convert.ToString(uri);

                myFileListAdapter.Add(sendUri);
                myFileListAdapter.NotifyDataSetChanged();
                setListViewHeightBasedOnChildren(listViewFiles);

                if (myFileListAdapter.Count == 5)
                {
                    btnAdd.Enabled = false;
                }

                listViewFiles.ItemClick += ClickHandler;
            }
        }
    }

Upon clicking the item from the listview, a dialogbox will appear showing the image. In the dialogbox, there are two options: Cancel and Delete (image).

public void ClickHandler(object sender, ItemClickEventArgs e)
    {
        string path = listViewFiles.GetItemAtPosition(e.Position).ToString();
        Android.Net.Uri uri2 = Android.Net.Uri.Parse(path);

        string[] proj = { MediaStore.Images.ImageColumns.Data };
        var cursor = ContentResolver.Query(uri2, proj, null, null, null);
        var colIndex = cursor.GetColumnIndex(MediaStore.Images.ImageColumns.Data);
        cursor.MoveToFirst();
        BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };
        BitmapFactory.DecodeFile(cursor.GetString(colIndex), options);
        int height = 250;
        int width = Resources.DisplayMetrics.WidthPixels;
        int outHeight = options.OutHeight;
        int outWidth = options.OutWidth;
        int inSampleSize = 1;

        if (outHeight > height || outWidth > width)
        {
            inSampleSize = outWidth > outHeight
                               ? outHeight / height
                               : outWidth / width;
        }

        options.InSampleSize = inSampleSize;
        options.InJustDecodeBounds = false;
        App.bm = BitmapFactory.DecodeFile(cursor.GetString(colIndex), options);
        System.IO.MemoryStream mem = new System.IO.MemoryStream();
        App.bm.Compress(Bitmap.CompressFormat.Png, 100, mem);
        //byte[] byteArray = mem.ToArray();
        //_imageView.SetImageBitmap(App.bm);

        var temp = new ImageView(this);
        temp.SetImageBitmap(App.bm);
        App.bm = null;
        GC.Collect();

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.SetTitle("Preview");            
        builder.SetView(temp);
        builder.SetCancelable(false);
        builder.SetPositiveButton("Cancel", (senderAlert, args) => 
        {
        });
        builder.SetNegativeButton("Delete?", (senderAlert, args) =>
        {

            Java.Lang.Object toRemove = myFileListAdapter.GetItem(e.Position);
            myFileListAdapter.Remove(toRemove);
            myFileListAdapter.NotifyDataSetChanged();
            setListViewHeightBasedOnChildren(listViewFiles);
            listViewFiles.ItemClick -= ClickHandler;

        });

        builder.Show();
    }

I've encountered these problems which I cannot figure out until now.

  1. For instance I added/imported (from the gallery) 3 images, which is 3 rows in the listview. If I click an item, 3 dialogbox appears as well, with same photos (of that particular item). Meaning, a dialogbox appears then after clicking cancel, another one, and another one.

  2. Delete button only works if there are more than 1 row or item in the listview. If there's only one and you delete it, the rest of the controls/tools below the listview are suddenly gone. In this case, I have textviews, button, etc. below the listview but after deleting the only row, these were deleted as well.

I'd really appreciate any help or suggestion because I've been trying to solve this for almost 2 days. Thank you.

来源:https://stackoverflow.com/questions/38073137/dialogbox-bug-xamarin-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!