Making a pyramid structure of multiple list views and items - java

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.

Related

Can an ArrayList made using a constructor from a class be saved to SharedPreferences?

So I am making a simple wine list app. There are three main activities.
MainActivity - this is where the list of wines is, along with a FAB that takes you to the AddWineActivity. Each list item on this page only shows a thumbnail, the wine name, price, and rating.
AddWineActivity - has a couple of EditTexts (to get the wine's name, price, and description), a ratingbar (to get the wine rating), and a button that converts the details into strings and puts them into into intent extras.
WineDetailsActivity - This will have a nice page that has all of the details for the wine in the list you clicked on.
I have the app pretty much working how I want it to currently. The only thing is that I need to actually save the list of wines so it wont reset after you go back in to add another wine, or if you leave the app and come back.
Here is the tricky part (which doesn't help since I don't know too much about saving to device anyway yet). I am adding to the arraylist using a Class.
I have a Wine class that has a constructor that looks like this:
public Wine(String mWineName, String mWinePrice, String mWineRating, String mWineDescription, int mWineImageResourceID) {}
So then on the MainActivity, to add to the list it looks like this
wines.add(new Wine(getIntent().getStringExtra("WINENAME"), getIntent().getStringExtra("WINEPRICE"), getIntent().getStringExtra("WINERATING"), getIntent().getStringExtra("WINEDESCRIPTION"), R.drawable.mywinelogo));
What would be the recommended way to add a list that looks like that to the device? Would it still be shared preferences or am I not on the right track?
If more details about the app are wanted, just ask for them and I'll provide them.
Thanks!
What would be the recommended way to add a list that looks like that to the device? Would it still be shared preferences or am I not on the right track?
In the end, you have a tabular data structure: rows (wines) and columns (name, rating, etc.).
When you have a tabular data structure, save it to something that is table-friendly. That could be a SQLite database or some form of file (e.g., JSON, XML).
SharedPreferences is not well-suited for this. You could convert the table to JSON and store it as a string preference, but that's relatively uncommon. IMHO, that would only make sense if most of the rest of your data were more natural for SharedPreferences (e.g., you were using PreferenceScreen to collect them) and wanted to keep everything together.

Generating a list from Database of Objects

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.

Create a ListView that is fed by two ArrayLists at once OR two ListViews that appear as one in Android?

Basically, what i wanna do:
A listView fed by two ArrayLists at once, and each list put in a section
Two ListViews that appear to the user like one, so if you scroll to the end of the first ListView you can smoothly scroll down further down the secound listView.
Yet i lag of knowledge to do so.
Following are some details on the problem i need to solve and why i am seeking a soloution like stated above.
I am trying to build a rather complex listview in android. Its data is stored in one ArrayList at the moment. Each row's data is provided by a custom object. I sorted the entries by date,
which works fine. I now need to
Split that table into two sections based on wheather a secound date provided by my custom object lies in the past or not (so basically based on a boolean)
Add a seperator in custom design between those two sections
If any of my two sections is empty, i need to ad the coresponding placeholder showing a message
This begs for a soloution involving two lists, it would be way easier to sort the entries and to handle design.
In case you ask what i tried: I already worked around it by mapping the relevant days way more into the future so they are on top of the list
Collections.sort(newsList, new Comparator<News>() {
#Override
public int compare(News p1, News p2) {
DateTime startDate = new DateTime(p2.getValidUntil());
DateTime endDate = new DateTime(p1.getValidUntil());
if(Util.isEntryRelevant(p1)){
endDate = endDate.plusDays(1000);
}
if(Util.isEntryRelevant(p2)){
startDate.plusDays(1000);
}
Minutes diff = Minutes.minutesBetween(endDate, startDate);
diff.getMinutes();
return diff.getMinutes();
}
});
Which is probably the dodgiest piece of code ive written so far. A few more workarounds come to my mind, but they all seem to get messy and unclean.
Essentially, you have to implement a custom BaseAdapter that knows to iterate over your two data sources. You could save yourself some time by using something like this to which you can add several regular adapters.

Struggling to get my List View working properly

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.

Where do i even start with this android project? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I know there are questions about sql databases, and things like that. i have been searching through the site for something that'll help, but it's only confusing me further. I'm reluctant enough to ask here, as the last time i asked for a nudge in the right direction, the question got closed because i didn't post up code.
I'd post some if i even knew where to begin.
But i digress.
I'm looking for the best (easiest?) way to do this:
Small interface, drop down menu with list of names, when a name is selected, it shows a small list of details about the selected name.
here i mocked up the basic idea in dreamweaver:
ideally it would call up the user info as needed.
I have no idea how to make something like this for android (or..at all really). Would anyone have any tutorials or can you point me in the right direction?
Thank you to anyone who can help.
Well, if you don't know anything about Android, how about doing some basic HelloWorld tutorials?
Anyway, you'll have to make some class that holds the info about your users (Location, Phone etc).
The dropdown thingy is called a spinner in Android, learn about how to put it in your layout, how to populate it, how to react to an item being selected from it.
Then think about how you want to present the info on the screen, learn about XML layout, TextViews etc.
We won't code this for you and it won't code itself, just start somewhere.
To start developing for Android, you could try the Getting Started tutorial on the android.com site: http://developer.android.com/training/index.html
I think you have the first step done. To me, if you can visualize and draw out what you want, the next step is to just dive head first into the code. You will learn, just don't be afraid to do some internet searches first.
First of all, I always recommend that new Android developers look at the Android lifecycle to understand what happens where. For your application, you should understand what an Activity is, and what happens in onCreate(). Do some reading and try to grasp what an Android Activity is.
Next, you should look at working with Android Layouts. This will help you put something on your screen. By default, a basic layout.xml file is generated when you create a new Android application in Eclipse. Use this as a basic framework for dropping Input Controls on to the screen. The specific "drop-down" functionality you are looking for can be found in the Spinner control.
Once you have a Spinner on your screen, you can then fill it by creating an Array of values that you would assign to an ArrayAdapter. This ArrayAdapter then gets associated directly to the Spinner to fill it.
Use the Android Developers site, it is very useful. They have tutorials and sample code in the SDK as well.
This answer should have plenty of terms, links, and direction for you to get started.
Update:
I would like to expand on my answer to be more specific, but I think I need a little more information. For example, are you pulling these names from anywhere specific (ie. the device itself?). Regardless, the names have to be put in the form of some type of array, either a String Array or an ArrayList. This is only because that is how ArrayAdapters work and ArrayAdapters are what populate Spinner objects. So I guess what I am trying to say is that regardless of how you get your names, you are going to have to organize them in some type of array to put them into the Spinner (if you want to keep it simple of course).
Now, once you get the names put into your array and you can click the Spinner to show them all, you now want to trigger an event that pulls the data you would like to show beneath it. I would recommend doing this in 1 of 2 ways. You can select a name from your Spinner and then hit a button to initiate a search for the other information you would like to show. This would be a better solution if you are pulling the data on the name from a network resource. If you are pulling the data associated to the name selected from the actual device (either a SQLite DB you configured or an Android system DB), I would just use an onSelectionChanged listener on your Spinner. You can do that by using something similar to this answer:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Spinner spinner = (Spinner)findViewById(R.id.mySpinner);
// create your arrayadapter of names and then set it to your spinner
// then add a lister to look for a change
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// update your view with the information for the selected user
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// do something
}
});
}
Once you can register a change in the spinner, you can then just initiate a query to determine what information to display. If you create a custom DB with all of the information associated to names, you have to create your own DB handler. If you pull the information from a network resource, make sure that you put your networking operations in a background thread, or AsyncTask. If you are pulling the information from your Android device (contact records or something), just use Android's build in functions, libraries, and API's. I would try to keep everything localized instead of having to depend on a network connection.
Once you have all of the information, you can dynamically create layouts and add them to your current view or you can just have a single layout that you change occasionally and to show and hide. Both of these solutions are completely reasonable, and I don't believe one is necessarily better then the other. I used to be a fan of just making objects invisible and showing them when I needed them, but I am getting into programmatically creating layouts on the fly when I need them. This seems to be a little more difficult for new Android developers though, so don't feel bad if you have sketchy if/else statements that just show and hide XML layouts.
Anyway, hopefully all of that helps!! Welcome to Android!
What you are looking for is an Android simple CRUD application tutorial, google it.
(Well, assuming you've got a basic android development knowledge...)
sample of result : http://www.theserverside.com/discussions/thread.tss?thread_id=62177
These are some useful tutorials that can be found via Google.
http://commonsware.com/Android/

Categories

Resources