I am doing a simple game app
In which the total score of two simple game is stored in file shared preferences
How shal I sync the score with firebase
And retrieve the score in total score activity
Please help
Create a model class with User with properties name and email . create setters and getters and store the model object to firebase as below.
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("users");
String userId = mDatabase.push().getKey();
User user = new User("username", "email");
// pushing user to 'users' node using the userId
mDatabase.child(userId).setValue(user);
Retreive the data in other class using the below code
*******************************************************
mDatabase.child(userId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
Log.d(TAG, "User name: " + user.getName() + ", email " + user.getEmail());
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
Related
When I'm trying to click save after the edited the column cell with new user name, I'm not able to get the new cell value, instead I'm getting the old value.
grid.getEditorFieldGroup().addCommitHandler(new FieldGroup.CommitHandler()
{
#Override
public void preCommit(FieldGroup.CommitEvent commitEvent) throws
FieldGroup.CommitException {
BeanItem item = (BeanItem)
commitEvent.getFieldBinder().getItemDataSource();
User user= (User) item.getBean();
user.getName();//getting the old value instead the new column value
}
}
I have used the BeanItemContainer like below:
BeanItemContainer<User> container = new BeanItemContainer<User>(User.class);
grid.setContainerDataSource(container);
preCommit(FieldGroup.CommitEvent commitEvent) is called before the commit. So the BeanItem's bean (User) contains the old values (the new values were not yet committed into the bean). Therefore getName() returns the old value.
If you want to access the new value you have to use postCommit(FieldGroup.CommitEvent commitEvent) which is called after the commit.
grid.getEditorFieldGroup().addCommitHandler(new FieldGroup.CommitHandler() {
#Override
public void preCommit(FieldGroup.CommitEvent commitEvent) {
BeanItem item = (BeanItem)
commitEvent.getFieldBinder().getItemDataSource();
User user = (User) item.getBean();
String name = user.getName(); // old value
System.out.println(name);
}
#Override
public void postCommit(FieldGroup.CommitEvent commitEvent) {
BeanItem item = (BeanItem)
commitEvent.getFieldBinder().getItemDataSource();
User user = (User) item.getBean();
String name = user.getName(); // new value
System.out.println(name);
}
});
We can able to fetch the new value in the preCommit() method itself by the following code,
TextField nameField = (TextField) grid.getColumn("name").getEditorField();
nameField .getValue(); // new name
i am trying to extract one data at a time from the sqlite database from the application and for every data return from the sqlite table,i will need to output the data into the GUI with every textview created for every return result else will throw message not found. i am using xamarin native android.
How do i achieve that,i am new to xamarin?
e.g i have a textedit for user to input values database table values(1,2,3,4) if user puts 1,there will be a textview below the textedit with value 1 created And if user continue to put 2,another textview below (the textview with value 1) will be created with value 2
E.G Below:
1)Edittext for user input
2)When user input values 1 in the editbox,it matches the values in the database and return the value 1 as below:
3)When user input another value 2 in the editbox,it matches the values in the database and return value 2 as below:
Cuurently,my code is as follows:
DB.cs(to create the sqlite database in a folder in android that i can access instead of the default application folder)
namespace TestApp
{
public static class Db
{
public static string DBFile = testPath.Combine(Android.OS.Environment.DirectoryDownloads, "test.db"); //for tmp use
public static List<Model.Test> TestList = new List<Model.Test>();
public static void InitializeLocalDB()
{
if (!File.Exists(Db.DBFile))
{
using (var connection = new SQLiteConnection(Db.DBFile))
{
connection.DropTable<Model.Test>();
connection.CreateTable<Model.Test>();
}
}
}
}
}
MainActivity.cs
namespace TestApp
{
[Activity(Label = "#string/app_name", Theme = "#style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
Db.InitializeLocalDB();
}
Once i am able to create the database in the download folder,i will extract the db file and manually add in data with compactview to test the app.
I'm writing a bank payment app using Xamarin android that store user card information.
I'm looking for a safe and secure for storing data.
the only thing I found is below link that store account information which is useless for me:
https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/general/store-credentials/
KeyStore is the most secure way to store sensitive data on Android. You can store there anything, if it can be serialized it to string.
For example, you can use free library KeyChain.Net that provides clear API for saving and retreading data from Android and iOS KeyStores.
If you want to save Class with account information and credit card number, you can serealize that class using JSON Newtonsoft.Json
using System;
using Newtonsoft.Json;
public class Account
{
public string user_id;
public string credit_card_number;
public override string ToString () {
return JsonConvert.SerializeObject (this);
}
}
Save Account:
string accountKey ="secure_key";
public static bool SaveAccount (Account account) {
KeyChainHelper keyChainHelper = new KeyChainHelper ();
return keyChainHelper.SetKey (accountKey, account.ToString ());
}
Get Account:
public static Account GetSavedAccount () {
KeyChainHelper keyChainHelper = new KeyChainHelper ();
string jsonAccount = keyChainHelper.GetKey (accountKey);
if (jsonAccount == null || jsonAccount == "") {
return null;
}
Account user = JObject.Parse (jsonAccount).ToObject<Account> ();
return account;
}
I have a neo4j social network db and have a usecase to look through a bunch of user ids and check how many user ids are present in the graph. The User looks like this :
#NodeEntity
public class User {
#GraphId
Long nodeId;
#Indexed(indexName = "uid",unique = true)
Long uid;
}
my check would look somrthing like this :
for(Long uid : allUserIds){
User friend = userRepo.findByPropertyValue("uid", uid);
if(friend!=null){
//Create a relationship
}else{
//continue
}
}
Is there a way I can get rid of the findByPropertyValue for every single userId ? Is there a faster way thru which I can get all existing Users given a bunch of uids in one request ?
Thanks..
You could try with a Cypher query:
Map<String, Object> params = new HashMap<String, Object>();
String query = "start user=node:__types__(className=\"<package>.User\") where ID(user)>=0 and ID(user) in {uids} return user"; // substitute <package> with the full package name
params.put("uids", allUserIds); // allUserIds should be a Collection<Long>
Collection<User> users = neo4jOperations.query(query.toString(), params).to(User.class).as(Collection.class);
for (User user: users) {
...
}
You're already doing it right.
There is also findByQuery afaik, that allows you to pass in a lucene query which would be "uid: value1 value2 value3"
I am using persistent object in blackberry to store config details specific to the app. Here is how I am implementing the class
public class Preferences implements Persistable
{
private static PersistentObject persistentObject = PersistentStore.getPersistentObject(0x2759d6ff72264bdbL);
private static Hashtable tbl = new Hashtable();
public static void storeLoginToken(String token)
{
token = removeCharAt(token,0);
token = removeCharAt(token,token.length()-1);
tbl.put("token", token);
persistentObject.setContents(tbl);
persistentObject.commit();
}
public static String getLoginToken()
{
Hashtable tbl = (Hashtable)persistentObject.getContents();
try
{
String token = tbl.get("token").toString();
System.out.println("Token = "+token);
return token;
}
catch(Exception e)
{
return null;
}
}
}
But if I uninstall/delete the app these stored values are not getting deleted. When I installs the app for next time the app is fetching the old stored values.
How can i do this properly in blackberry?
Thanks
Create a custom hashtable class like this
package com.myapp.items;
import net.rim.device.api.util.Persistable;
import java.util.*;
public class MyAppHashtable extends Hashtable implements Persistable{
}
And change your code to
public class Preferences
{
private static PersistentObject persistentObject = PersistentStore.getPersistentObject(0x2759d6ff72264bdbL);
private static MyAppHashtable tbl = new MyAppHashtable ();
public static void storeLoginToken(String token)
{
token = removeCharAt(token,0);
token = removeCharAt(token,token.length()-1);
tbl.put("token", token);
persistentObject.setContents(tbl);
persistentObject.commit();
}
public static String getLoginToken()
{
MyAppHashtable tbl = (MyAppHashtable )persistentObject.getContents();
try
{
String token = tbl.get("token").toString();
System.out.println("Token = "+token);
return token;
}
catch(Exception e)
{
return null;
}
}
}
This is so that we adhere to the following info from RIM
The BlackBerry persistence model
When you use the BlackBerry persistence model, data is only deleted if the store contains data that belongs to the removed application.
For example, if an application stores an object with a package called com.mycompany.application.storage and no other application on the BlackBerry smartphone makes reference to the package, the persistent store and the removed application are deleted.
The same is true if the object is wrapped in a container such as a Vector. Even if only one of the elements of the Vector has a package name that is not used by other applications, the entire Vector is removed from the persistent store.
Note: If the application does not store any objects with an identifying package structure, (for example, an application that stores java.util.Vector or javax.microedition.location.AddressInfo objects), the application should create and use a class that extends Vector in order to identify that Vector belongs to the given application. When you store this Vector, which is identified uniquely by its package, you guarantee that the data is removed from the persistent store when the application is removed.
This info is from here