Using Arraylist display data of questions and answers - android

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.

Related

How to get database data in listview after insert new data in android [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Here's the scenario like, I have a disease table in SQLITE database it contains a list of disease. While i enter into the new activity,i will get the datas from disease table and load it in listview. Now,in listview page..im adding new disease in disease table. After insert data,how to refresh the listvew.
Any Suggestions??
You have to notify the adapter for changes on data so it will refresh the listview. In your case, call something like cursorAdapter.notifyDataSetChanged() after doing the insertion.
You can add newly inserted data into your listview's dataset that is your arraylist or any other object and then call notifyDataSetChanged of the listview's adapter.
Post more code for better understanding.
You can use swipe-to-refresh and fetch the data from database meanwhile. Fetch the new data in OnRefreshListener and then update UI.
You should notify the adapter that the data has changed and then should refresh the listView. below is a sample:
myAdapter.notifyDataSetChanged();
myListView.invalidateViews();
myListView.refreshDrawableState();

How to filter data based on items count in Firebase Database

I am writing an android chat app and I am trying to implement endless scrolling using Firebase Database in RecyrclerView using custom FirebaceRecyclerAdapter. For first messages load I get data from Firebase Database by using query that ordering data by .orderByChild() and then .limitToFirst(specificCount) so far so good. But when user scrolled to the last visible item I have to get next portion of messages and the problem comes I did not get a straight way to get messages from Firebase Database after specificCount. I tried with that code:
int startCount = mLayoutManager.getItemCount();
FirebaseDatabase
.getInstance()
.getReference()
.child(RequestParameters.FB_CHILD_MESSAGES)
.orderByChild(RequestParameters.FB_CHILD_MESSAGES_ORDERING_TIME)
.startAt(startCount)
.endAt(startCount + CustomFirebaceRecyclerAdapter.NEXT_MESSAGE_PORTION_COUNT)
.addListenerForSingleValueEvent(...)
But after I read the documentation for .startAt() I realize how stupid is this. Anyone could help to make a query that gets items after specificCount and limit received messages to some count.
I'm pretty sure I answered this less than a day ago, so I recommend scanning my answers. But I'll repeat.
The Firebase Database API is inherently not very well suited for pagination. Think of what happens when an item is inserted while the user is on page 1 (with the first ten items). They end up seeing the previous item 10 at the start of page 2, but will never have seen the newly inserted item.
That said, if you want to do pagination, it is technically possible. But instead of wanting to skip 10 items, you will instead have to tell Firebase to start the query at the last item from the previous page. This is called an anchor item and in code it looks like this:
FirebaseDatabase
.getInstance()
.getReference()
.child(RequestParameters.FB_CHILD_MESSAGES)
.orderByChild(RequestParameters.FB_CHILD_MESSAGES_ORDERING_TIME)
.startAt(timeOfLastItemOnPreviousPage, keyOfLastItemOnPreviousPage)
.addListenerForSingleValueEvent(...)
The key properties here are:
timeOfLastItemOnPreviousPage: the value of the FB_CHILD_MESSAGES_ORDERING_TIME property of the last item on the previous page
keyOfLastItemOnPreviousPage, the key of the last item on the previous page (which is needed in case there are multiple items with the same value for FB_CHILD_MESSAGES_ORDERING_TIME

android listview onclick button

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.

Display SQLite query result in Listview in Android

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 :).

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.

Categories

Resources