If no search result, then add item - android

In my android app I have a search screen.
The content is stored in a database, and a listview is populated via a ListAdapter.
This all works correct.
But I want the following functionality.
When a user searches, but gets no result, I would like to give him/her the option to add the search item.
for example:
list in listadapater is: [monkey, donkey, mouse]
when the user searches for: lion, he will see no results.
But I want the option to add lion to the database list.
Hopefully someone can get me in the right direction.
Thanks in advance,
Goldhorn

You simply add the new item into the database. Then you requery the database for a new cursor which you then pass to the adapter.

Related

Saving Information Android

I am still fairly new to android and trying to learn day by day. I am trying to create a simple test application that you register for with an email/password to learn how to use save data from users.The app I am making is going to be simple, once they register and login, the user can search through a list of items I created on my MainActivity, and when they click on the listview item it will open a new activity. On the new activity, there will be a favorites button that I want to be able to click on and it will store that listview item information onto a favorites fragment I created and it will show the user a list of movies the user saved as favorites.
I am just trying to find out the best way I can do this. I have looked into SQLite and Firebase, hoping someone can explain to me which way would be the best way to approach this and maybe link me to a tutorial if possible.
Also the adding favorites to a listview item, if any one has a tutorial on that. I have already created my main listview and using Intents take the information and pass it to the new activity. It just clicking on the favorites button and saving the information into a new listview I do not know how to do, especially for when the users closes and re-opens the app the favorites will still be there.
Any help will be greatly appreciated.
To save favourite items in a new ListView you have to store the information somewhere. If you're using SQL, then store the Primary Key value in a favourites table that you can then use to find the corresponding information from the initial ListViews table.
If you are using firebase then each item in your list should have a unique key. Save that key and then when loading the favourites ListView, use the saved key to get the key's child data.
To summarise:
Store in a favourites table/branch the items the user selects as favourites and use that table/branch then to populate the favourites ListView.
I hope this helps
Another approach you could consider doing is storing your favorites using SharedPreference. The logic that would then follow is you have each row in your list view be an object that has an ID. Anytime a user favorites that object you save it into a set and store that set into SharedPreference that are corresponding to the current account username. You can then retrieve that set and show only the favorite objects for the user later on.
As for the SQLite vs Firebase question, I have not worked with Firebase yet, most of my interactions have been custom API calls. Having said that, I think SQLite would be a nice way to handle this as it would keep things easy and give you a good understanding of both sides of the equation.
Check out this tutorial. This very next one also covers SQLite:
https://www.youtube.com/watch?v=4SwAvFYMsXE

How to set spinner adapter from firebase query

How can I get a firebase query result into a spinner adapter in android. The image above is a sample structure of my database. I want only those that available to be shown in the spinner, and only the the number is to be shown, so in this example 0954555 will be shown, but in the query i have to get only those whose available is yes. Thank you :)

Send MySQL query to a remote database using listview [duplicate]

This question already has answers here:
Sending Queries to a database from android
(2 answers)
Closed 9 years ago.
I have a ListView of sports and when pressed I would like it to send a MySQL query to my remote database and send back the rest of the data about that sport so like:
SELECT * FROM SportsTable WHERE name="????"
So I would like a way to replace the '????' with the option I have pressed in the listview
How would I go about doing this?
Thanks,
Zach
I'd advise you to read up on some tutorials before trying random things.
You can register for the OnItemClickListener which will hand you the pressed position.
From there, you can use the position to get the item from your original collection.
Any ListView tutorial will show you this.
Secondly, I hope you're not trying to connect straight into some remote DB or send a total query to some webservice which just takes any input as a query.
So I would like a way to replace the '????' with the option I have
pressed in the listview
How would I go about doing this?
So most likely you are looking for parametrized statements(and i recommend use them). Simply you created statement like this:
select * from Sports where name = ?
? is called placeholder that will be replaced with value from ListView. Probably you are using PHP so you'll use bindParam() function for bind parameter to query. Have look at PDO
And to be able to handle clicks over ListView, you need to use OnItemClickListener and in onItemClick method you can get item at clicked position from your Collection.

Android AutoCompleteTextView suggestion if nothing is found

I am making a point of sale app for Android, and I have a AutoCompleteTextView loaded in with the database of items that they can buy, but I also want the AutoCompleteTextView to double as a way to add custom items/notes into the order.
So for example, the customer is ordering Steak, but doesnt want any seasoning. My database includes options for 'No Onions' 'No Mushrooms', etc, but not 'No Seasoning', and if the waiter types in 'No Seasoning' into the AutoCompleteTextView, I want a suggestion 'Add as Note' to pop up.
Is there a way to add this suggestion when no other suggestions are found, either by making it always on the dropdown list (making it not able to be filtered out), or by detecting when no items are on the dropdown list and adding it in then?
I think this can be done using MatrixCursor.
When you retrieve the cursor from your database, check if its empty with Cursor.moveToFirst(). Then either fill the MatrixCursor with your database cursor or a single "Add as Note" item if empty.
I use this to combine two database Cursor in one for suggestions here (method getUrlSuggestions(), the last one). Note that this example is not very clean, as all values are casted to String before insertion in the MatrixCursor. This is not necessary as the addRow() method take an array of Object.

delete a specific item from listview stored in database in android application

I am making an android application in which i have five items in a list .On each item click data is coming from url and then i have stored the items in local database.Now i want to have delete button before each item so that user can delete the item acc to his choice.That is how to delete a specific value from database in android application.I have tried googling ,,tried some ways given,but that didnot worked
Can anyone guide me how to accomplish this task?
you need to remove the item from the position and then set the adapter again so that the list will be refreshed.
listofitems.remove(tempPosition);
setListAdapter(customAdapter );
to remove from the database. use the position to get the id.
c.moveToPosition(position);
id= c.getInt(c.getColumnIndex("_id"));
using the id you can delete
myDataBase.delete(tablename, "_id=?", new String[] {Integer.toString(id)});
you can set onclick listener for the items in the adapter and in that onclick you can delete the textview and using the above method can delete it from the database.
check this question and answer.
Deleting items from a ListView using a custom BaseAdapter
you can combine this and come to a solution. sorry i havent done something like you are doing. this is an idea.

Categories

Resources