Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Xamarin Android Display Alert Dialog in Adapter

Greeting, I am having difficulty displaying the alert box that is in my list view adapter. I always get the "System.NullReferenceException: Object reference not set to an instance of an object." error.

Below is my code for the getview method in the adapterclass, i think its an issue with the context.

Can you please help me identify the problem and fix it.

Many Thanks.

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View listitem = convertView;
        listitem = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.expensesListView, parent, false);
        TextView TxtExpName = listitem.FindViewById<TextView>(Resource.Id.txtExpName);
        TextView TxtExpAmount = listitem.FindViewById<TextView>(Resource.Id.txtExpAmt);
        ImageView ImgExp = listitem.FindViewById<ImageView>(Resource.Id.expImg);
        SfLinearGauge linearGauge = listitem.FindViewById<SfLinearGauge>(Resource.Id.sfExpensesProgress);
        ImageView btnEdit = listitem.FindViewById<ImageView>(Resource.Id.btnEdit);
        btnEdit.Click += delegate {
            LayoutInflater layoutInflaterAndroid = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);

            //LayoutInflater layoutInflaterAndroid = LayoutInflater.From(context);
            //View view = inflater.inflate(R.layout.myNewInflatedLayout, null);
            //Button myButton = (Button)view.findViewById(R.id.myButton);
            View mView = layoutInflaterAndroid.Inflate(Resource.Layout.changeBudget, null);

            Android.Support.V7.App.AlertDialog.Builder alertDialogbuilder = new Android.Support.V7.App.AlertDialog.Builder(parent.Context);
            alertDialogbuilder.SetView(mView);

            var userContent = mView.FindViewById<SfNumericUpDown>(Resource.Id.sfNumericUpDown);
            userContent.Value = mItem[position].Budget;
            alertDialogbuilder.SetCancelable(false)
            .SetPositiveButton("Confirm", delegate
            {
                TxtExpAmount.Text = mItem[position].Spent + "/" + userContent.Value;
                Toast.MakeText(parent.Context, "Changed Budget to " + userContent.Value.ToString(), ToastLength.Short).Show();
            }).SetNegativeButton("Cancel", delegate
            {
                alertDialogbuilder.Dispose();
            });

            Android.Support.V7.App.AlertDialog alertDialogAndroid = alertDialogbuilder.Create();
            alertDialogAndroid.Show();


        };
        //btnEdit.Click += ButtonEdit_Click;


        int mydrw;
        try
        {
            mydrw = (int)typeof(Resource.Drawable).GetField(mItem[position].CatIcon).GetValue(null);
        }
        catch
        {
            mydrw = Resource.Drawable.PFM_Logo; // use this when no drawable matchs
        }
        //float density = 0.5f;
        //ImgCat.LayoutParameters = new ViewGroup.LayoutParams(new LinearLayout.LayoutParams((int)(40 * density), (int)(40 * density)));
        ImgExp.SetImageResource(mydrw);
        TxtExpName.Text = mItem[position].CatName;
        TxtExpAmount.Text = mItem[position].Spent + "/" + mItem[position].Budget;


        listitem.Click += (object sender, EventArgs e) =>
        {
            Toast.MakeText(parent.Context, "Clicked " + mItem[position].CatName, ToastLength.Long).Show();
        };
        return listitem;
    }

Comments