Expandable ListView Adapter ohne Fehler doch bei .SetAdapter?

  • Antworten:13
Lukas R.
  • Forum-Beiträge: 180

14.01.2015, 10:58:24 via Website

Hei Leute ich habe gerade in einem Fragment einen kompletten Adapter für eine Expandable List View gemacht und er weist auch keine Fehler auf.
Jedoch wenn ich den Adapter der Expandable ListView zuweisen will.
Hier der Fehler:

image

Also es findet die Zuweisungen nicht...
Hier der ganze Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;

namespace HKEDV_GPS
{
    public class LocationBookFragment : Fragment
    {
        public static Fragment NewInstance(int position)
        {
            Fragment fragment2 = new LocationBookFragment();
            Bundle args = new Bundle();
            return fragment2;


        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            return inflater.Inflate (Resource.Layout.locationbook, container, false);


            var list1 = View.FindViewById<ExpandableListView> (Resource.Id.expandableListView1);
            list1.SetAdapter (new ExpendListAdapter (Activity, _dictGroup));


            list1.ChildClick += delegate(object sender, ExpandableListView.ChildClickEventArgs e) {
                var itmGroup = lstKeys [e.GroupPosition];
                var itmChild = _dictGroup [itmGroup] [e.ChildPosition];


                Toast.MakeText (Activity, string.Format ("You Click on Group {0} with child {1}", itmGroup, itmChild), 
                    ToastLength.Long).Show ();          
            };
        }


        public class ExpendListAdapter: BaseExpandableListAdapter
        {
            Dictionary<string, List<string>> _dictGroup =null;
            List<string> _lstGroupID = null;
            Activity _activity;
            private readonly Context _context;

            public ExpendListAdapter (Activity activity,
                Dictionary<string, List<string>> dictGroup)
            {
                _dictGroup = dictGroup;
                _activity = activity;
                _lstGroupID = dictGroup.Keys.ToList();
            }


            public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
            {
                var item = _lstGroupID [groupPosition];

                if (convertView == null)

                    convertView = _activity.LayoutInflater.Inflate (Android.Resource.Layout.SimpleListItem1, null);

                return convertView;
            }

            public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
            {
                var item = _dictGroup [_lstGroupID [groupPosition]] [childPosition];

                if (convertView == null)

                    convertView = _activity.LayoutInflater.Inflate (Android.Resource.Layout.SimpleListItem1, null);

                return convertView;
            }

            public override Java.Lang.Object GetChild (int groupPosition, int childPosition)
            {
                throw new NotImplementedException ();
            }
            public override long GetChildId (int groupPosition, int childPosition)
            {
                throw new NotImplementedException ();
            }
            public override int GetChildrenCount (int groupPosition)
            {
                throw new NotImplementedException ();
            }

            public override Java.Lang.Object GetGroup (int groupPosition)
            {
                return _lstGroupID [groupPosition];
            }
            public override long GetGroupId (int groupPosition)
            {
                return groupPosition;
            }

            public override bool IsChildSelectable (int groupPosition, int childPosition)
            {
                throw new NotImplementedException ();
            }
            public override int GroupCount {
                get {
                    return _dictGroup.Count;
                }
            }
            public override bool HasStableIds {
                get {
                    throw new NotImplementedException ();
                }
            }


            void CreateExpendableListData ()
            {
                for (int iGroup = 1; iGroup <= 3; iGroup++) {
                    var lstChild = new List<string> ();
                    for (int iChild = 1; iChild <= 3; iChild++) {
                        lstChild.Add (string.Format ("Group {0} Child {1}", iGroup, iChild));
                    }
                    _dictGroup.Add (string.Format ("Group {0}", iGroup), lstChild);
                }
                    var lstKeys = new List<string>(_dictGroup.Keys);
            }

        }



    } 
}

Hoffe jemand kann helfen!

MFG Lukas R.
Programmieren ist nicht nur eine Wissenschaft, sondern auch ein Lifestyle!

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

14.01.2015, 15:03:26 via Website

Hallo Lukas,

Du hast vergessen deine Beiden Variablen im Fragment selber zu Definieren:
Das gehört als Klassenvariable da rein

Dictionary<string, List<string> > dictGroup = new Dictionary<string, List<string> > ();
    List<string> lstKeys = new List<string> ();

Zudem fehlt dir noch irgendwo die Möglichkeit einträge in deine ListView einzufügen ;)

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Lukas R.
  • Forum-Beiträge: 180

14.01.2015, 15:09:51 via Website

Hi Pascal,

Ich habe jetzt den Adapter mal als eigene cs File gemacht damit es übersichtlicher ist.
Hier die cs File jetzt:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace HKEDV_GPS
{
    [Activity (Label = "ExpandListAdapter")]            
    public class ExpendListAdapter: BaseExpandableListAdapter
    {
         Dictionary<string, List<string>> _dictGroup =null;
         List<string> _lstGroupID = null;
         Activity _activity;
         readonly Context _context;

        public ExpendListAdapter (Activity activity,
            Dictionary<string, List<string>> dictGroup)
        {
            _dictGroup = dictGroup;
            _activity = activity;
            _lstGroupID = dictGroup.Keys.ToList();
        }


        public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
        {
            var item = _lstGroupID [groupPosition];

            if (convertView == null)

                convertView = _activity.LayoutInflater.Inflate (Android.Resource.Layout.SimpleListItem1, null);

            return convertView;
        }

        public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
        {
            var item = _dictGroup [_lstGroupID [groupPosition]] [childPosition];

            if (convertView == null)

                convertView = _activity.LayoutInflater.Inflate (Android.Resource.Layout.SimpleListItem1, null);

            return convertView;
        }

        public override Java.Lang.Object GetChild (int groupPosition, int childPosition)
        {
            throw new NotImplementedException ();
        }
        public override long GetChildId (int groupPosition, int childPosition)
        {
            throw new NotImplementedException ();
        }
        public override int GetChildrenCount (int groupPosition)
        {
            throw new NotImplementedException ();
        }

        public override Java.Lang.Object GetGroup (int groupPosition)
        {
            return _lstGroupID [groupPosition];
        }
        public override long GetGroupId (int groupPosition)
        {
            return groupPosition;
        }

        public override bool IsChildSelectable (int groupPosition, int childPosition)
        {
            throw new NotImplementedException ();
        }
        public override int GroupCount {
            get {
                return _dictGroup.Count;
            }
        }
        public override bool HasStableIds {
            get {
                throw new NotImplementedException ();
            }
        }

    }

}

und die Fragment Activity sieht nun so aus:

    public class LocationBookFragment : Fragment
    {
        //Fragment
        public static Fragment NewInstance(int position)
        {
            Fragment fragment2 = new LocationBookFragment();
            Bundle args = new Bundle();
            return fragment2;
        }

        //OnCreate mit der Expandable ListView
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            return inflater.Inflate (Resource.Layout.locationbook, container, false);


            var list1 = View.FindViewById<ExpandableListView> (Resource.Id.expandableListView1);
            list1.SetAdapter (new ExpendListAdapter (Activity, _dictGroup));


            list1.ChildClick += delegate(object sender, ExpandableListView.ChildClickEventArgs e) {
                var itmGroup = lstKeys [e.GroupPosition];
                var itmChild = _dictGroup [itmGroup] [e.ChildPosition];


                Toast.MakeText (Activity, string.Format ("You Click on Group {0} with child {1}", itmGroup, itmChild), 
                    ToastLength.Long).Show ();          
            };
        }

        void CreateExpendableListData ()
        {
            for (int iGroup = 1; iGroup <= 3; iGroup++) {
                var lstChild = new List<string> ();
                for (int iChild = 1; iChild <= 3; iChild++) {
                    lstChild.Add (string.Format ("Group {0} Child {1}", iGroup, iChild));
                }
                _dictGroup.Add (string.Format ("Group {0}", iGroup), lstChild);
            }
            var lstKeys = new List<string>(_dictGroup.Keys);
        }


    } 
}

Und der Teil mit Create Expendable List Data generiert die Items von der ListView.
So stehts in einem Tutorial.

Nur mein Problem jetzt ist dass die Fragment cs den Folgenden Parameter der Adapter cs nicht findet:

_dictGroup

Was ja irgendwie klar ist
nur weis ich nicht wie man den Parameter _dictGroup von der Adapter cs in die Fragment cs einbinden oder importen kann damit es in der Fragment cs die Fehler nicht mehr wirft

MFG Lukas R.
Programmieren ist nicht nur eine Wissenschaft, sondern auch ein Lifestyle!

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

14.01.2015, 15:15:00 via Website

Du annst den icht übernehmen, denn erst kommt dein Fragment und erst dann dein ListView Adapter.
Somit musst du deine Dictionary oder dein 2Dimensionales Array einfach im Fragment definieren, dann ist der Fehler weg.
Aber nur scheinbar, da die Liste ja leer ist bekommst du eine NullRefence Exception.
Dann musst du die Liste noch irgendwo füllen.
Hast du zufällig den Link zum Tut?

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Lukas R.
  • Forum-Beiträge: 180

14.01.2015, 15:21:33 via Website

Hier dass ist das Tutorial doch einpaar Fehler hab ich noch drinnen.
http://www.codeproject.com/Articles/677206/MonoAndroid-Writing-ExpandableListView-amd

Dass Problem ist wenn ich die Parameter in der Fragment cs definiere fehlen sie in der Adapter.cs und dann bekomme ich dort den selben Fehler.

MFG Lukas R.
Programmieren ist nicht nur eine Wissenschaft, sondern auch ein Lifestyle!

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

14.01.2015, 15:32:06 via Website

Wen mich nicht alles steuscht stimmt das Beispiel, da ist die DictGroup nämlich zusetzlich in der MainActivity bei dir Fragment definiert:

[Activity (Label = "ExpendListBox", MainLauncher = true)]
public class MainActivity : Activity
{
    Dictionary<string, List<string> > dictGroup = new Dictionary<string, List<string> > ();
    List<string> lstKeys = new List<string> ();

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Lukas R.
  • Forum-Beiträge: 180

14.01.2015, 15:41:18 via Website

Wenn ich es in meiner MainActivity definiere wird Dictionary und List Rot und es sagt es exestiere nicht :O

image

MFG Lukas R.
Programmieren ist nicht nur eine Wissenschaft, sondern auch ein Lifestyle!

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

14.01.2015, 15:53:01 via Website

Sowohl in Java also auch C# gibt es Dictionary wahrscheinlich fehlt nur ein using/import.

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Sven R.
  • Forum-Beiträge: 1.904

14.01.2015, 20:56:41 via App

Ich sage nur: Unreachable Statement! In der OnCreateView() wird der Code nach return nicht ausgeführt. Das könnte auch die anderen Probleme verursachen.

Wenn dir mein Beitrag gefällt, kannst dich einfach mit dem 👍 "Danke"-Button auf der Website dieses Forums bedanken. 😀

Why Java? - Because I can't C#

Antworten
Lukas R.
  • Forum-Beiträge: 180

15.01.2015, 08:43:27 via Website

@Pascal

Dass mit import kann nicht sein da es im Adapter auch keinen Import benötigt.
Daran liegt der Fehler nicht

@Sven

Hab dass mit dem return ausgebessert.
Doch immernoch der selbe Fehler

Ich bin also nah dran an einer Lösung:
Fakt ist ja dass ich die Parameter jetzt in der Fragment cs als erstes definieren muss da zuerst das Fragment und erst dann der Adapter aufgerufen wird.
Jetzt liegt es nurmehr daran wie man dass am besten in einem Fragment macht.
Ich hab an sowas gedacht:

public class LocationBookFragment : Fragment
    {
        Dictionary<string, List<string>> _dictGroup =null;
        List<string> _lstGroupID = null;
        Activity _activity;
        readonly Context _context;

        public LocationBookFragment (Activity activity,
            Dictionary<string, List<string>> dictGroup)
        {
            _dictGroup = dictGroup;
            _activity = activity;
            _lstGroupID = dictGroup.Keys.ToList();
        }

        //Fragment
        public static Fragment NewInstance(int position,Activity activity,Dictionary<string, List<string>> dictGroup)
        {

            Fragment fragment2 = new LocationBookFragment();
            Bundle args = new Bundle();
            return fragment2;
        }

Das Problem bei diesem Beispiel ist nur dass dadurch mit dem public LocationBookFragment die cs File nichtmehr als Fragment anerkannt wird. also müsste man die zwei sachen iwie kombinieren in einem:

public LocationBookFragment (Activity activity,
            Dictionary<string, List<string>> dictGroup)
        {
            _dictGroup = dictGroup;
            _activity = activity;
            _lstGroupID = dictGroup.Keys.ToList();
        }

        //Fragment
        public static Fragment NewInstance(int position,Activity activity,Dictionary<string, List<string>> dictGroup)
        {

            Fragment fragment2 = new LocationBookFragment();
            Bundle args = new Bundle();
            return fragment2;
        }

— geändert am 15.01.2015, 08:51:27

MFG Lukas R.
Programmieren ist nicht nur eine Wissenschaft, sondern auch ein Lifestyle!

Antworten
Lukas R.
  • Forum-Beiträge: 180

16.01.2015, 13:07:17 via Website

keiner mehr eine Idee?

MFG Lukas R.
Programmieren ist nicht nur eine Wissenschaft, sondern auch ein Lifestyle!

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

16.01.2015, 13:32:00 via App

Bitte nicht Pushen.

Du hast doch dein Tutorial was sind da zu deinem Code die Interschiede dass es im Tut tut ;) und bei dir nicht?

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Lukas R.
  • Forum-Beiträge: 180

16.01.2015, 13:37:34 via Website

Ok sry wusste ich nicht :O

Der unterschied ist dass im Tutorial dass alles mit einer Activity gemacht wird und nicht mit einem Fragment.
Also bei ihm ist es so:

Activity ( mit der Listview usw ) + Adapter als eigene cs

Bei mir ist es so:

Fragment (mit der Listview ) + Adapter als eigene cs

An sich ist dass ja nichtmal das Problem sondern das Problem ist dass bei Ihm in der Activity _dictGroup gefunden wird obwohl er _dictGroup in seinem Adapter definiert. Bei mir wird es aber im meinem Fragment nicht gefunden.

MFG Lukas R.
Programmieren ist nicht nur eine Wissenschaft, sondern auch ein Lifestyle!

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

16.01.2015, 14:11:18 via Website

Ich glaube du hast da was falsch verstanden:

Es ist egal ob im Adapter die Variable auch definiert ist.
Du musst sie in deinem Fragment extra definieren. Im Tutorial ist die Var auch in der Activity extra Definiert:
Activity:

public class MainActivity : Activity
{
    Dictionary<string, List<string> > dictGroup = new Dictionary<string, List<string> > ();
    List<string> lstKeys = new List<string> ();

    protected override void OnCreate (Bundle bundle)

listAdapter:

class ExpendListAdapter : BaseExpandableListAdapter
{
    Dictionary<string, List<string> > _dictGroup =null;
    List<string> _lstGroupID = null;
    Activity _activity;

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten