2

(How to call custom list view adapter from fragment)

Any way to call a custom list view adapter and show the list.

I have "Home.axml" and Home.cs(List fragment)

I need to call one custom list view adapter from the Home.cs(List fragment) and display it in Home.axml

below is my code.

Fragment

namespace AndroidApp2.Fragments
{
    public class Home : ListFragment
    {
         public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
          {  
        var adapter = new ArrayAdapter<String>(Activity, Android.Resource.Layout.SimpleListItemChecked, allheading);
        ListAdapter = adapter; 
        return base.OnCreateView(inflater, container, savedInstanceState);
          }    
    }
}

The above code is what am using, it is working fine for me. But how to create a costume list view adapter which is able to call from fragment?

Home.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <fragment
        android:id="@+id/heading_fragment"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent" />
    <FrameLayout
        android:id="@+id/details"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent" />
</LinearLayout>
Pravee
  • 85
  • 1
  • 10
  • 1
    see here: http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view – pnavk Oct 19 '16 at 05:32
  • I agreed, but that is also calling the custom list view adapter from an Activity not from a fragment,I need to call it from fragment. – Pravee Oct 19 '16 at 06:26
  • Isn't the idea of the Activity/Fragment relationship where all the logic and "work" is done on the activity? Can't your fragment call a method on the parent activity: `((MyActivity)Activity).MyMethod();` – jaymarvels Oct 19 '16 at 10:03
  • Actually i have an activity and am calling a fragment from that activity, below is the code. switch (position) { case 0: fragmentszz = Home.NewInstance(); break; } FragmentManager.BeginTransaction().Replace(Resource.Id.content_frame, fragmentszz).Commit(); This way i cal and once the fragment load i need to list details in the fragment,My question is how is it possible to load it from the activity itself. Is this make any sense? – Pravee Oct 19 '16 at 17:29

1 Answers1

1
Take a look on this code. I manged to call it from fragment, i only had to set the context properly otherwise i got this error "Window.getLayoutInflater()' on a null object reference".



        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            var view = inflater.Inflate(Resource.Layout.TagDialog, container, false);
            Button confirmBtn = view.FindViewById<Button>(Resource.Id.ConfirmBtn);
            Button cancelBtn = view.FindViewById<Button>(Resource.Id.CancelBtn);

            AbsenceService service = new AbsenceService();
            ListViewTags = view.FindViewById<ListView>(Resource.Id.TagListView);
            TagItems = service.GetTags(1);
            ListViewTags.Adapter = new Cards_TagListAdapter(context, TagItems);

            // "Cancel" click
            cancelBtn.Click += delegate {
                Dismiss();
            };

            confirmBtn.Click += ConfirmBtn_Click;
            return view;
        }
Dongolo Jeno
  • 414
  • 6
  • 8