2017-08-25 4 views
1

私はExpandbleListViewを持っているが(http://www.appliedcodelog.com/2016/06/expandablelistview-in-xamarin-android.html &ペーストここからコピー)。XamarinアンドロイドExpandbleListView追加子項目

これまでのところ、私は(リストlstCS;)私のリストを宣言し、ボタンのクリックハンドラで私の項目を追加し、(NotifyDataSetChangedを呼び出して、私のクラスではなくメソッドで子アイテムのために)

それだけで感じていませんでしたそれは魅力のように働くにもかかわらず、そうしている。いくつかのティップスはありますか?あなたが提供されるリンクでのアダプタを使用した場合は

using System; 
using Android.Widget; 
using System.Collections.Generic; 
using Android.App; 
using Android.Views; 

using Android.Content; 
using Android.OS; 
using Android.Runtime; 

namespace TestApp 
{ 
    public class ExpandableListAdapter : BaseExpandableListAdapter 
    { 
     private Activity _context; 
     private List<string> _listDataHeader; 
     private Dictionary<string, List<string>> _listDataChild; 

    public ExpandableListAdapter(Activity context, List<string> listDataHeader, Dictionary<String, List<string>> listChildData) 
    { 
     _context = context; 
     _listDataHeader = listDataHeader; 
     _listDataChild = listChildData; 

    } 
    //for cchild item view 
    public override Java.Lang.Object GetChild(int groupPosition, int childPosition) 
    { 
     return _listDataChild[_listDataHeader[groupPosition]][childPosition]; 
    } 
    public override long GetChildId(int groupPosition, int childPosition) 
    { 
     return childPosition; 
    } 

    public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent) 
    { 
     string childText = (string)GetChild(groupPosition, childPosition); 
     if (convertView == null) 
     { 
      convertView = _context.LayoutInflater.Inflate(Resource.Layout.ListItemCustomLayout, null); 
     } 
     TextView txtListChild = (TextView)convertView.FindViewById(Resource.Id.lblListItem); 
     txtListChild.Text = childText; 
     return convertView; 
    } 
    public override int GetChildrenCount(int groupPosition) 
    { 
     return _listDataChild[_listDataHeader[groupPosition]].Count; 
    } 
    //For header view 
    public override Java.Lang.Object GetGroup(int groupPosition) 
    { 
     return _listDataHeader[groupPosition]; 
    } 
    public override int GroupCount 
    { 
     get 
     { 
      return _listDataHeader.Count; 
     } 
    } 
    public override long GetGroupId(int groupPosition) 
    { 
     return groupPosition; 
    } 
    public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent) 
    { 
     string headerTitle = (string)GetGroup(groupPosition); 

     convertView = convertView ?? _context.LayoutInflater.Inflate(Resource.Layout.HeaderCustomLayout, null); 
     var lblListHeader = (TextView)convertView.FindViewById(Resource.Id.lblListHeader); 
     lblListHeader.Text = headerTitle; 

     return convertView; 
    } 
    public override bool HasStableIds 
    { 
     get 
     { 
      return false; 
     } 
    } 
    public override bool IsChildSelectable(int groupPosition, int childPosition) 
    { 
     return true; 
    } 

    class ViewHolderItem : Java.Lang.Object 
    { 
    } 
} 
} 





using Android.App; 
using Android.Widget; 
using Android.OS; 
using System.Collections.Generic; 
using System; 
using System.Runtime.Remoting.Contexts; 

namespace TestApp 
{ 
    [Activity(Label = "TestApp", MainLauncher = true)] 
    public class MainActivity : Activity 
    { 
     ExpandableListAdapter listAdapter; 
     ExpandableListView expListView; 
     List<string> listDataHeader; 
     Dictionary<string, List<string>> listDataChild; 
     int previousGroup = -1; 
     List<string> lstCS; 

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

     SetContentView(Resource.Layout.Main); 
     expListView = FindViewById<ExpandableListView>(Resource.Id.lvExp); 

     // Prepare list data 
     FnGetListData(); 

     //Bind list 
     listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); 
     expListView.SetAdapter(listAdapter); 

     FnClickEvents(); 

     Button button1 = (Button)FindViewById(Resource.Id.button1); 
     button1.Click += ButtonClicked; 
    } 

    void ButtonClicked(object sender, EventArgs args) 
    { 
     lstCS.Add("foo"); 
     listAdapter.NotifyDataSetChanged(); 

     Toast.MakeText(ApplicationContext, "df", ToastLength.Long).Show(); 
    } 
    void FnClickEvents() 
    { 
     //Listening to child item selection 
     expListView.ChildClick += delegate (object sender, ExpandableListView.ChildClickEventArgs e) { 
      Toast.MakeText(this, "child clicked", ToastLength.Short).Show(); 
     }; 

     //Listening to group expand 
     //modified so that on selection of one group other opened group has been closed 
     expListView.GroupExpand += delegate (object sender, ExpandableListView.GroupExpandEventArgs e) { 

      if (e.GroupPosition != previousGroup) 
       expListView.CollapseGroup(previousGroup); 
      previousGroup = e.GroupPosition; 
     }; 

     //Listening to group collapse 
     expListView.GroupCollapse += delegate (object sender, ExpandableListView.GroupCollapseEventArgs e) { 
      Toast.MakeText(this, "group collapsed", ToastLength.Short).Show(); 
     }; 

    } 
    void FnGetListData() 
    { 
     listDataHeader = new List<string>(); 
     listDataChild = new Dictionary<string, List<string>>(); 

     // Adding child data 
     listDataHeader.Add("Computer science"); 
     listDataHeader.Add("Electrocs & comm."); 
     listDataHeader.Add("Mechanical"); 

     // Adding child data 
     lstCS = new List<string>(); 
     lstCS.Add("Data structure"); 
     lstCS.Add("C# Programming"); 
     lstCS.Add("Java programming"); 
     lstCS.Add("ADA"); 
     lstCS.Add("Operation reserach"); 
     lstCS.Add("OOPS with C"); 
     lstCS.Add("C++ Programming"); 

     var lstEC = new List<string>(); 
     lstEC.Add("Field Theory"); 
     lstEC.Add("Logic Design"); 
     lstEC.Add("Analog electronics"); 
     lstEC.Add("Network analysis"); 
     lstEC.Add("Micro controller"); 
     lstEC.Add("Signals and system"); 

     var lstMech = new List<string>(); 
     lstMech.Add("Instrumentation technology"); 
     lstMech.Add("Dynamics of machinnes"); 
     lstMech.Add("Energy engineering"); 
     lstMech.Add("Design of machine"); 
     lstMech.Add("Turbo machine"); 
     lstMech.Add("Energy conversion"); 

     // Header, Child data 
     listDataChild.Add(listDataHeader[0], lstCS); 
     listDataChild.Add(listDataHeader[1], lstEC); 
     listDataChild.Add(listDataHeader[2], lstMech); 
    } 
} 

}

答えて

1

、あなたは単にたとえば、このアダプタでAddChild方法を追加し、Activityからそれを呼び出すことができます。

public class MyExpandableListAdapter : BaseExpandableListAdapter 
{ 
    private Activity _context; 
    private List<string> _listDataHeader; 
    private Dictionary<string, List<string>> _listDataChild; 

    public MyExpandableListAdapter(Activity context, List<string> listDataHeader, Dictionary<string, List<string>> listChildData) 
    { 
     _context = context; 
     _listDataHeader = listDataHeader; 
     _listDataChild = listChildData; 
    } 

    ...... 

    public void AddChild(int groupPosition, string newitem) 
    { 
     var children = _listDataChild[_listDataHeader[groupPosition]]; 
     children.Add(newitem); 
    } 
} 

それであなたのActivityをこのように呼んでください:

Button btn1 = FindViewById<Button>(Resource.Id.ac1btn1); 
btn1.Click += (sender, e) => 
{ 
    var parent = listAdapter.GetGroup(0); 
    listAdapter.AddChild(0, "abc"); 
    listAdapter.NotifyDataSetChanged(); 
}; 

ご覧のとおり、私はButtonを使って、最初のグループに子供を追加しました: enter image description here

関連する問題