I currently have an app that essentially let's the user enter text in an editText. And when the user clicks the button "create", it creates a textView with the text of what was in the editText(the user can make as many as he or she wants). Simultaneously, I have a sharedPreference that will save the String entered by the user when a textView is made. My app then allows the user to go onto another activities page that has them doing something else with each piece of text they entered on the previous page. So, I need my app to retain the textViews that were created. This is why I have the sharedPreference saving the text of each textView. My question is, how can I take each String saved into my sharedPreference, and then put each onto a textView that will be created when the activity starts. I won't know the names of the key for each value, because it will be whatever the user typed in, and I won't know how many pieces of text the user will make, because there isn't a limit or minimum to how many he or she can make.
Use
Set<String> keys = pref.getAll().keySet();
to get a set of all the keys in the shared preference. Then display them however you want (a listView sounds like a good idea).
Set<String> keylist = pref.getAll().keySet();
for (String s : keylist) {
//Use sharedpreference with s as the key and get the values
}
Related
In my piece of code there is one input box so data will be fetched in that box on drop down selection. And if user do not want to insert/update/delete that data and click on save button unnecessarily then i have to show an error message that: "There is nothing to save..You pressed the save button unnecessary."
Currently , i am checking if the data in input box is equals to the database value then show that error but its not working.
if (inputVal1.equals(dbVal1.getValue()) && inputVal2.equals(dbVal2.getValue())) {
addPageError(T_NOTHING_TO_SAVE);
}
Please suggest how to handle this validation in java.
Store the data in a separate variable and simply compare it to the current values in the input box. If you do this, you could even detect if the values were changed, but then changed back to the original value. Apart from this you could also even deactivate the button and compare in an edit-event of the input box if the content has really changed and enable the button accordingly.
I'm trying to make an "Add contact" activity. The user will be able to insert multiple phone numbers and multiple physical addresses for each user (probably up to 5 is enough), as seen below. Pressing the blue "+ Add.." button will create a field, which can then be deleted by pressing the 'x' on its right hand side.
I've tried creating these fields statically, and hiding/showing them upon clicking the buttons, but I don't like this approach (primarily because if I delete an entry in the middle, and an entry exists after it, it won't move to take the previous entry's spot).
How can I create, as well as add/remove, these EditTexts dynamically?
EDIT: Also, each address field will look like so:
I'm creating an Android app which will allow the user to create CVs. I want the user to add all the details initially, but then have the option to return and edit if necessary. I want to repopulate the entry fields with the user's details which were entered previously (and saved to the database using SQLite).
I have a SAVE button which sends the data to the DB however I wasn't sure whether to add a LOAD button or whether the data will load automatically when the page is opened. Please advise.
I haven't pasted any code as I'm not sure if it will benefit my question right now.
ListView seems to be the most common method for displaying data however this doesn't help me as I require population of previously entered data to the original entry field.
There shouldn't be any load button in this case. The data should be fetched and populated automatically when the user navigates to the fragment or activity.
ListView can be used to display previously populated data.
Before I start. I do apologise for not uploading any code. But I know already understand the problem I have. I just don't know how to sort it.
The problem I'm having is that I have a List View, which is populated by many rows. Each row has a Contacts name, how many contacts the user has in common. A contact display picture and an action button.
When the user clicks on the action button. They are promoted with a Popup Box. Displaying a further four more options; unfriend, block, message, and cancel.
When the user clicks on the unfriend or block button. The row that is associated with that button is then removed from the List View. Sounds simple right?
The List View also has a search filter, which the user may use. The can search by Contact names. Whilst the user is inserting values, the list view is refreshed consecutively, displaying Contacts that contain the values inserted into the search function. Same rules apply, the user may remove, block... Etc.
The problem I face is, when the user tries to unfriend a user by searching their contact name first. It tries to remove a Contact at position 0. Now as we may know, this searched contact may actually be at position 7 in the Array List. And thus, causes a problem. It removes the wrong user. Now, if the user doesn't use the search function. It actually works, the correct position is removed from the list. And ole Joe is happy.
I clear the ArrayList first before reading contacts that contain entered search values.
I'm sorry that I can't upload code, but my Mac isn't available at this time, and thus I can't access my project SC.
Could anybody give me some kind of indication on where I could have possibly went wrong?
Thanks, Peter.
First set the tag in getView method if you have adapter
textView.setTag(position) // set the tag (assuming you have textView in row, or you can set the tag for any other control)
Then while removing
int position = (Integer)v.getTag(); // get the tag that you had set before.
your_arraylist.remove(position); // remove
// your remaining code
notifyDataSetChanged();
Hope this gives you some idea. Happy coding.
I have a SearchView in my action bar. When the user searches for a query and presses the "Go" button on keyboard, the search intent gets passed to my activity but the search field collapses and the query text is lost. How can I prevent this from happening? I would like the text to remain in the search field until explicitly cleared by the user.
Thanks
You can store the search request either in a String variable or in the shared Preferences when the "Go" button is clicked by using getQuery() when the search event happends (setOnSearchClickListener) and then write them back in either as the Query (setQuery) or as a hint (setQueryHint).
Edit (Based on the Question you can read below this answer)
How can you store the query string if the activity is recreated after every search? If you have some service that is persitent in the runtime of your aplication you could store it there, but that isn't that elegant. The easyest way to do it is to use the SharedPreferences. This is a persitant (key,value)-storage that you can use throughout your app to store and retreive values.
You initiate a SharedPreference with
SharedPreferences prefs = getSharedPreferences("mysharedprefenrences", 0);
The String can be anything you want. Whenever you use the same String to create a SharedPreferences-Object in your app, you will have acces to these values again.
To get a String, like your search query, use
String aString = prefs.getString("searchQuery", "")
with the second String being the default, if the query isn't set yet (in this case "").
When the setOnSearchClickListener is enacted you can store the Query by using an Editor.
Editor editor = prefs.edit();
editor.putString("searchQuery", queryString);
editor.commit();
Now the next time the activity is created, instead of the default String your stored String will be found.
My solution turned out to be dead simple. Apparently, I wasn't setting the android:launchMode properly for the Activity. Ideally an activity that provides search functionality should be a single top so the user doesn't have to navigate backwards through all the previous searches upon clicking the "Back" button. Mine wasn't. Therefore, every time I searched, a new instance of my search activity was initiated and added to the view stack.
I added this to my manifest file under the correct activity.
android:launchMode="singleTop"