I have a problem generating a table view from array json data that I have get.
I have completed of getting and saving the json data in array form, but the problem I facing is that I cannot load them into my table.
Below is my code,
client.ExecuteAsync(request, response =>
{
string json = response.Content;
Console.WriteLine(json);
List<PostInfo> curTest = JsonConvert.DeserializeObject<List<PostInfo>>(json);
for (int i = 0; i < curTest.Count; i++)
{
PostInfo t1 = curTest[i];
arraydata.Add(t1.title.rendered);
}
array1 = arraydata.ToArray();
});
UITableView _table;
_table = new UITableView
{
Frame = new CoreGraphics.CGRect(0, 20, View.Bounds.Width, View.Bounds.Height - 20),
Source = new TableSource(array1)
};
View.AddSubview(_table);
The table returns a blank page when I trying to put the array1 as my TableSource. And I believe that if the array1 has nothing in the array once it out from the client.ExecuteAsync.
Can someone guide me how to generate TableView based on array get from the json data?
Your problem here is that when you try assign data source to your table view the array is still empty. Short term solution would be - re-assign datasource in async operation closure and dont forget to call TableView.ReloadData(). But it's not the prettiest solution. I would suggest you MVVMCross - You write your Core project with business logic and then you create platform project and use bindings to bind say Datasource to a collection of items. You can find tutorials with source code on the internet. There are plenty of them.
Your view doesn't realise that the source has changed. Big chance that if you get it working and change the properties of objects in your array, your view also won't know its data has changed.
Take a look at this:
https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx
Basically what this interface does is tell the view or binding that something has changed. This way the view knows not only to update, if you are smart about it it only updates 1 value, the one that changed.
Now for your current problem, how to fix it with this interface? Simply change your array to an ObservableCollection -> https://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx
An ObservableCollection is like a List but it already has the INotifyPropertyChanged interface implemented.
Related
Consider the following data
I am trying to update the property name from givenName to PetName. Below are the things I tried.
Option 1:
List<PatchOperation> patchOperations = new List<PatchOperation>();
dynamic data = new
{
PetName= "Dummy"
};
//Remove the 0th element from the pet array
patchOperations.Add(PatchOperation.Remove("/children/0/pets/0"));
//insert the new entity
patchOperations.Add(PatchOperation.Add<dynamic>("/children/0/pets/0", data));
await Container.PatchItemAsync<dynamic>("AndersenFamily", new
PartitionKey("AndersenFamily"), patchOperations);
This works on the first element (as I have specified the index as 0).
But I couldn't find a way to update all the elements in the array. Something like /children/*/pets (I get an error "Invalid navigation for array(*)").
Option 2:
Read all the data from the database, remove the existing pets and upload the new object with the property PetName. But I guess it will take lot of time to loop through all the objects and make these changes.
Can someone suggest a better solution or a way to fix the partial updates so that it updates all the elements in the array?
The actual dataset that I am working on is huge and has more nested arrays.
Yes you are right, Current version of Patch does not support patching nested array. You need to know the index in order to replace/add values as you mentioned in the first question.
One way to think about this would be to write your own logic to replace the keys , there are libraries available in dotnet and then using the bulk insert using the SDK to insert those documents.
I'm facing problem in binding the data radlistview by using it in xamarin forms. The code mentioned below is working fine with me in a single array
var listView = new RadListView();
listView.ItemsSource = new List<string>() { "A", "B", "C" };
but when to use the syntax of binding in multidemensional array like this
List<List<string>> strings = new List<List<string>>()
{
new List<string>{"Id:001","Name:Decker1"},
new List<string>{"Id:002","Name:Decker2"}
};
Data is not binding. The data is coming from Api
Please help figure out the problem where im missing and guide me so that i can bind the data in radlistview of telerik from api.
Your first list is just a list of strings so the listview can just do a ToString and get the value.
The second one if it does a ToString it wont get any string just a System.Object or something similar.
You either need to create a list of classes with properties and define these in the listview or flatten your multi dimensional array into a single dimension again.
I am still very new to programming, as im sure my question will show. I have spent the past two days utterly stuck, and i have not been able to find the answer to this anywhere. Might be because im just overlooking something so obvious that no one has had to ask, but here it goes:
I have made an application in VS Express 2013 for windows where the user chooses an alternative from a combobox displaying only the name of the object. From the choice i send the whole object to my "Converter Class".
EU = new Converter("Enriched Uranium", "44");
CO = new Converter("Coolant", "9832");
BI = new Converter("Biocells", "2329");
CB = new Converter("Construction Blocks", "3828");
FR = new Converter("Fertilizer", "3693");
GL = new Converter("Genetically Enhanced Livestock", "15317");
object[] myArray1 = { EU, CO, BI, CB, FR, GL };
comboBox1.DisplayMember = "name";
There are around 50 of these in the program. The first part is the name, the other is the ID that the XML uses to find stuff. They perform lots of stuff further on in the code, but this is the start:
Converter a = ((Converter)comboBox1.SelectedItem);
a.CallXml();
a.taxPrice(comboBox2.Text);
a.getNumber(textBox4.Text);
a.getTax(taxrat);
And so on... I know its not exactly beautiful, and i am seeing a lot of ways to make it more effective after learing. But right now im focusing on converting the whole thing to a web site, and im using VS Express 2013 for Web.
There is no combobox there, so im stuck using dropdownlist. The above method of loading the list with "AddRange" did not work, and ive trid about a hundred things, until i finally get to display the names this way:
List<object> myList1 = new List<object>();
myList1.Add(EU);
myList1.Add(CO);
myList1.Add(BI);
DropDownList2.DataSource = myList1;
DropDownList2.DataTextField = "name";
DropDownList2.DataBind();
So far so good! Where i am now utterly stuck is at the point where i need the users choice to return the object and send it to the "Converter" class. This is the closes i feel ive come:
protected void Button1_Click(object sender, EventArgs e)
{
object a = (Converter)DropDownList2.SelectedItem;
}
It says: "Cannot convert type blablabla to WebApplication4.Converter. How come? And is there any way i can make it to perform the same action the combobox did back in good, old winform?
What i want to do is that when the user chooses "Enriched Uranium", the program calls the "Converter class" with for example:
EU.CallXml();
And so on.
Welcome to the world of web development and the whole stateless request / response mechanism. To understand this fully have a read up on the differences between windows and web development.
In webforms the controls are a lot more basic than the winforms ones. Really the binding is just one way - its just using your source data to get the info needed to draw the list. If you think about it it has to be like this, otherwise it would be having to pass your entire list of objects between the server and the clients browser which would be wasteful, and its hard in a stateless environment to know when to bind back etc.
The webforms Dropdownlist.SelectedItem is of type ListBoxItem - which just has the minimum fields needed to draw and get the item from the dropdownlist control - just the text from the ListItem, and whether its selected. Which is why you can't cast it back to your converter object.
So how can you get map the selected item back to your Converter? You can either use the string value Dropdownlist.SelectedItem.Value to do some sort of lookup, or use the Dropdownlist.SelectedIndex to find the item from your source list at that index.
You could do something like this
List<Converter> myList1 = GetConverters(); //todo - write a method to make the list
DropDownList2.DataSource = myList1;
DropDownList2.DataTextField = "name";
DropDownList2.DataBind();
and on the click event
protected void Button1_Click(object sender, EventArgs e)
{
string s = DropDownList2.SelectedItem.Value;
Converter c = GetConverters().Find(x => x.name == s);
//do whatever with the converter
}
disclaimer - none of this is compiled - so may have some errors. Also its definitely not the best way of doing it (no error checking + making the list of converters just to do the datasource bind is a bit wasteful), just to show you how it could be done.
One other warning - make sure you only do the datasource bind once - its possible if you do it without a check that the page hasn't posted back it could bind just before you try to get the value - meaning the first value will always seem selected. Good luck!
During the construction of user control I populate the combobox with some data. Works fine. I have Status.ID as valuePath and Status.Name as displayPath.
cmb.ItemsSource = dbEntities.Status
The comobox will be used as a filter control and I need to insert some value for "All", which will be used as the empty filter.
First I tried a funny solution:
ObjectSet objectSet= dbEntities.Status;
Status stAll = new Status();
stAll.ID = -1;
stAll.Name = "All";
objectSet.AddObject(stAll);
cmb.ItemsSource = objectSet;
For some reason the object is not added to the objectSet. It didnt throw any exception either.
Then I tried to insert it manually to the first index but I got the error:
"Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."
My code looked like:
cmb.ItemsSource = entities.Status;
cmb.Items.Insert(0,"All");
Both didnt work. What would be the easiest way to add that line to the combobox? The error message got me confused. I am not sure how to use the ItemsSource for such a purpose.
edit: I did not have enough rep to answer my own question so here is the working code. Thanks Craig again.
CompositeCollection comp = new CompositeCollection();
comp.Add(new CollectionContainer {Collection = dbEntities.Status});
Status stAll = new Status();
stAll.ID = -1;
stAll.Name = "All";
comp.add(stAll);
cmb.ItemsSource = comp;
//do whatever filter you want when the selected value is -1
There are a couple different problems with what you are trying to do. You can't manipulate the Items when you are using the ItemsSource, instead you have to go through the object that is set to the ItemsSource. That is what the error message is about in the second part. Its because when you set the ItemsSource the Items value is unused it is not populated with the values of the ItemsSource.
I'm not familiar enough with the ObjectSet class to know why the first case is not working. However, it seems awkward to add an item to your values you are pulling from somewhere else, just to have the all case. The better solution is to use a null value to represent nothing. Unfortunately, there is no built in way to do this in WPF. However, there is a fairly easy solution of using an Adaptor do do this. I have used this solution a NullItemSelectorAdaptor, that enables null as a selection even if null is not in the list. All you have to do is wrap your combobox in the NullItemSelectorAdapter and null will be added as a value. The blog post explains everything pretty clearly, so I won't repeat it. You than can setup your filter so that null equates to no filtering.
I have a wpf c# application, that loads tasks to a treeView from a text file, the data about the tasks are loading into a list, Im trying to delete data in position I in the list, but i can not figure out how. I have this for loop checking to see if the selected treeView item is equal to the item in position I in the list and if so I wanna delete that item from the list. Here is the for loop which works, im just wondering how to do the actual delete, I've tried things such as .delete and .remove which I found on msdna.
for (int i = 0; i < name.Count; ++i)
{
string selectName = ((TreeViewItem)(treeView1.SelectedItem)).Header.ToString();
if (selectName == name[i])
{
//name.Remove(i) or name.Remove[i] or name[i].Remove
}
}
If name is a List<T> then you probably want name.RemoveAt(i) if I understand the question correctly.
Alternatively, you could just use name.RemoveAll(n => n == selectName); instead of the for loop.
How about:
string selectName = ((TreeViewItem)(treeView1.SelectedItem)).Header.ToString();
name.RemoveAll(x => x == selectedName);
You have to give List.Remove() an object, not an index.
So, assuming name is a List,
name.Remove((TreeViewItem)treeView1.SelectedItem);
Depending on your application needs, your data structure should have some time of Parent-Child relationship. All you should have to do is remove the Child that is selected from the parent, and the TreeView in WPF should just automatically update.
I suggest you read Josh Smith's article on using ViewModels with the TreeView for easier management. Trust me it will make your life a lot easier the sooner you start using a stronger data structure, and just remember that a TreeView only represents the data provided, and you should have other methods to edit the data directly and not the TreeView.