Dynamically generating headings with clickable list items displaying grouped contents - java

Apologies for the vague title but this is a bit hard to explain... though I'll try my best!
I have a task to replicate some specific functionality on an application screen and thus while the method by which this is being done may seem odd, there is no allowance to variate from the specific task.
Firstly, all data is retrieved from an SQLite database. This retrieval part is no problem. The available columns in the database are:
pk (primary key)
heading_id
heading_name
sub_heading_name (can be duplicated on multiple rows with different contents)
sub_heading_contents
The final screen should display the following:
<heading_name>
<clickable List(View?) of abbreviated sub_heading_contents>
<next heading_name>
<next clickable list>
<etc...>
So basically just a list of headings and brief descriptions of sub_heading information. For instance, this might look like:
Food
Sticky Date Pudding is a...
Chocolate Cake tastes...
Ice Cream can be...
Pizza is often...
Pasta once was...
Burgers are...
Now, the weird thing is that while you see the list of sub heading details, when you click on any of them you are taken to a screen that shows information from every sub heading of the same name. In other words, clicking on the first 3 sub heading descriptions under food (pudding/cake/ice cream) would take you to another page under the sub heading name "Dessert" with all 3 descriptions listed on the same page, while clicking on the last 3 (pizza/pasta/burgers) would take you to a different page with the sub heading name "Mains". So you see all the sub heading descriptions in a big list in a seemingly unordered fashion but they are grouped by hidden sub heading names that are only apparent once you click on them to be taken to the next page.
So my problem now is... what would be the best way to store and reference the data retrieved from the database in order to create such a list? I've tried some variations of maps, treemaps, custom classes, lists, etc., but all seem to fall down at some point during the process (no doubt because I'm not using them properly!).
I wish I had some code to paste but it's all so disorganized at the moment I wouldn't even know where to start. Any even vague suggestions as to how such a thing might be accomplished would be greatly appreciated!
Thanks!

You appear to have a three-tier structure, of which only two tiers are directly user-visible. An example of this tiered structure, from your prose, is:
Food -> Dessert -> Pudding
For the purposes of this answer, I will call these the topLevel, midLevel, and detail tiers. I haven't the foggiest notion how this maps to your terms, as your prose suggests that both "Dessert" and "Pudding" are "sub headings", which muddies the waters. If "Pudding" appears in the UI, and "Dessert" does not, they are distinct things and should be treated as such in the data model.
Hence, this would appear to be three database tables, one for each tier, with classic 1:N foreign key relationships (i.e., the midLevel table has a column containing the key of the topLevel that contains it). Or, hold the entire tree representation in a separate file, in JSON or XML format, rather than a database.
From an in-memory data representation, a TopLevel holds a collection of MidLevel objects, each of which has a collection of Detail objects (again, using my terms, not yours).
In terms of UI, since you have elected to only show two tiers, you could use ExpandableListView, with TopLevel instances driving the parent nodes and the aggregate collection of Detail objects (for all MidLevel objects in the TopLevel) driving the child nodes.
The problem with your desired UI is that you have to have all of your TopLevel, MidLevel, and Detail objects in memory, at least to the level of knowing their parent-child relationships and enough data to drive the ExpandableListView UI. If your data model is big, this will be expensive.

Related

Solution for selecting an appropriate category for something based off of the words within its description (java)

Im trying to better organise the types of tasks regularly sent to my team based off of the titles and short comment people enter.
Our team only handles a handful of issues (maybe 10 or so) different types of tasks, so I've put together a list of common words used within the description of a particular type of task and i've been using this to categorise the issues. for example.... an issue might come through like "User x doesn't have access to office after hours, please update their swipecard access level". what i've got so far is if the comments contain 'swipecard' or 'access', its a building access type request.
I've quickly found myself with code that's LOTS of ... if contains, and if !contains...
Is there a neater way of doing what im after?
If you want to make it complex, it sounds like you have a classification problem.
If you want to keep it simple, you're probably on the right track with your if statements and contains(). To get to a cleaner solution, I would approach it as follows:
Create a class to modify your categories - give it two attributes: String categoryName, List<String> commonlyUsedWords;
Populate a list with instances of that class - one per type.
For each issue, loop through the list of categories and check how many words match, and store that as a percentage (e.g. 8 out of 10 words match, therefore 80% match).
Return the category with the highest match rate.

Graph algorithm to find the most likely ancestor of a node

I'm working on the Wikipedia Category Graph (WCG). In the WCG, each article is associated to multiple categories.
For example, the article "Lists_of_Israeli_footballers" is linked to multiple categories, such as :
Lists of association football players by nationality - Israeli footballers - Association football in Israel lists
Now, if you climb back the category tree, you are likely to find a lot of paths climbing up to the "Football" category, but there is also at least one path leading up to "Science" for example.
This is problematic because my final goal is to be able to determinate whether or not an article belongs to a given Category using the list of categories it's linked with : right now a simple ancestor search gives false positives (for example : identifies "Israeli footballers" as part of the "Science" category - which is obviously not the expected result).
I want an algorithm able to find out what the most likely ancestor is.
I thought about two main solutions :
Count the number of distinct paths in the WCG linking article's category vertices to the candidate ancestor category (and use number of paths linking to other categories of same depth for comparison)
Use some kind of clustering algorithm and make ancestor search queries in isolated graph spaces
The issue with those options is that they seem to be very costly considering the size of the WCG (2 million vertices - even more edges). Eventually, I could work with a solution that uses a preprocessing algorithm in O(n) or more to achieve O(1) later, but I need the queries to be overall very fast.
Are there existing solutions to my problem ? Open to all suggestions.
Np, thanks for clarifying. anything like clustering is probably not a good idea, because those type of algorithms are meant to determine a category for an object that is not associated with a category yet. In your problem all objects (footballer article) is already associated to different categories.
You should probably do a complete search through all articles and save the matched categories with each article in a hash table so that you can then retrieve this category information when you need to know this for a new article.
Whether or not a category is relevant for an article seems totally arbitrary to me and seems to be something you should decide for yourself (e.g. determine a threshhold of 5 links to a category before it is determined part of the category).
If you're getting these articles from wikipedia you're probably going to have a pretty long run working through the entire tree, but in my opinion it seems like it's your only choice.
Search with DFS, and each time you find an arcticle-category match save the article in a hashtable (you need to be able to reduce an article to a unique identifier).
This is probably my most vague answer I've ever posted here, and your question might be too broad... if you're not helped with this please let me know so I can consider removing it in order to avoid confusion with future readers.

Views containing calculated values (category totals) not shown in XPages

I was reading through a few active SO questions and I came across this one that reminded me of a question that I have had for a while.
In a few applications that I have done, there came a point when I had to use a view that uses category totals to display some easy to read values, such as cost, or counts, or whatever. Up until now, I have always had to find workarounds for these totals because I could not get them to show up in the dynamicViewPanels, or anything else of the like. My solutions have always been, (as David Leedy suggests in the linked question) static HTML tables displaying the counts with the category, View Navigators which display the information in repeaters, and building complex dialogs which allow the user to pick certain information to get a calculation table with the appropriate values and formulas etc.
My question is, did I overlook something in the controls that actually allow these column totals to show up in the existing view panel controls?
EDIT
Just for the sake of clarity, I am not talking about a column with a calculated value, but really the totals for categories.
There is a ready baked function that uses a ViewNavigator to get to values from category totals. That just might do the trick for you.

Retrieving random data from SQLite

I want to design a language learning Android application where random set of questions are retrieved from the database and displayed to the user.
I have different type of questions such as multiple choice questions, fill in the blanks, matching the correct choice, arranging the words into a proper sentence and listening to audio and answering the comprehension questions.
How to store these different types of questions in the database and retrieved randomly and displayed to the user to be answered?
I am using SQLite.
This is a pretty broad question and there are a lot of things you need to consider. For example, the modelling of your questions, and persistence -- storing them in the DB. I made a similar Android app a few years ago.
The first thing I suggest is working in incremental steps; things are much easier if you start out with, say, multiple-choice questions only (that all have four answers). Then once you have that working, add other types of questions.
For modelling your object domain, I suggest starting with a single base Question class. This can contain some common properties, like the unique ID, the name/shortname, perhaps some sort of difficulty metric.
After this, you can have sub-types of questions, such as a MultipleChoiceQuestion, a ShortAnswerQuestion, and an EssayQuestion. They may have other properties (like the list of possible answers for mutliple choice).
Mapping all this to a bunch of tables is not trivial. The usual method is one table per class; another option is one table with all the properties for all classes, and some sort of 'type' discriminator. I suggest the former, since it's a cleaner separation of attributes.
Finally, in terms of random, there are two approaches: random on the database level (more efficient, since it returns less data) or random on the application level. The difference is "hey database, get me N random questions" vs. "hey database, get me all the questions; hey application, pick N random questions."
To leverage the DB, you can use this answer here, where they use ORDER BY RANDOM() LIMIT N. I don't know about SQLite, but in SQL Server, with large data sets, this can be extremely slow; if you have a lot of questions, profile it and see if this is too slow for your app.
If you have a base Questionstable with all questions, this will work quite easily for you. If not, you may need to do something similar where you poll forAmultiple choice questions,Bshort answer questions, etc. whereA + B + ... = N` number of questions you want.
The approach I use for this type of problem is to uniquely identify each of your rows. A sequence ID is fine.
Each time you go through the loop, place all of the IDs into an array structure. Each entry in the array has a random number and an ID into your SQLite table.
After you do this, sort the array and now you have randomly sequenced the questions. Process in order.
Your question is too abstract, still trying to answer your question.
First of all, you will need to create a database which will contain a table that uniquely identifies your questions.
And then, you can fire the query as:
SELECT * FROM table ORDER BY RANDOM() LIMIT 1; which can be used for selecting a random row from sqlite table.
And then, simply code for displaying that question.

JasperReport: Purpose of the Details Band

I figured this would be something that would be fairly-well documented as its a central theme to JasperReports, however I can't find an answer for this anywhere.
What is the purpose/function/intention of a details band? Is it supposed to just be the central or core part of a report?
From another question it was pointed out to me that there is a 1:1 relationship between a details band and a record/bean provided by the JRDatasource. This revelation brings to light a few tangential questions:
It is possible to add detail bands programmatically in Java; what happens if you specify more/less detail bands (programmatically) than there are records/beans returned by your JRDataSource?
What is the relationship between a details band, and say, a page inside an exported PDF document? Does 1 details bands translate to 1 page?
What happens if you pass the JasperFillManager a null data source? Is it possible to still have detail bands?
I don't like to ask multiple questions at a time, but these are so similarly-related I'd rather do it all at once than clutter SO with multiple nearly-identical questions. Thanks in advance.
The details band is indeed the band where each element of the data source is reported. The report engine automatically iterates over the data source and inserts data into the template of the detail band with respective element of the data source.
You may of course have several elements on a single page. According to the properties of the band (split allowed, height, etc.), the paging will be handled by Jasper Reports automatically, and it's the engine that will thus decide how many elements are printed on each page, when to go to the following page, etc.
The details band is not printed if you have nothing in the data source. The printing of the other bands depend on the parameters of the report.
The purpose of the detail band is to provide you with a model where you place and configure report elements. I don't think the relationship between a bean and detail band is one to one, since you can place many bean property elements in one bean. So, i would say the detail band is tightly tied to a collection of report bean elements.
One difference though, is the fact that detail band's functionality is irrelevant to how many beans/records you provide through a JRDataSource. The detail band will iterate through all of them until the data source is "consumed".
Furthermore, i personally find it very useful that the detail band allow you to iterate through a collection of bean properties. So, placing several properties in a detail band will iterate through all of them, before proceeding forward.
If you pass a null as DataSource you wont get any data on the report, and only static text will show up.
As I remember it, you can see the details band as the "model" for a row/record in the report. All elements you put inside the detail band will be repeated for each record provided by the JRDatasource.
For example, your details band might have two text fields, one with the value ${companyName}, and one with the value value ${revenue}.
If you now pass your report three rows, then "companyName" and "revenue" will be evaluated for each of them, and you may get something like:
|Apple | $1000,000,000|
|Microsoft | $500,000,000|
|My amazing company | $12|
I.e., the detail band contents has been repeated three times.
So:
- As you see you probably only need one details band configured. Not sure if it's allowed in the JRXML to have multiple ones.
- There is no relationship between the details band and a page. Pagination is handled separately.
- Not sure, to be honest. My best guess is either an exception or you get no rows where the detail band is supposed to be rendered.
You misunderstand what a detail band is.
The detail band of your report will be printed for each of the elements in your datasource. If you use a database datasource, the detail will be printed as many times as rows has your resultset. If you use a JRBeanCollection datasource, the detail band will be printed as many times as items has your collection.
To answer your questions:
You can't specify more than one detailband, it's illegal. You can
use subreports for that purpose.
The report will output as many detail bands as it can per page. On the otherhand, you can make the detail band the same height your desired report
output will be (minus header/footer/etc height). If you want to print the report in an A4 paper you
can make your detail band 297 mm high (considering you have no other bands). Then each detail band will be
printed in a separate page.
If you pass an empty datasource, the report will be generated with
no pages.
The information you have is correct, you've just had a slight misunderstanding of what it means.
The 1:1 relationship is at fill-time; Once the report has been filled there will be exactly 1 detail band for every record in the dataset. When you are designing the report you only add the band once, but that one band is repeated over and over again when the report is filled.
You can add as many detail bands as you wish. Each of the bands have a 1:1 relationship with the dataset records, and all of the detail bands are filled before moving on to the next record (i.e. If you have three bands A, B, and C; Their order in the report will be ABCABC... not AAA...BBB...CCC...).
It is possible to add bands programmatically, but the important point to note is that you are adding bands to the report design, not to the completed report. So just as with a jrxml design, you add the band once and it gets repeated for each record. Check this example.
There isn't a relationship between report pages and detail bands. The report filler will try to put as much onto a page as possible while respecting the splitType of each band as well as other report properties.
Is passing a null datasource is allowed? In any case the 1:1 relationship is still valid: A dataset with 0 records produces 0 detail bands in the report.

Categories