I'm trying to read the contents from a SQL query and store them into a listView (Android-C#). For whatever reason the listView is only showing the last item. When I debug through the code, I see that it is going through every result, however, I cannot get the adapter to display all the results. Maybe I am doing something wrong with the adapter. Any ideas?
Code:
while (rdr.Read ()) {
string[] Text = new string[] { (rdr[0])+ System.Environment.NewLine} ;
ListView mylistview = FindViewById<ListView> (Resource.Id.listView1);
var myAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1 , Text);
mylistview.Adapter = myAdapter;
}
For whatever reason the listView is only showing the last item.
Because in while every time new Text Array is created and pass to ArrayAdapter so last is Array data is showing in ListView.
To show all items in ListView,create Adapter object outside while loop and use ArrayList which will grow according to data-size:
ArrayList listText = new ArrayList();
while (rdr.Read ()) {
listText.add((rdr[0])+ System.Environment.NewLine);
}
ListView mylistview = FindViewById<ListView> (Resource.Id.listView1);
var myAdapter = new ArrayAdapter(this,
Android.Resource.Layout.SimpleListItem1,listText);
mylistview.Adapter = myAdapter;
Related
Here is part of code:
private void load_lv() {
lv_ss.Items.Clear();
lv_ss.Items.Add(supp.lv_standstill((optionsloaded) ? cbx_options.SelectedIndex : 0));
}
lv_ss is a Listview.
I want to add items to lv_ss using another method:
Here is another part of the code:
public ListViewItem lv_standstill(int option) {
string[] row = new string[6];
ListViewItem lv_item = new ListViewItem();
//not important part of code
//loading data from sql etc to row[]
if (first) {
lv_item = new ListViewItem(row);
first = false;
} else {
lv_item.SubItems.AddRange(row);
}
}
return lv_item;
}
Every new item of lv_item contains a row.
The problem is to move all ROWS to lv_ss listview.
When I use lv_ss.Items.Add it puts only first row.
I've tried with ListViewItem.ListViewSubItemCollection .. and then foreach subitem.
But it reads a single string from a row, not a whole row.
Not sure how to do that.
I mean, of course, I can put whole code in -> private void load_lv(){}
But I'm stubborn :D
Any help or ideas?
i want to add Items to my Listview, which i create dynamically using C#.
It looks kinda like this:
System.Windows.Controls.ListView test = new ListView();
test.Margin = new Thickness(28 + i, 23,485 - i, 23);
test.BorderBrush = new SolidColorBrush(Colors.Red);
test.BorderThickness = new Thickness(2);
test.VerticalContentAlignment = VerticalAlignment.Center;
Grid.SetColumn(test,1);
Grid.SetRow(test,1);
Now obviously the Listviews i create are completely empty.
So again my question: Is there any posibility to add items (say from an object) and display them in those Listviews i create here?
Ques : Is there any posibility to add items (say from an object) and display them in those Listviews i create here?
Let's say you get a list of string from an object. Like, objectMain.stringlist where objectMain is your object and stringlist is a property (List<string>) inside your object. Then, you can assign it like, test.ItemsSource = objectMain.stringlist
Using code behind is not recommended. Try using MVVM and create UIElements from the xaml.
I recommand you use MVVM patern to bind a list to ItemsSource in ListView and do that in xaml, but in code it is still available manually.
GridView gv = new GridView();
gv.Columns.Add(new GridViewColumn() { Header = "Something" });
test.View = gv;
test.Items.Add("Data1");
How to: Sort items of a DataGrid by Content, not by Name
I am sorting my items of the DataGrid by Name, but I want to sort them by using the Content property. See the last line:
listView_BusinessContacts.Items.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
Here is my code, that sets the items using C# (WPF):
public void SetListViewItems()
{
int i = 0;
string[] companies = File.ReadAllLines(#"C:\Users\Companies.txt", Encoding.UTF8);
string itemName = string.Empty;
foreach (string company in companies)
{
Image image = new Image();
image.Source = new BitmapImage(new Uri(#"Images\folder.png", UriKind.Relative));
image.Stretch = Stretch.None;
Label label = new Label();
label.Content = companies[i];
DockPanel.SetDock(image, Dock.Left);
DockPanel.SetDock(label, Dock.Right);
DockPanel dockPanel = new DockPanel();
dockPanel.Children.Add(image);
dockPanel.Children.Add(label);
ListViewItem listViewItem = new ListViewItem();
listViewItem.Content = dockPanel;
itemName = label.Content.ToString();
listViewItem.Name = itemName.Replace(" ", "");
listViewItem.Selected += delegate
{
companyContent = label.Content.ToString();
SetItemsToDataGrid();
};
listView_BusinessContacts.Items.Add(listViewItem);
i++;
}
listView_BusinessContacts.Items.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
}
My problem is, that sorting by Content is not working.
Example output by using Name: HelloMyNameIsCompany
Example output by using Content: Hello My Name Is Company // NOT WORKING!
As you know, the Name property does not allow blank fields, so I need to Replace them. But I do not want to replace them - I want to use the Content, but that does not sort correctly. Do you know, how to fix that?
Thanks in advance.
Edit: First one is the txt file, second one is the application and the CORRECT sorting (unfortunately by using Name and the Replace method).
Sorry for hiding text, but the information in that file contain business matters.
In MonoTouch, how can i immediately redraw an empty UITableView when i cleared its datasource?
List<string> ds = new List<string>();
UITableView tbl = new UITableView();
tbl.DataSource = new MyTableViewDataSource(ds);
//And the other parts(datasource class, delegate class etc.) are truely coded when i try to do somewhere at my program, something like
ds.Add("new string");
//it works and table shows new data too, but when i say
ds.Clear();
//it works and clear but table is not redrawn
Thanks
Are you calling tbl.ReloadData (); from somewhere ?
I have a Datagrid which show data from a database.
each time i load the data from my data vase the old data is earsed and the is replaced with the new data.
What i want is for the pervious row to remain and the new data to be appened to the end of the datagrid.
my code is as shown below :
public MainPage()
{
InitializeComponent();
dataGrid1.Columns.Add(new DataGridTextColumn { Header = "Year", Binding = new System.Windows.Data.Binding("year") });
dataGrid1.Columns.Add(new DataGridTextColumn { Header = "One", Binding = new System.Windows.Data.Binding("One") });
dataGrid1.Columns.Add(new DataGridTextColumn { Header = "Two", Binding = new System.Windows.Data.Binding("Two") });
dataGrid1.Columns.Add(new DataGridTextColumn { Header = "Three", Binding = new System.Windows.Data.Binding("Three") });
}
void client_DoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
{
dataGrid1.ItemsSource = e.Result;
}
how do i append the new data from the database rather than overwriting the data ?? .
One very simple and no-frills way to do it (because you don't appear to be using a regular MVVM approach), is to create a new list (IEnumerable) which unions both the old list and the new list, then assign it back to the ItemsSource property:
List<MyObjects> abc = new List<MyObjects>(dataGrid2.ItemsSource).Union(e.Result);
dataGrid2.ItemsSource = abc;
I've broken this on to two lines to make it easier to understand.
If you were using a ViewModel, then the property that is bound to the DataGrid would be a List<>, and then you can just do an AddRange(e.Result) to that property, and due to the beauty of data binding (and notifying) it will automatically show up in your datagrid.