Beispiel #1
0
        //Grabs the json file path string sent with the intent and deserializes it into a string
        private void Save_File(string fileName)
        {
            if (File.Exists(fileName))
            {
                //read the stuff out of the file and put it into the plant items list

                using (var streamReader = new StreamReader(fileName))
                {
                    //reads the file and then sets it to a string
                    string content = streamReader.ReadToEnd();
                    plantList = JsonConvert.DeserializeObject <PlantList>(content);
                }
            }
            else
            {
                plantList = new PlantList();
            }


            //put plantObject in List
            plantList.Items.Add(plantObject);
            //write list to json
            //creates a streamWriter and uses above filepath to write file
            using (var streamWriter = new StreamWriter(fileName, false))
            {
                //writes the new plant object instance to a json file
                streamWriter.WriteLine(JsonConvert.SerializeObject(plantList));
            }
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            //set the layout to a list view
            SetContentView(Resource.Layout.PlantListView);


            //find the list view and make an object
            ListView listView = new ListView(this);

            data = Intent.GetStringExtra("plantFilePath") ?? "Data not available";

            using (var streamReader = new StreamReader(data))
            {
                //reads the file and then sets it to a string
                string content = streamReader.ReadToEnd();
                //get the list out of the file
                plantList = JsonConvert.DeserializeObject <PlantList>(content);
            }

            //and then put each item in the list class instance into a listview thing
            PlantAdapter plantAdapter = new PlantAdapter(this, plantList);

            ListView.Adapter = plantAdapter;
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            //set the layout to a list view
            SetContentView(Resource.Layout.PlantListView);


            //find the list view and make an object
            ListView listView = new ListView(this);

            dataNew = Intent.GetStringExtra("plantFilePath") ?? "No data file";

            if (data != dataNew)
            {
                data = dataNew;
            }

            //call the method for streamreader to deserialize json
            plantList = Get_File(data);
            //and then put each item in the list class instance into a listview thing
            PlantAdapter plantAdapter = new PlantAdapter(this, plantList);

            ListView.Adapter    = plantAdapter;
            ListView.ItemClick += ListView_ItemClick;
        }
Beispiel #4
0
        //Intent that sends data file as well as current position in plantlist of data
        public static Intent intent(Context context, Type openActivity, string data, string plantName)
        {
            PlantList plantList = new PlantList();
            Intent    intent    = new Intent(context, openActivity);

            intent.PutExtra("plantFilePath", data);
            intent.PutExtra("PlantName", plantName);
            return(intent);
        }
 private PlantList Get_File(string file)
 {
     using (var streamReader = new StreamReader(data))
     {
         //reads the file and then sets it to a string
         string content = streamReader.ReadToEnd();
         //get the list out of the file
         plantList = JsonConvert.DeserializeObject <PlantList>(content);
     }
     return(plantList);
 }
Beispiel #6
0
        private string File_Plant_Name()
        {
            //creates a new streamreader and tells it what file to read

            using (var streamReader = new StreamReader(data))
            {
                //reads the file and then sets it to a string
                string    content   = streamReader.ReadToEnd();
                PlantList plantList = JsonConvert.DeserializeObject <PlantList>(content);
                //need to get latest plant object out of list.
                Plant plant = plantList.Items[plantList.Items.Count - 1];
                //need to get plant name from object
                string name = plant.PlantName;
                //return name;
                return(name);
            }
        }
        private void Save_Click(object sender, System.EventArgs e)
        {
            //find the place to save json files and create a new folder
            string plantFiles = BaseContext.GetDir("PlantsFile", 0).AbsolutePath;
            //Name the new json file
            string fileName = Path.Combine(plantFiles, "PlantsFile.json");

            PlantList plantItems;

            if (File.Exists(fileName))
            {
                //read the stuff out of the file and put it into the plant items list

                using (var streamReader = new StreamReader(fileName))
                {
                    //reads the file and then sets it to a string
                    string content = streamReader.ReadToEnd();
                    plantItems = JsonConvert.DeserializeObject <PlantList>(content);
                }
            }
            else
            {
                plantItems = new PlantList();
            }


            //put plantObject in List
            plantItems.Items.Add(plantObject);
            //write list to json
            //creates a streamWriter and uses above filepath to write file
            using (var streamWriter = new StreamWriter(fileName, false))
            {
                //writes the new plant object instance to a json file
                streamWriter.WriteLine(JsonConvert.SerializeObject(plantItems));
            }

            //on save click opens view plant screen.
            Intent intent = new Intent(this, typeof(ViewPlantActivity));

            //passes the json file just created to the new activity
            intent.PutExtra("plantFilePath", fileName);
            StartActivity(intent);
        }
        private string Current_Plant_Name(string name)
        {
            using (StreamReader streamReader = new StreamReader(_data))
            {
                string content = streamReader.ReadToEnd();
                PlantList plantList = JsonConvert.DeserializeObject<PlantList>(content);
                string plantName;

                if(name == "Data not available")                {
                    //need to get latest plant object out of list.
                    var plant = plantList.Items[plantList.Items.Count - 1];
                    plantName = plant.PlantName.ToString() ;
                }else                {
                    var plant = plantList.Items.Find(Items => Items.PlantName == name);
                    plantName = plant.PlantName.ToString();
                }
                return plantName;
            }

        }
 public PlantAdapter(Activity context, PlantList plantList) : base()
 {
     this.context   = context;
     this.plantList = plantList;
 }