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.
Related
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
Edited, thnx 4 de answer but wht I meant is this-have populated listview with image,Id,and name of candidates from mysql database. Made a custom adapter with android vote button, which auto generate on every candidate in the listview. So what I want is when a voter clicks on the vote button either Id, or name is sent 2 database and then increase number of votes by +1 in the vote field of the database. Android voting app using php(android and php)Any1 pliz help.
Very Simple You can do this by getting your current count from database then increment the same in your Android code and then update the database with new value that you just incremented.
i want to show questions in list view from sqlite and under every question there is editText field to submit answer. i am fetching questions from local db through array list, but problem is when user will submit answer then next question will be displayed in list and so on.
kindly help me how to do it?
Store data in DB in sequence with number as primary key
While fetching question from SQLite in query add
limit Question_Number
Question_Number is the question limit you want to display.
First time add limit 1
Everytime when user enter answer the question and click ok.
Fetch the data from database using limit currentPosition +1
i did it by passing a variable in constructor. and showed 3 question one after the other in my scenario. and it solved my problem.
In my Android app I have an activity with a listview that displays about 4000 items that are stored
locally in a SQLite Database. If I make an edit to an item, how can I get only this change in the listview,
without having to refresh all the item list (which means a new query for 4000 results)? This query slows down the performance.
I would like something like NSFetchedResultsController of ios.
Strategy should be -
As you are editing the contact, if the update is successful you just fetch the latest info from db for this specific contact only.
Update the edited contact object in your adapter's source List/Array's specific position.
Invoke your adapter.notifyDataSetChanged().
you must have an id for each contact in your SQL lite database.
when you edit a contact update that specific record in your database on basis of the id.
if update is successfull means the information you sent to database is stored successfully .
now you can use the same informtion to update your ArrayList/HashMap whatever you are using to populate your listview.
Ex:- suppose you edited 3rd index contact in your listview on successfull update you add like yourarraylist.add(3,contact);
and the fire notifydatasetChanged.
Try these steps if possible:
Try to fetch the data from db, but do it in different thread which won't effect the main UIThread.
Then you can call the adapter.notifyDataSetChanged() on adapter object. It will do the job i hope :).
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.