简体   繁体   中英

edit TextView in child view in ExpandableListView android

i have Expandable List View android when change my Text view txtChild.Text = "some Words..."; after clicking button in the same child view ,, last child only editing in all parent !! i want the child position clicked only change not the last child ? how do that ?

this is my ExpandableListAdapter class

` class ExpandableListAdapter : BaseExpandableListAdapter { TextView txtListChild;

    private Activity _context;

    private List<string> _listDataHeader; // header titles

    // child data in format of header title, child title

    private Dictionary<string, List<string>> _listDataChild;





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


    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);
           txtListChild = (TextView)convertView.FindViewById(Resource.Id.lListItem);
              Button CollectAll = (Button)convertView.FindViewById(Resource.Id.button1);

              CollectAll.Click += delegate
              {
                  txtListChild.Text = "some words ..";
                 // i want child  position clicked only text  changed 

              }; 
        }


        txtListChild.Text = childText;
        return convertView;
    }




    public override int GetChildrenCount(int groupPosition)
    {
        return _listDataChild[_listDataHeader[groupPosition]].Count;
    }


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


    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.lListHeader);
        lblListHeader.Text = headerTitle;

        return convertView;
    }


    public override int GroupCount
    {
        get
        {
            return _listDataHeader.Count;
        }
    }


    public override bool HasStableIds
    {
        get
        {
            return false;
        }
    }


    public override bool IsChildSelectable(int groupPosition, int childPosition)
    {
        return true;
    }

}`

and this my activity

 public class layout1 : Activity
{
    Sqlitedb db = new Sqlitedb();
    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;

    List<string> listDataHeader;
    Dictionary<string, List<string>> listDataChild;
    int previousGroup = -1;

      protected  override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.layout1);

        // Create your application here
        expListView = FindViewById<ExpandableListView>(Resource.Id.vExp);

        //// Prepare list data
         prepareListData();

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

        FnClickEvents();




    }



      void prepareListData()
      {
          listDataHeader = new List<String>();
          listDataChild = new Dictionary<String, List<String>>();

          // Adding child data
          listDataHeader.Add("Top 250");
          listDataHeader.Add("Now Showing");
          listDataHeader.Add("Coming Soon..");

          // Adding child data
          List<String> top250 = new List<String>();
          top250.Add("The Shawshank Redemption");
          top250.Add("The Godfather");
          top250.Add("The Godfather: Part II");
          top250.Add("Pulp Fiction");
          top250.Add("The Good, the Bad and the Ugly");
          top250.Add("The Dark Knight");
          top250.Add("12 Angry Men");

          List<String> nowShowing = new List<String>();
          nowShowing.Add("The Conjuring");
          nowShowing.Add("Despicable Me 2");
          nowShowing.Add("Turbo");
          nowShowing.Add("Grown Ups 2");
          nowShowing.Add("Red 2");
          nowShowing.Add("The Wolverine");

          List<String> comingSoon = new List<String>();
          comingSoon.Add("2 Guns");
          comingSoon.Add("The Smurfs 2");
          comingSoon.Add("The Spectacular Now");
          comingSoon.Add("The Canyons");
          comingSoon.Add("Europa Report");

          // Header, Child data


          listDataChild.Add(listDataHeader[0], top250);
          listDataChild.Add(listDataHeader[1], nowShowing);
          listDataChild.Add(listDataHeader[2], comingSoon);
          //RunOnUiThread(() =>
          //{
          //    listDataChild.Remove("Top 250");
          //    listAdapter.NotifyDataSetChanged();


          //});

      }







         void FnClickEvents()
    {
        //Listening to child item selection
        expListView.ChildClick += delegate(object sender, ExpandableListView.ChildClickEventArgs e)
        {

           // listDataChild.Visibility = Android.Views.ViewStates.Gone;
            Toast.MakeText(this, "child", 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", ToastLength.Short).Show();
        }; 
    }



}

i have Expandable List View android when change my Text view txtChild.Text = "some Words..."; after clicking button in the same child view ,, last child only editing in all parent !!

It's because you store the txtChild globally like below:

class ExpandableListAdapter : BaseExpandableListAdapter { 
    TextView txtListChild;
    ...
    public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
    {
       ...
       txtListChild = (TextView)convertView.FindViewById(Resource.Id.lListItem);
       ...
    }
    txtListChild.Text = childText;
    return convertView;
}

So when the children views get rendered. GetChildView get called for several times and txtListChild points to the last child view.

To fix the problem you simply need to move the declaration TextView txtListChild to inside the method GetChildView like below:

class ExpandableListAdapter : BaseExpandableListAdapter { 
    ...
    public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
    {
       TextView txtListChild;
       ...
       txtListChild = (TextView)convertView.FindViewById(Resource.Id.lListItem);
       ...
       txtListChild.Text = childText;
    }
    return convertView;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM