Forum Xamarin.Android

Error while using fragment inside another fragment on custom Listview adpater

Hello Forum,

please assist me with the one problem which I am facing.

I have view pager setup which shows two fragments of which one include listview and on click of list item it should show a custom dialogfragment. Now I have don till the click event of ListView and everything is working fine.

But I am getting error while calling fragmentmanager

custom dialog fragment layout ---

alertdialog.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">
  <TextView
      android:text="Lorem ipsum"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/textView1" />
  <TextView
      android:text="Lorem ipsum dolor sit amet, consectetuer adipiscing elit...."
      android:textAppearance="?android:attr/textAppearanceSmall"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/textView2"
      android:layout_marginTop="10dp" />
  <Button
      android:text="Close"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/CloseButton"
      android:layout_marginTop="10dp" />
</LinearLayout>

first fragment in the viewPager which includes the listview and the listitem click(Which is working fine)----

using Android.OS;
using Android.Views;
using System.Collections.Generic;
using Android.Content;
using Android.Widget;
using metrohk.metrohkWS;
using System;
using Android.Support.V4.App;

namespace metrohk.Fragments
{
public class lunchroom_checklist_Fragment : Fragment
{
List<metrohkWS.checklist> _checklist;
ListView _lvChecklist;

    public int ListHeight = 0;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.Inflate(Resource.Layout.lunchroom_CheckList_Fragment, null);

        //webservices object
        metrohkWS.ws objWS = new metrohkWS.ws();
        _checklist = new List<metrohkWS.checklist>(objWS.getAllCheckList());

        _lvChecklist = view.FindViewById<ListView>(Resource.Id.checklistView);

        _lvChecklist.Adapter = new CheckListAdapter(this.Activity, _checklist);
        return view;
    }



    public class CheckListAdapter : BaseAdapter<metrohkWS.checklist>
    {

        private readonly IList<metrohkWS.checklist> _items;
        private readonly Context _context;

        public CheckListAdapter(Context context, IList<metrohkWS.checklist> items)
        {
            _items = items;
            _context = context;
        }
        public override checklist this[int position]
        {
            get { return _items[position]; }
        }

        public override int Count
        {
            get { return _items.Count; }
        }

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

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = _items[position];
            var view = convertView;

            if (view == null)
            {
                var inflater = LayoutInflater.FromContext(_context);
                view = inflater.Inflate(Resource.Layout.List_Item, parent, false);


            }
            view.FindViewById<TextView>(Resource.Id.checklistText).Text = item.checklistItem;
            view.FindViewById<TextView>(Resource.Id.checklistText).Click += CheckListAdapter_Click;
            view.FindViewById<TextView>(Resource.Id.checklistFrequency).Text = item.checklistFrequency;
            return view;
        }

        private void CheckListAdapter_Click(object sender, EventArgs e)
        {
            var t = (TextView)sender;
            Toast.MakeText(_context, t.Text, ToastLength.Long).Show();

            FragmentTransaction transaction = FragmentManager.BeginTransaction();
            DialogFragment1 dialogFragment = new DialogFragment1();
            dialogFragment.Show(transaction, "dialog_fragment");
        }
    }

    public class DialogFragment1 : DialogFragment
    {
        public static DialogFragment1 NewInstance(Bundle bundle)
        {
            DialogFragment1 fragment = new DialogFragment1();
            fragment.Arguments = bundle;
            return fragment;
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment
            View view = inflater.Inflate(Resource.Layout.alertFragment, container, false);

            // Remove the title bar from the dialog
            this.Dialog.RequestWindowFeature((int)WindowFeatures.NoTitle);

            // remove the fading background
            this.Dialog.Window.ClearFlags(WindowManagerFlags.DimBehind);

            // give the dialog a custom in/out transition animation
            //this.Dialog.Window.SetWindowAnimations(Resource.Style.ForgotPasswordDialogAnimation);

            Button button = view.FindViewById<Button>(Resource.Id.CloseButton);
            button.Click += delegate
            {
                Dismiss();
                Toast.MakeText(Activity, "Dialog fragment dismissed!", ToastLength.Short).Show();
            };

            return view;
        }
    }
}
}

Now the problem is --

FragmentTransaction transaction = FragmentManager.BeginTransaction();

Which gives me error of An object referece is required for non static method, property 'Fragment.FragmentManager'

I have even tried --

FragmentTransaction transaction = this.FragmentManager.BeginTransaction();

but no success.

P.S. I am using Android.Support.V4.App.Fragment

Please assist.

Thanks,
Sandip Kumbhar

Answers

  • AfzalAfzal USUniversity

    holder.imgDocStatus.Click += delegate
    {
    var fragment = new DocumentDetailsFrg(items[(int)holder.imgDocStatus.Tag]);
    FragmentTransaction fragmentTransaction = ((Activity)context).FragmentManager.BeginTransaction();
    fragmentTransaction.Replace(Resource.Id.flContent, fragment);
    fragmentTransaction.AddToBackStack("hello");
    fragmentTransaction.Commit();
    };

Sign In or Register to comment.