I have in my app a database with two tables : country and rights. Long story short, the db tells me whether a right (there is 10 rights in total) is legal or not in a specific country.
Now, I want the user to be able to search in my db by criterias. I have a layout with checkbox. If the user check a box, it mean he want to see every country in where the right is legal. For exemple, if he check the box "criteria1" and "criteria6", the user want the list of every country where criteria1 and criteria6 are legal, but we don't care wether the other rights are legal or not.
I asigned values to the checkboxs (1 if legal, 0 if illegal, just like in my db) and passes all of them to the activity who display the result of the search.
My problem is, I can't figure out how to search in my database. I need to only get the country where where the selected criters are equal to 1, but I don't know how to formulate my sql request (since I never know which criterias are going to be checked or not). My request need to only be about the criterias who has the value 1.
I had the idea of sending all my values to a function (witch returns a cursor) where I excecute a select statement if the value is equal to one, but I don't know how I could join all the result of my selects in a cursor. I also thought about using "CASE WHEN..." but it doesn't seem to work.
Does anyone have a clue on how I could deal with my search ?
If you need precisions on my problem, please ask.
This guy here (https://www.youtube.com/watch?v=NGRV2qY9ZiU&list=PL200JxfhYIgCrrpH4rCz-uNfBTb5sng1e) has the right idea.
The clip may be a bit slow but it does exactly what you want.
He creates a custom string based on if checkbox is checked and removes it from the string if unchecked.
To get what you want, you need to do a couple of things.
First, create a table with countries as rows, and rights as columns. Add 1 for right is present in country and 0 if not. Get this into an sqlite database (eg import via csv in DB browser for SQLite, free software; don't forget to create the android_metadata table in the sqlite database - search online for this). Import the database in the app (there is plenty of documentation for this online).
Second, change the text inputed in the if/else checkbox part of the script (he writes fruit names, you write for ex. "right1 = 1", or the exact query the checkbox should do on the column right1).
You also need to pay attention to the selection.add and selection.remove (know that selection is an array list which will store all your criteria for search by column).
Third, you need to change the content of his finalSelection (View view).
Delete all he has written and just create two strings:
String final1 = android.text.TextUtils.join(" or ", selection);
String final2 = "select country from table where " + final1;
The string final2 is your key for a cursor with a rawQuery. Just create a cursor in the database and pass the key to it. This can be done easily.
PS the method android.text.TextUtils.join() is amazing :)
You can place the operator of choice there.
If you need more than one operator (and, or etc), you can create different ArrayLists which you fill in the if/else checkbox is filled and join later in the finalSelection.
Oh, btw, if you have too many checkboxes, you will get a warning in the XML file (layout has more than 80 views is bad for performance).
In order to get around that, you need to get to know grid views a bit better. After reading a few tutorials on the basic use of GridViews, a good start for checkboxes inside them is here.
It may seem like a lot, but you need to learn to use holders to get information out of the getView of the modified BaseAdapter.
If you want to understand it better, follow the arrPath.
It is a String[] filled with all the paths of images found inside the cursor (string values from the dataColumnIndex, which contains paths of images).
Within the onClick() listener of the Button, from the arrPath he extracts only the rows of the cursor that were selected by checkbox click (thumbnailsselection[i] is a boolean - with a value TRUE/FALSE for each row in the cursor).
The selected paths are placed in the selectImages String, separated by OR.
Related
I am looking for some help to find a powerfull way to allow selection of different List items.
My case is that i have for exemple a List profiles, List teams ... and i'd like to have an autocomplete input that will show, for exemple if i type Al, all Teams and Profiles objects having there member variable name begining by Al.
The result would be that i could get from the activity, on submit click performed, a List & a List containing all the objects who have been selected through the autocomplete form.
Also i'd like that the list offered to the user that match the chars he typed show the name and a picture (facebook like tag selection).
Obviously i am not asking for some code but at least some guidelines from experienced Android devs who know what to do and not to do to create this kind of thing.
Thanks
Loader are one of the best way to filter list. You init a loader which take the String constraint used for filter, the each time the user type, you update the constraint and restart your Loader.
If I suppose that all your object are cached in a SQLite database you can use a CursorAdapter, Cursor and CursorLoader.
You create the needed CursorLoader by filter the query with the content of the EditText.
If you're not familiar with CursorLoader there is the AsyncTaskLoader, with this you won't have the need of DB and to code a provider which can accept raw query. Your object in ListA, ListB, etc must inherits of common class (hum... DataThing maybe :-)), you concatenate the objects in a list then you can filter the list : What is the best way to filter a Java Collection?
That's for the data filtering. Now in order to display the data the way you want, you can display of list below the EditText field or create a custom component if you want a more advanced look.
I have in my db 2 table with a many to many relationship.
TAB_ARTICLES: {_ID, TITLE, BODY, DATE}
TAB_TAG: {_ID, NAME, COLOR, DATE}
TAB_ART_TAG: {_ID, ARTICLE_ID, TAG_ID}
I need to populate a ListView, one row for article and in every row I need to have a TextView for every label linked to that article. Like the following image
I think 2 solutions.
a. I use a CursorAdapter with a cursor made only on TAB_ARTICLE and than in every row I do a query to join the other 2 tables looking for all tags related at this article. This solution require a lot of db accesses.
b. I realize a temporary table
TABLE_TEMP: {ARTICLE_TITLE, ARTICLE_BODY, ARTICLE_DATE, TAG1_NAME, TAG1_COLOR, TAG2_NAME, TAG2_COLOR, ...}
and I use a query on this table as cursor for custom adapter. This solution use more space and have a limitation on possible displayed tags due to table columns.
Are there other ways?
Well, actually, it's a multicriterion thing: time, space, updates, search, etc. So there's no single recipe. It's very probable, however, that multiple queries will bog down scrolling. Worse, on some devices only. A temporary table may or may not be OK depending on the overall size of your data. And you may want to keep this redundant table in sync with the main one, making simultaneous updates to both.
One of the simplest trade-offs could be adding a redundant TEXT/CLOB column with the tag data (XML, JSON, other markup/separated format) to TAB_ARTICLES and keeping it in sync with your detail data. By the way, you will really need the M:M schema only if your queries substantiate that. Otherwise, the single table would suffice.
Again, I'd list and evaluate all the criteria first and decide what dimensions really need to be scalable and simplify the rest.
Ok, I have a database with id column as timestamp
I made an activity list from the db.
I want to manage the db (delete rows) using the list, but the thing is I don't want to
View the whole timestamp, in every row I'll put only the time with some info and
I want to group the list ,as in contacts grouped by alphabet, by the date.
First, how can I make group in an activity list? (Making groups to the output list not the db)
Second, what is the best way to implement this? When user chooses an item and confims delete
I should delete it from the db but I have only patial timestamp...
(My only link to the db is the timestamp - I don't actually know where to store it in the list and I don't want to put it as a string in the text view, do a substring to get it back - is there another way to do this?)
I tried to search tthe web for some examples but I only found a simple ones.
Thnx :-)
?
I think what you're trying to do is create a database of tasks identified by a timestamp. You probably don't want to use a timestamp as a unique ID for the row. Instead, use an integer and qualify it as "PRIMARY KEY" when you create the database.
group the list? I'm not sure why you want to do this in the structure of the database. It's more common to group the list in the output, and leave the db itself in as flat a structure as possible.
Retrieve the primary key when you display a list of tasks. When the user clicks a task, use the primary key to choose the task to delete. You don't have to display the primary key; it serves as a behind-the-scenes "link" between the displayed info and the db row.
http://www.vogella.com/articles/AndroidListView/article.html
I should use cursor adapter for managing db.
And this one for grouping a list:
http://code.google.com/p/android-amazing-listview/
Thnx for the efforts
Confused beginner here.
I'm extending the functionality of the android notepad tutorial program. I can successfully get the data from the sql to display in a list view and I can use the findNote function to get an individual note.
What I want to be able to do is extract an individual note AND all following notes in the table. I'd be happy with some way to iterate through the remaining notes (a puzzle for me because the rowIds are not sequential) but would also settle for designing a query that... I don't know, returns a String[] with the item with the matching id at position 0 and all subsequent items later in the array.
I'm sure there are a thousand ways to do this, I'm willing to take almost any of them. Please let me know if I need to clarify further.
Just looking at the NotesDbAdapter class in that tutorial, there doesn't appear to be any timestamp captured with the notes. You would need to extend the table structure to include this and update the createNote and updateNote methods to set that accordingly. Then you can write your SQL based upon that timestamp.
John
In my application I have a sqlite database that looks like this:
CREATE TABLE notes (_id integer primary key,
content text);
CREATE TABLE tags (_id integer primary key,
name text,
noteid integer,
foreign key(noteid) references notes(_id));
I'm storing text that can have some tags associated with it. Now I want to show this text and the tags in a ListView. However I can't figure out how to do this with a SimpleCursorAdapter. Is it even possible? My data might look like this:
sqlite> select * from notes;
1|foo bar baz
sqlite> select * from tags;
1|x|1
2|y|1
The query to get all notes and the data it returns looks like this:
sqlite> select notes._id, notes.content, tags.name from notes, tags where notes._id = tags.noteid;
1|foo bar baz|x
1|foo bar baz|y
Now, if I want to bind this data to the ListView in some way, how to do it? I would be happy if each row int the ListView contained two lines, one line with the content and one line with all the tags. Am I correct in guessing that the SimpleCursorAdapter won't help me here? What should I do instead?
SimpleCursorAdapter alone can't help you here.
If your goal is that you want one row to be one note + all its tags, you can try overriding bindView() in SimpleCursorAdapter and pouring in the tags that way. That would imply that you have already built up some sort of HashMap of note->tags and therefore can quickly determine the tags to go in the row.
To build up the HashMap, you have two choices that I see:
Build them on the fly by looking up the note in the HashMap, then doing a query to get the tags for that note if they're not found, caching them in the HashMap for later reuse (e.g., scrolling). The catch here is that you're doing a bunch of little queries (bad) and doing them on the main application thread while the user is scrolling (really bad).
Do one big query using an IN clause to get all tags for all notes, and convert the resulting Cursor into a fully-populated HashMap. Then, your per-row lookups will all succeed. This works well if you only have a modest number of rows; otherwise, this query may take longer than the user has patience for.
If your schema is flexible, you might consider whether you are better served with some amount of denormalization, such as having the tags in a single column of the notes table via a comma-delimited list or something. Even if that complicates write operations (e.g., putting tags in two places), if your reads greatly outnumber your writes, it may be worth it.