Forum Xamarin.Android

Need to add item one by one in Activity2 on selecting item in Activity1(am using treeview)

GnanaGnana Member ✭✭
edited January 2019 in Xamarin.Android

Hi,
Can anyone please have a look at below code...

Am new to xamarin.android. Any help would be appreciable.

  public class EmptyActivity : Activity
    {
        public SfTreeView treeView1;
        public Intent intent;
        public EmptyActivity()
        {
      //main = new MainActivity();
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.EmptyViewlayout);

            var linearLayout = FindViewById<Android.Widget.LinearLayout>(Resource.Id.emptylinearLayout);
             treeView1 = new SfTreeView(linearLayout.Context);
            treeView1.SelectionMode = SelectionMode.Multiple;
            treeView1.SelectionChanged += TreeView1_SelectionChanged; ;

            var india= new TreeViewNode() { Content = "Australia", IsExpanded = true };
            var maharashtra= new TreeViewNode() { Content = "New South Wales" };
            var delhi= new TreeViewNode() { Content = "Victoria" };
            var bengal= new TreeViewNode() { Content = "Queensland" };
            var karnataka= new TreeViewNode() { Content = "West Australia" };
            var tamilNadu= new TreeViewNode() { Content = "South Australia" };

            india.ChildNodes.Add(delhi);
            india.ChildNodes.Add(maharashtra);
            india.ChildNodes.Add(bengal);
            india.ChildNodes.Add(karnataka);
            india.ChildNodes.Add(tamilNadu);

            treeView1.Nodes.Add(india);

            linearLayout.AddView(treeView1);
        }

        private void TreeView1_SelectionChanged(object sender, ItemSelectionChangedEventArgs e)
        {
            var items = e.AddedItems;
            for (int i = 0; i < items.Count; i++)
            {
                var item = items[i];
                if (item is TreeViewNode)
                {
                    TreeViewNode node = (TreeViewNode)item;
                    intent = new Intent(this, typeof(MainActivity));
                    if (node.ParentNode != null)
                    {
                        intent.PutExtra("ParentNode", node.Content.ToString());
                        StartActivity(intent);
                    }
                    else
                    {
                        intent.PutExtra("ChildNode", node.Content.ToString());
                        StartActivity(intent);
                    }
                }
            }
        }
    }

 public class MainActivity : AppCompatActivity
    {
        public SfTreeView treeView;
        public TreeViewNode newTreeviewNode;
        public TreeViewNode newChildNode;
        EmptyActivity EmpObject;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            EmpObject = new EmptyActivity();
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            var linearLayout = FindViewById<Android.Widget.LinearLayout>(Resource.Id.linearLayout1);
            treeView = new SfTreeView(linearLayout.Context);
            treeView.SelectionMode = SelectionMode.Single;
            Button button = new Button(linearLayout.Context);
            button.Text = "Start activity";
            button.Click += delegate
            {
                StartActivity(typeof(EmptyActivity));
            };
            linearLayout.AddView(button);
            linearLayout.AddView(treeView);

        }

        protected override void OnResume()
        {
            string Node = Intent.GetStringExtra("ParentNode");
            string childNode = Intent.GetStringExtra("ChildNode");
            newTreeviewNode = new TreeViewNode() { Content = Node, IsExpanded = true };
            newChildNode = new TreeViewNode() { Content = childNode };

            newTreeviewNode.ChildNodes.Add(newChildNode);

            treeView.Nodes.Add(newTreeviewNode);

            base.OnResume();
        }
    }

In my above code am trying to add treeview node in MainActivity, one by one. But on each time when i select i only get one item in my Activity page.
can anyone suggest me how to make each item visible when selecting the treeview node in EmptyActivity page

Best Answer

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    There are several problems:
    1. use the following code:

        intent = new Intent(this, typeof(MainActivity));
        if (node.ParentNode != null)
         {
              intent.PutExtra("ChildNode", node.Content.ToString());
              StartActivity(intent);
          }else
          {
            intent.PutExtra("ParentNode", node.Content.ToString());
            StartActivity(intent);
          }
    

    instead of

         intent = new Intent(this, typeof(MainActivity));
         if (node.ParentNode != null)
            {
               intent.PutExtra("ParentNode", node.Content.ToString());
               StartActivity(intent);
              }else
              {
                  intent.PutExtra("ChildNode", node.Content.ToString());
                  StartActivity(intent);
               }
    
  • GnanaGnana Member ✭✭

    @jezh

    One more query!..

    When i pass each data from EmptyActivity, passed data should present already in MainActivity.
    How to save each time when i pass to the treeview which in MainActivity. So the on each time when i add i can see the already added childnodes in MainActvity.

    Thanks in advance....

Sign In or Register to comment.