Android C# Clear TextView before populating with new data - c#

I'm trying to clear 3 TextView fields before new data is then displayed. I'm using SQLite and returning 3 phone numbers.
I can't run the app as I get "cannot convert from string to int" on the
txtFire.SetText("");
section of code.
The 3 fields in my SQLite DB are TEXT fields, so slightly confused on how to resolve this.
Any advice is appreciated.
private void EmergencyServicesData()
{
var location = FindViewById<AutoCompleteTextView>(Resource.Id.autoCompleteCountry).Text;
ICursor selectData = sqliteDB.RawQuery("SELECT POLICE, FIRE, MEDICAL FROM EmergencyServices WHERE COUNTRY = '" + location + "'", new string[] { });
if (selectData.Count > 0)
{
selectData.MoveToFirst();
do
{
txtFire = (TextView)FindViewById(Resource.Id.txtFire);
txtMedical = (TextView)FindViewById(Resource.Id.txtMedical);
txtPolice = (TextView)FindViewById(Resource.Id.txtPolice);
txtFire.SetText("");
txtMedical.SetText("");
txtPolice.SetText("");
EmergencyServices emergencyServices = new EmergencyServices();
EmergencyServices.Clear();
emergencyServices.POLICE = selectData.GetString(selectData.GetColumnIndex("POLICE"));
emergencyServices.FIRE = selectData.GetString(selectData.GetColumnIndex("FIRE"));
emergencyServices.MEDICAL = selectData.GetString(selectData.GetColumnIndex("MEDICAL"));
EmergencyServices.Add(emergencyServices);
}
while (selectData.MoveToNext());
selectData.Close();
}
foreach (var item in EmergencyServices)
{
LayoutInflater layoutInflater = (LayoutInflater)BaseContext.GetSystemService(Context.LayoutInflaterService);
View addView = layoutInflater.Inflate(Resource.Layout.EmergencyServices, null);
TextView txtPoliceData = addView.FindViewById<TextView>(Resource.Id.txtPolice);
TextView txtFireData = addView.FindViewById<TextView>(Resource.Id.txtFire);
TextView txtMedicalData = addView.FindViewById<TextView>(Resource.Id.txtMedical);
txtPoliceData.Text = item.POLICE;
txtFireData.Text = item.FIRE;
txtMedicalData.Text = item.MEDICAL;
container.AddView(addView);
}
}

You have nullpointers in your textviews. Change to this.
LayoutInflater layoutInflater = (LayoutInflater)BaseContext.GetSystemService(Context.LayoutInflaterService);
View addView = layoutInflater.Inflate(Resource.Layout.EmergencyServices, null);
txtFire = addView.FindViewById<TextView>(Resource.Id.txtFire);
txtMedical = addView.FindViewById<TextView>(Resource.Id.txtMedical);
txtPolice = addView.FindViewById<TextView>(Resource.Id.txtPolice);

Related

Reloading live charts chart only adds new data

I'm using Live Charts in my application for a pareto chart. I have made a SeriesCollection. I'm loading it from a Stored Procedure in the following way:
public void LoadChart()
{
List<DataTops> dataTops = GetTops();
ChartValues<int> Pareto = new ChartValues<int>();
List<string> timevalues = new List<string>();
int selected = ComboSelect();
IDLables = new List<string>();
foreach (var item in dataTops)
{
values.Add(item.Total);
Pareto.Add(item.Running);
IDLables.Add((item.W) + "." + (item.B));
}
TopAlarms = new SeriesCollection
{
new ColumnSeries
{
Title = "Total",
Values =values,
DataLabels = false,
ScalesYAt = 0,
},
new LineSeries
{
Title = "%",
Values = Pareto,
ScalesYAt = 1,
PointGeometrySize = 0
}
};
public List<DataTops> GetTops()
{
int selected = ComboSelect();
DataSet Ds = new DataSet();
DataSetTableAdapters.TimePerID_TopTableAdapter TimerTopta = new DataSetTableAdapters.TimePerID_TopTableAdapter();
TimerTopta.Fill(Ds.TimePerID_Top, selected);
List<DataTops> Tops = new List<DataTops>();
foreach (DataRow row in Ds.Tables["TimePerID_Top"].Rows)
{
Tops.Add(new DataTops() { Total = (int)row["Total"], W = (int)row["W"], B = (int)row["B"], Amount = (int)row["Amount"], Running = (int)row["Running"] });
}
return Tops;
}
I have a combobox to select an amount to show (selected in the dataset) and a button that I use to update the chart. The Chart works fine, but whenever I press the update button it only adds new data behind the already existing data.
Live charts doesn't automatically clear the chart collection data upon loading so I did this:
private void UpdateChart_Click(object sender, RoutedEventArgs e)
{
if (TopAlarms != null)
{
TopAlarms.Clear();
}
LoadChart();
}
But it still won't clear and reload the chart. How can i reload the chart when the button is pressed so the new selected data amount will show?
after some testing and reasearch I have the following solution for clearing the chart:
try
{
if (TopAlarms != null)
{
TopAlarms.Clear();
}
}
catch { }
This is probably not the best solution but it works for me.

Checkbox turning multiple values true

I've been stuck of this for a while and would love some insight.
When I add a new user or source and check the drawn checkbox, it causes the bool to be true when checked, as expected.
However it also checks all the other users connected to that source as true or false.
[before]
[after]
This only occurs when I load the XML file into my program that already had existing data, then add new Users or Sources via the UI.
this is a part of the LoadXML Method.
// Load the source list
sourceProps.SystemSources.Clear();
fCMUserSourceBindingSource.Clear();
fCMSourceBindingSource.Clear();
foreach (FusionSource src in fusionSystem.sourceList)
{
FCMSource fSource = new FCMSource(src);
sourceProps.SystemSources.Add(fSource);
fCMSourceBindingSource.Add(fSource);
fCMUserSourceBindingSource.Add(fSource);
}
txtSourceID.Text = sourceProps.GenerateNextSourceId();
// Load the user data from the fusionSystem object.
userProfile.SystemUserList.Clear();
//currently clears grid. might need to clear list instead
fCMUserBindingSource.Clear();
foreach (FusionUser usr in fusionSystem.userList)
{
FCMUser fUser = new FCMUser(usr);
userProfile.SystemUserList.Add(fUser);
fCMUserBindingSource.Add(fUser);
foreach (FusionSourcePermission srcPerm in usr.SourcePermissionList)
{
FCMSource fSource = new FCMSource(srcPerm);
fUser.fusionUserSources.Add(fSource);
}
}
AddColumnsUsersToSourceAllocation();
txtUserID.Text = userProfile.GenerateNextUserId();
// Load the zone group data from the fusionSystem object
zoneProps.systemZoneGroupList.Clear();
fCMZoneGroupBindingSource.Clear();
Any help would be appreciated.
Edit
The ListView is not binded and all the 'checking' is done programmatically when the user activates the click event.
private void lstSourceToUser_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
if (e.ColumnIndex > 1)
{
int usrIndex = userProfile.GetUserIndexByID(e.Header.Name);
int srcIndex = userProfile.GetUsersSourceIndex(e.Header.Name, e.Item.SubItems[0].Text);
Graphics g = e.Graphics;
CheckBoxRenderer.DrawCheckBox(g,new Point((e.Bounds.X + e.Bounds.Width/2 -10 ),(e.Bounds.Y)), userProfile.SystemUserList[usrIndex].fusionUserSources[srcIndex].UserSourceChkBox.MyCheckedState ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal);
}
else
// Draw the other subitems with default drawing.
e.DrawDefault = true;
}
Edit
This is the FCMSource constructor.
public FCMSource(string sourceId, string sourceName, string icnId, string lstIndx)
{
id = sourceId;
icon = icnId;
name = sourceName;
listIndex = lstIndx;
UserSourceCheckState = false;
UserSourceChkBox = new MyCheckBoxItem(false);
}
public FCMSource(FCMSource fSource, bool chk)
{
name = fSource.name;
icon = fSource.icon;
id = fSource.id;
listIndex = fSource.listIndex;
UserSourceCheckState = chk ? true : false;
UserSourceChkBox = new MyCheckBoxItem(chk);
}
And this is the FCMUser constructor which contains the different Sources.
public FCMUser(string userid, string nm, string icn, string pinNo, CheckBox pinEn, string lstIndx)
{
id = userid;
name = nm;
icon = icn;
pin = pinNo;
pinEnabled = pinEn.CheckState;
chkBoxPINEnable = new MyCheckBoxItem(false);
fusionUserSources = new List<FCMSource>();
ListIndex = lstIndx;
updateCheckbox();
}

C# WPF and Sqlite: Datagrid displays last table recording when trying to read from an sqlite database table

I can't figure why my data grid is displaying the last table recording from an Sqlite database.
here is the code I've writen.
ObservableCollection<Activity> _activitiesList = new ObservableCollection<Activity>();
while (reader.Read())
{
_act.Name = reader.GetString(reader.GetOrdinal("Activity"));
_act.Customer = reader.GetString(reader.GetOrdinal("ActivityCustomer"));
_act.Activitytpe = reader.GetString(reader.GetOrdinal("ActivityNature"));
string st = reader.GetString(reader.GetOrdinal("StartedAt"));
_act.StartedAt = Convert.ToDateTime(st);
string et = reader.GetString(reader.GetOrdinal("FinishedAt"));
_act.EndedAt = Convert.ToDateTime(et);
//string _ratio = reader.GetString(reader.GetOrdinal("ActivityRatio"));
//_act.ActivityRatio = Convert.ToDouble(_ratio);
_act.Notes = reader.GetString(reader.GetOrdinal("Notes"));
string _hldy = reader.GetString(reader.GetOrdinal("Holiday"));
_act.IsHoliday = Convert.ToBoolean(_hldy);
_activitiesList.Add(_act);
}
ActivitiesDataGrid.ItemsSource=_activitiesList;
inside the while you have to instantiate a new activity class:
while (reader.Read())
{
Activity _act=new Activity();
....
}
Can you create new object instance for "Activity" class in inside while loop.
ObservableCollection<Activity> _activitiesList = new ObservableCollection<Activity>();
**while (reader.Read())
{
***_act = new Activity();***
_act.Name = reader.GetString(reader.GetOrdinal("Activity"));
....
....
}**

Add Ext.Net.Gridpanel in code behind

I'm trying to build several gridpanels with the result of a search in the DB.
But they never appears...
Here is my code, what's wrong with it ?
http://pastebin.com/WuTZwZrP
EDIT
Ok, got it. For those who have the same problem, I solved it by adding this to the GridPanel :
RenderTo = this.ExtPanel.ClientID
And this after the build :
ext.GridPanel grid = this.BuildGridPanel(forwarder.Key, forwarder.Value);
grid.Render();
//this.ExtPanel.Controls.Add(grid);
public void AddGridPanel()
{
Ext.Net.GridPanel g=new Ext.Net.GridPanel();
Ext.Net.Store store1=new Ext.Net.Store();
Model model = new Model();
for (int i = 1; i < tas.getTaskDE().Count / 2; i++)
{
fields = fields + "," + tas.getTaskDE()[i].FieldName;
ModelField modelField = new ModelField();
modelField.Name = tas.getTaskDE()[i].FieldName;
model.Fields.Add(modelField);
if (tas.getTaskDE()[i].Visibility == "true")
{
g.ColumnModel.Columns.Add(new ColumnBase[] {
new Column
{
Text = tas.getTaskDE()[i].FieldADName,
DataIndex = tas.getTaskDE()[i].FieldName,
Flex = 1
},
});
}
}
SqlDataSource s = new SqlDataSource();
s.ConnectionString =ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
s.SelectCommand = query;
store1.Model.Add(model);
store1.DataSource = s;
store1.DataBind();
g.Store.Add(store1);
g.Render(this.Form);
}
this an example of dynamic gridpanel hope it helps

Dictionary stopping at loadingfromscreen

I run the code and it will not store the values it stops immediately at the loadingfromscreen. What its supposed to do is a multi-page application form that will reinput the values to the texbox's on the back button from the next form.
The ASP.net code is too long to post, but basically its just texbox's and dropboxes. If needed i can post it, but the main issue im 90% sure is the C# code.
UPDATE: When i say stop it continues the code but will not run the dictionary method...i have put a arrow where the method stops in DICTIONARY ONLY
C#:
public partial class employment_driversapplication_personalinfo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Dictionary<string, string> DriversApplicationData = (Dictionary<string, string>) Session["DriversApp"];
try
{
if (Page.IsPostBack)
{
LoadMemoryFromScreen(DriversApplicationData);
}
else
{
LoadScreenFromMemory(DriversApplicationData);
}
}
catch //(Exception ex)
{
// throw new Exception("Exception occured in employment_driversapplication_personalinfo.aspx - Page_Load" + ex.Message);
}
finally
{
}
}
private void LoadMemoryFromScreen(Dictionary<string, string> DicDriversApp)
{
DicDriversApp["position"] = position.Text; <---Stops here (won't even store this)
DicDriversApp["fname"] = fname.Text;
DicDriversApp["middleinitial"] = middleinitial.Text;
DicDriversApp["lname"] = lname.Text;
DicDriversApp["birthday"] = birthday.Text;
DicDriversApp["proofofage"] = proofofage.SelectedValue;
DicDriversApp["address"] = address.Text;
DicDriversApp["city"] = city.Text;
DicDriversApp["state"] = state.Text;
DicDriversApp["email"] = email.Text;
DicDriversApp["phone"] = phone.Text;
DicDriversApp["altphone"] = altphone.Text;
DicDriversApp["citizen"] = citizen.SelectedValue;
DicDriversApp["whoreferred"] = whoreferred.Text;
DicDriversApp["famfriend"] = famfriend.Text;
DicDriversApp["relationship"] = relationship.Text;
DicDriversApp["rateofpayexpected"] = rateofpayexpected.Text;
DicDriversApp["rateofpaytype"] = RadioButtonList1.SelectedValue;
DicDriversApp["employedNow"] = employednow.SelectedValue;
DicDriversApp["curremployment"] = curremployment.Text;
DicDriversApp["pastAddress"] = pastaddress.SelectedValue;
DicDriversApp["previousAddress"] = previousaddress.Text;
DicDriversApp["previousCity"] = previouscity.Text;
DicDriversApp["previousZip"] = previouszip.Text;
DicDriversApp["previousState"] = previousstate.Text;
DicDriversApp["previousDuration"] = previousduration.Text;
DicDriversApp["previousAddress1"] = previousaddress1.Text;
DicDriversApp["previousCity1"] = previouscity1.Text;
DicDriversApp["previousZip1"] = previouszip1.Text;
DicDriversApp["previousState1"] = previousstate1.Text;
DicDriversApp["previousDuration1"] = previousduration1.Text;
Session["DriversApp"] = DicDriversApp;
}
private void LoadScreenFromMemory(Dictionary<string, string> DicDriversApp)
{
position.Text = DicDriversApp["position"];
fname.Text = DicDriversApp["fname"] ;
middleinitial.Text = DicDriversApp["middleinitial"];
lname.Text = DicDriversApp["lname"];
birthday.Text = DicDriversApp["birthday"];
proofofage.SelectedValue = DicDriversApp["proofofage"];
address.Text = DicDriversApp["address"];
city.Text = DicDriversApp["city"];
state.Text = DicDriversApp["state"];
email.Text = DicDriversApp["email"];
phone.Text = DicDriversApp["phone"];
altphone.Text = DicDriversApp["altphone"];
citizen.SelectedValue = DicDriversApp["citizen"];
whoreferred.Text = DicDriversApp["whoreferred"];
famfriend.Text = DicDriversApp["famfriend"];
relationship.Text = DicDriversApp["relationship"];
rateofpayexpected.Text = DicDriversApp["rateofpayexpected"];
RadioButtonList1.SelectedValue = DicDriversApp["rateofpaytype"];
employednow.SelectedValue = DicDriversApp["employedNow"];
curremployment.Text = DicDriversApp["curremployment"];
pastaddress.SelectedValue = DicDriversApp["pastAddress"];
previousaddress.Text = DicDriversApp["previousAddress"];
previouscity.Text = DicDriversApp["previousCity"];
previouszip.Text = DicDriversApp["previousZip"];
previousstate.Text = DicDriversApp["previousState"];
previousduration.Text = DicDriversApp["previousDuration"];
previousaddress1.Text = DicDriversApp["previousAddress1"];
previouscity1.Text = DicDriversApp["previousCity1"];
previouszip1.Text = DicDriversApp["previousZip1"];
previousstate1.Text = DicDriversApp["previousState1"];
previousduration1.Text = DicDriversApp["previousDuration1"];
}
try something like this:
private void LoadMemoryFromScreen()
{
Dictionary<string, string> driver = new Dictionary<string, string>();
driver.Add("position", position.Text);
driver.Add("othervalue",value.Text); ///etc..
Session["dict"] = driver;
}
then, later on if you want to access the values in your dictionary, use something like this:
var dict = (Dictionary<string, string>)Session["dict"];
problem with this is that you're going to have to use dict.GetElementAt(index) in order to retrieve values, and i don't think that's a very good approach.
This will work, but i kinda dont understand why you are using a dictionary to do this. I assume you are only loading data from 1 person on your page? Then you might as well just make a class with all the properties in it, and then you can just pass that around instead of using this dictionary/session hack.
public class DriversApplicationData
{
public string position {get;set;}
public string firstname {get;set;}
//etc
}
then, you can just do something like
DriversApplicationData data = new DriversApplicationData();
data.position = position.Text;
data.firstname = fname.Text;
this is gonna be alot easier to maintain and to retrieve/insert values.

Categories