i trying to insert my string into my database tables ,but it doesnt appear, so any advice on this how do i string my text so it able to appear on android database table
btnplayer = (Button) findViewById(R.id.button1);
tw1 = (TextView)findViewById(R.id.PX2);
btnplayer.setOnClickListener(new View.OnClickListener() {
SharedPreferences myPrefs = getSharedPreferences("PREF_COUNT", 0);
SharedPreferences.Editor myEditor = myPrefs.edit();
public void onClick(View v) {
String Player = myPrefs.getString("tw1",name);
DataStorage.this.btnplayer.setText("add name [" + Player + "]");
myEditor.putString("Player", Player);
myEditor.commit();
Please make your question more clear.
If you want store your data in database in android, you can go for SQLite database available in android http://developer.android.com/training/basics/data-storage/databases.html
but I suggest use sharedpreferences if only strings need to store.
I didn't get the meaning behind this statement String Player = myPrefs.getString("tw1",name); if you already have stored the same value (first time there will be no value so you will getting back the default "name" and then you will again store that name with the new key called "Player"). Where are you storing "tw1"? Exactly what issue you are facing?
Related
Currently I am adding my user class to a firebase database using this code:
public void onClick(View v)
{
Firebase ref = new Firebase("https://xxxxxx.firebaseio.com/");
createAccount(emailString, passwordString);
User user = new User ();
user.setEmail(emailString);
user.setPassword(passwordString);
ref.child("users").push().setValue(user);
}
Right now, since I use the .push() method, I am creating a unique ID in my database. How do I pull that unique ID? I looked at this tutorial but I don't understand how to implement it.
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference(); //get the reference to your database
User user = new User ();
user.setEmail(emailString);
user.setPassword(passwordString);
String yourKey = dbRef.child("users").push().getKey(); //get the key
dbRef.child("users").child(yourKey).setValue(user); //insert user in that node
But if you want to access that node (yourKey) later, you will need to store it in some sort of permanent storage like a database on your web server.
Great example of how to get key check these docs out helped me a lot.
Firebase Docs
// Get a key for a new Post.
var newPostKey = firebase.database().ref().child('posts').push().key;
Im using SharedPreference on android to store a Set of Strings. It is stored and retrived fine to my knowledge but when the app is restarted some data is lost. Strings are add one by one and before adding them I retrieve the set, add a String and then store it again.
This is how I store it:
Set<String> emptySet = null;
SharedPreferences prefs = getContext().getSharedPreferences(getContext().getString(R.string.pref_disagree_key), Activity.MODE_PRIVATE);
String newIdAgreed = getItem(position).getId();
if (prefs.contains(getContext().getString(R.string.pref_disagree_key))) {
Set<String> updateSet = prefs.getStringSet(getContext().getString(R.string.pref_disagree_key), emptySet);
updateSet.add(newIdAgreed);
SharedPreferences.Editor editor = prefs.edit();
editor.putStringSet(getContext().getString(R.string.pref_disagree_key), updateSet);
editor.commit();
} else {
Set<String> newSet = new HashSet<String>();
newSet.add(newIdAgreed);
SharedPreferences.Editor editor = prefs.edit();
editor.putStringSet(getContext().getString(R.string.pref_disagree_key), newSet);
editor.commit();
}
And this is how I get it back:
if (prefsDisagree.contains(getContext().getString(R.string.pref_disagree_key))){
disagree_set = new HashSet<String>(prefsDisagree.getStringSet(getContext().getString(R.string.pref_disagree_key), emptySet));
for (String item: disagree_set){
//stuff done here
}
}
I saw some similar questions about this topic but none of the answers solved my problem. Any ideas?
The StringSet is not persistent when you are trying to edit it all again after it was saved and therefore newer data that was just added wont get saved when you quit the app and open it again.
It is actually documented: getStringSet
You need to copy first the StringSet and then insert/add data to the copied StringSet:
Set<String> s = new HashSet<String(prefs.getStringSet(
getContext().getString(R.string.pref_disagree_key),
emptySet));
How do you make a variable that can be used across the application. For example, in Swift, you can name it by:
var formattedPrice: String = rowData["date"] as String
defaults.setObject(rowData, forKey: "rowData")
and you can fetch it with:
var variable = defaults.dictionaryForKey("rowData") as? NSDictionary
How can this behavior be mimicked in android studio?
The example you provided looks very similar to SharedPreferences in Android. SharedPreferences allow you to store data and access said data globally within your application. Here is a simple code snippet that shows you how to save and recall an int. You can save the variable as follows:
SharedPreferences sharedPreferences = getSharedPreferences("your_preferences", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("your_integer_key", yourIntegerValue);
editor.commit();
And then retrieve it anywhere in your application like so:
SharedPreferences sharedPreferences = getSharedPreferences("your_preferences", Activity.MODE_PRIVATE);
int myIntegerValue = sharedPreferences.getInt("your_integer_key", -1);
For an app I am developing, I am having the user input some information on one activity, and then on the next, the user will read something, in such that he/she won't be putting any information on it, but on the third activity, I want to display what the user put in the first. Is there a way I can carry that information from the 1st to the 3rd activity without going through the second?
Use sharedPreferences to store that piece of data on the phone. On the 3rd activity, read it and use it.
See it here-
http://developer.android.com/guide/topics/data/data-storage.html#pref
This can be done in many ways !
Why not use Intents to pass the data ?
STEP-1: <Eg:: From Activity1>
Pass the telefone into a new activity with the help of intents
Intent i = new Intent(getApplicationContext(), Activity2.class);
i.putExtra("title",telefone);
i.putExtra("descrip",telefone);
startActivity(i);
STEP-2: <Eg:: From Activity2>
Receive the string passed in another activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
String title= extras.getString("title");
String descrip= extras.getString("descrip");
}
STEP-3: <Eg:: From Activity2> pass data again to Activity3
Receive the string passed in another activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
String title= extras.getString("title");
String descrip= extras.getString("descrip");
}
Pass the data to Activity3
Intent i = new Intent(getApplicationContext(), Activity3.class);
i.putExtra("title",telefone);
i.putExtra("descrip",telefone);
startActivity(i);
Hope it helps ! ..........Let me know if you need more info
Use SharedPreference. Save in A1 and retrieve in A3.
Initialization
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
Storing Data
editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long
editor.commit(); // commit changes
Retrieving Data
// returns stored preference value
// If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean
Deleting Data
editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes
Clearing Storage
editor.clear();
editor.commit(); // commit changes
I'm trying to compare the value of Markers with an id in my pre-made SQLite database. The reason being is so that I can have specific details for specific markers.
For example:
If the marker clicked has id = 1 then I wish to search the database for id '1' and then grab the details from that row. I thought it was simple enough to just loop through the database, but this doesn't seem to be working. My current code is:
final Cursor dbId = monDatabase.database.rawQuery("SELECT _id from monuments", null);
int idColumnCount = dbId.getColumnCount();
dbId.moveToFirst();
while(dbId.isAfterLast() == false) {
for(int f = 0; f < idColumnCount; f++) {
mainMarkerId = dbId.getInt(0);
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
if(marker.getId().equals("m" + mainMarkerId)){
Log.v("marker.getId()", "the id is: " + marker.getId());
selectDesc = monDatabase.database.rawQuery("SELECT description from monuments WHERE _id = " + mainMarkerId, null);
selectDesc.moveToFirst();
_description = selectDesc.getString(0);
Intent descriptionIntent = new Intent(MainActivity.this, DisplayData.class);
descriptionIntent.putExtra(EXTRA_MESSAGE, _description);
startActivity(descriptionIntent);
markerId.moveToNext();
}
}
});
dbId.moveToNext();
}
}
I'm unsure if it is possible this way, or maybe I have to use a Hashmap or something.
Any help is much appreciated,
Thanks!
EDIT:
I forgot to mention that I have all the Markers already displayed on the maps, so it's more of a case of being able to get the description for that location and then passing it through to a new Intent.
SOLUTION:
Just thought I'd post the solution on here if anyone else needed it. I didn't need a Hashtable, all I used was:
selectDesc = monDatabase.database.rawQuery("SELECT description from monuments WHERE title = '" + marker.getTitle() + "'", null);
No need for all the for loops or anything. Very very simple!
your best bet is to create a HashMap of the database elements using the database id as the key and the marker as the value so then you can pull from your database and have what marker you need