I'm kinda new to Android Programming. I'm trying to create a list of several objects. Each list item should look like this:
_________________________________________
| Title Text View | Edit |Delete|
| Secondary Title Text View |Button|Button|
Each list item represent an object which was read from a database. My question is, how do I keep the list dynamic according to additions, edits and deletes, which the user can perform?
I thought about reading the database into an array and then using array adpater, and when the programs finishes, writing it back into the database, but I cannot match the objects' fields into each text view (or atleast I don't know how).
I'm sorry if my question seems stupid, I'm really new to android.
I think what you want to do is create an adapter which holds the data that you got from the DB.
The data will be displayed using a ListView (this is the part that will display your attached sketch).
After you change the data in the DB, you should refresh the adapter using:
yourAdapter.notifyDataSetChanged();
So your ListView will reflect the changes.
Related
How can I read a single row from a sqlite database in android studio and put the data from each field in separate TextViews? E.G: Name in one text view, last name in another and so on.... So far the tutorials I have seen used recycler views and such because they are viewing multiple rows of the database, not just one....
I am quite new to this so please be as detailed as possible...Thanks!
What I want to create
I want to create a list where each item in that list is an data table like below: The user needs te be able to sort items in the data table and needs to be able to sort the data tables itself.
Each table is going to represent a client and each item in that table is an order. The user will then collect the orders, if he collected an order the order wil dissapear but the user is also able to bring them back.
What I tried
I tried to put a Recyclerview inside a Recyclerview but this caused unitended side effects and bugs, also I read online that it is basically a bad practice. My initial intention was to use a recylcerview with a sortedlist.
I did some searching online and a lot of people recommended using categories between items so that you only need one list. But because I have data tables (CardViews) that can be independent sorted this isn't an option.
If anyone want to give me a nudge in the right direction, I would be really thankful.
What you can do you can use recycler view and in custom row check box with 8 text views and when position is 0 inflate categories like fat, calcium etc and after that position populate data and if you want next button which shows next items in list use fragments or pagination that should do the trick and you can achieve this using single recycler view. You can also use header to show categories.
I am attempting to create a list view which displays different data based on a selection from an a spinner.
My list view is implemented via a base adapter which works fine.
My data is stored in an array list of objects which each contains fields that store individual data and based on the selection from the spinner filters the data stored in the array list and displays it on the list view.
But my question is how can I clear a list view content without clearing its underlying data (stored in the array list) and calling notifyDataSetChanged? I want to be able to preserve the data stored so if the user goes back to a previous selection, the data is still present.
Thank you for your time in reading this.
As your data is stored into an ArrayList, you can simply use:
listView.setAdapter(null);
It will remove the adapter of your ListView, and so, remove all the items displayed.
The data will still be in your ArrayList so if you want to re-display this ListView, you just have to recreate your Adapter and set it again. You can even create a global instance of your BaseAdapter so you only have to create it once.
You could just pass another ArrayList to the adapter with the new data to show inside the ListView.
Or, you could create two ArrayList inside the Adapter, one named original which contains the original elements of the ListView and another currentElements which will be the real ArrayList to show changed as you want. With some custom methods, you can choose to switch between the two versions of the ArrayList.
It's could be the same concept of an Adapter which implement a Filter.
Anyway if i understand your problem, you want to change the content of a ListView based on the selection. Well, in this case i would send another ArrayList to show.
Create a list of adapters, one adapter for each selection. Then simply switch to a different adapter when the selection is changed with listView.setAdapter(adapter)
I am trying to make A app that starts which a text view (question)and below then a drop down with list options (answers to choose) depending on the result if click that item it takes you to a different view with more question text view and drop down list views options. Finally at the end result there is something different depending on what options you choose. Make sense? looking for any source code or example on how to even get start in sure its very repetitive so anything will please guys.
Well to make the drop down list you will need to use a spinner object as detailed here and then you will need to use spinner adapter to populate this list, this may be useful.
Once a user selects an answer in your quiz, you could repopulate the same layout with a different question and answers to start the next round by changing your answerArray and using invalidate(); to redraw the view (if they get the answer wrong you'd move to a "Game Over" layout instead).
As far as storing the questions and answers goes I would suggest some sort of SQLite database. SQLite databases can be stored in the app's data directory. Android has ways of creating, editing and reading databases, check this out. Most tutorials show you how to create a database with code, but if you have a predefined database of questions to ship then you can put it in the assets folder and copy it into the app's data directory from there.
The database would store questions in one table and answers in another. By using a question id field you can show which answers belong to which question. You can also show which answers are correct using a true/false boolean field.
When the apps read the answers into the adapter you could create an answer object containing the text and also the boolean right/wrong value
public class AnswerObject{
public String text;
public boolean isCorrect;
public AnswerObject(){
this.text = "";
this.isCorrect=false;
}
}
Then create an object for each answer in a question and store them in an array
AnswerObject[] answers = new AnswerObject[numberOfAnswers];
for(i=0; i<answers.length; i++){
answers[i].text = textFromDatabase;
answers[i].isCorrect = booleanFromDataBase;
}
I apologise for mistakes in code. I typed it by hand.
Im trying to make an android-app that shows a list of albums and it displays the album-title on each listitem. So far so good.
But I would also need the album-id somehow connected to each listitem to use when I get the next level to list (the album-tracks). How can I in each list item store more values for the items then the displayed text (album-title)?
Right now Im using an ArrayList (to store the album-titles) that is connected to a ArrayAdapter wich use the simple_list_item_1 layout. I get the album-info (title, artist, id) form external xml.
I was thinking on using a multidimensional array but I don't know how to connect it to the ArrayAdapter since it only is expecting an array?
Any suggestions on how I should tackle this?
Instead of using simple list adapter you should use android custom list view.
Check out this link
Hope this will help you.
try Android sample demo project
http://developer.android.com/resources/tutorials/views/hello-gridview.html
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/index.html