Unable to parse xml data in listview on xamarin android

In the /Genre.xml menu:

<?xml version="1.0" encoding="UTF-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <group android:checkableBehavior="single">
    <item
      android:id="@+id/nav_home"
      android:icon="@drawable/ic_home_black_48dp"
      android:title="Home" />
    <item
      android:id="@+id/nav_genre"
      android:icon="@drawable/ic_toc_black_48dp"
      android:title="Genres" />
   
    <item
      android:id="@+id/nav_download"
      android:icon="@drawable/ic_get_app_black_48dp"
      android:title="Download" />
  </group>
  <item android:title="Account">
  	<menu>
		<group android:checkableBehavior="single">
			<item 
  			android:id="@+id/nav_about"
  			android:icon="@drawable/ic_lock_open_black_48dp"
  			android:title="About"/>
  			<item 
  			android:id="@+id/nav_signout"
  			android:icon="@drawable/ic_perm_identity_black_48dp"
  			android:title="Sign out"/>
		</group>
  		
  	</menu>
  </item>
 
</menu>
Run codeHide result
In the layout /GenreFragment.axml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lst_genre"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />
Run codeHide result

In the ViewModel / GenreFragmentVM:

public class GenerFragmentVM : Fragment
	{
		public override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			// Create your fragment here
		}
		ListView lst;
		public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
		{
			// Use this to return your custom view for this Fragment
			// return inflater.Inflate(Resource.Layout.YourFragment, container, false);

			base.OnCreateView (inflater, container, savedInstanceState);
			var view = inflater.Inflate (Resource.Layout.abc_list_menu_item_layout, container, false);
			lst = view.FindViewById<ListView> (Resource.Id.lst_genre);
			lst.SetAdapter(new ArrayAdapter<string> (this,Resource.Layout.abc_list_menu_item_layout,Resource.Menu.Genres));

		

			//Event click on listview
			lst.ItemClick+= delegate(object sender, AdapterView.ItemClickEventArgs e) {
				
			};
		}
	}
Run codeHide result

Error: (19.19): Error CS1502: the best overloaded method matches `Android.Widget.ArrayAdapter.ArrayAdapter (Android.Content.Context, int, int) 'has some invalid arguments (CS1502) (SoundCloud)

(45,45): Error CS1503: expression #1' cannot convert expression SoundCloud.GenerFragment for type `Android.Content.Context '(CS1503) (SoundCloud)

In Mainactivity.cs

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

        // Set our view from the "main" layout resource
        //ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
        SetContentView (Resource.Layout.Main);


        var mToolbar= FindViewById<Toolbar> (Resource.Id.toolbar);
        //Toolbar will now take on default action bar chacracteritics
        SetActionBar(mToolbar);
        ActionBar.Title = "home";




        //Enable suppport action bar to display hamburger
        //ActionBar.SetHomeAsUpIndicator(Resource.Drawable.icon_hambuger);
        //ActionBar.SetDisplayHomeAsUpEnabled (true);

        //Set menu hambuger
        ActionBar.SetHomeAsUpIndicator (Resource.Drawable.ic_menu_white_24dp);

        ActionBar.SetDisplayHomeAsUpEnabled (true);

        drawerLayout = FindViewById<DrawerLayout> (Resource.Id.drawer_layout);
        navigationView = FindViewById<NavigationView> (Resource.Id.nav_view);

        //Event click on navigatonView
        // 
        CreateFragments();
        LoadInditialFragment ();

        navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;




    }

    void NavigationView_NavigationItemSelected (object sender, NavigationView.NavigationItemSelectedEventArgs e)
    {
        e.MenuItem.SetChecked (true);
        if (e.MenuItem.ItemId != currentFragmentId) {
            SwitchFragment (e.MenuItem.ItemId);
        }
        drawerLayout.CloseDrawers ();
    }

    // Khoi tao gia tri cho fragment layout

    private void CreateFragments()
    {
        explorFragment= new ExploreFragment();
        genreFragment = new GenerFragment ();

    }
    //
    private void LoadInditialFragment ()
    {
        var transaction = FragmentManager.BeginTransaction ();
        transaction.Add (Resource.Id.fragmentContainer, explorFragment).Hide (explorFragment);
        transaction.Add (Resource.Id.fragmentContainer, genreFragment);
        transaction.Commit ();
    }

    private void SwitchFragment(int FragmentId)
    {
        var transaction = FragmentManager.BeginTransaction ();

        switch (currentFragmentId) {
        case Resource.Id.nav_home:
            transaction.Hide (explorFragment).Commit ();
            break;
        case Resource.Id.nav_genre:
            transaction.Hide (genreFragment).Commit ();
            break;
        }

        transaction = FragmentManager.BeginTransaction ();
        switch (FragmentId) {
        case Resource.Id.nav_home:
            transaction.Show (explorFragment);
            transaction.Commit ();
            break;

        case Resource.Id.nav_genre:
            transaction.Show (genreFragment);
            transaction.Commit ();
            break;
        }

        currentFragmentId = FragmentId;
    }

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
<!-- your content layout -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:titleTextColor="@android:color/background_light" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <FrameLayout
                android:id="@+id/fragmentContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </RelativeLayout>
    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/nav_view"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/navmenu" />
</android.support.v4.widget.DrawerLayout>
Run codeHide result
+4
source share
2 answers

, SushiHangover, (, ) ArrayAdapter Constructor.

run-time ( ​​ ) - , Activity null. "" , . , Activity OnCreateView. OnAttach OnCreate. .

`public class GenerFragmentVM : Fragment
{
    ListView lst;
    public override void OnCreate (Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);

         lst.SetAdapter(new ArrayAdapter<string> (this.Activity,Resource.Layout.abc_list_menu_item_layout,Resource.Menu.Genres));
    }

public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
var view = inflater.Inflate (Resource.Layout.abc_list_menu_item_layout, container, false);
lst = view.FindViewById<ListView> (Resource.Id.lst_genre);

//Event click on listview
lst.ItemClick+= delegate(object sender, AdapterView.ItemClickEventArgs e) {             
};
return view;
}
}
+1
  • Activity Fragment Widget ArrayAdapter
  • return ;

:

public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    base.OnCreateView (inflater, container, savedInstanceState);
    var view = inflater.Inflate (Resource.Layout.abc_list_menu_item_layout, container, false);
    lst = view.FindViewById<ListView> (Resource.Id.lst_genre);
    lst.SetAdapter(new ArrayAdapter<string> (this.Activity,Resource.Layout.abc_list_menu_item_layout,Resource.Menu.Genres));
    //Event click on listview
    lst.ItemClick+= delegate(object sender, AdapterView.ItemClickEventArgs e) {             
    };
    return view;
}
+1

All Articles