B4A Library ComboSearch

Name: ComboSearch
Version: BETA 0.99 and BETA 1
Date: 05/16/2014
Author: LucaMs

[Version BETA 1: Added GetEditText, GetListView]

Dependencies: Libraries: IME 1.05, JavaObject 1.20, Reflection 2.40, SQL 1.20

Description:
First of all, since it is a beta, will surely be full of flaws and shortcomings.
So I invite you to point out every flaw and properties that I forgot to add.


ComboSearch is a custom multifunction view.
It is practically a ComboBox with the functionality of AutoCompleteEditText.

It is noteworthy that the user will have the ability to add items at runtime.

ComboSearch allows to obtain as result the simple selected text
or an object of type Map, which is composed of the text (the key of the Map)
and an object (the value of the Map).

The returned object may contain:
Lists, Arrays, Maps, Strings, primitive types and user defined types (with some limitations).

ComboSearch can be filled with data from a SQLite database.
It may be useful, for example, load it with the description field and the primary key field of a table, which will be the text and the related object returned from the Click event.

ComboSearch allows the user to select one item from a list which is based
on the contents of the text entered by the user himself.
The list will be updated while the user will type the text and it will contain
words that start with the text entered or contain the text entered or all
the words, depending on the setting of the "SelectionMode" property.

It can be also used as a classic ComboBox.

The search part is similar to Erel's SearchView.

The main differences are:

1) this class allows you to associate a list of objects to the list of texts;

2) the search can also be performed using the exact match of the text (case sensitive);

3) it is possible to place the view also in proximity of the lower edge of the screen,
since the height of the view is equivalent to an EditText and the list
will be displayed above or below it according to the space available;

4) Many properties of the EditText and the list (ListView) can be set.
For example, the background of both, the color of both, gravity of both, etc.

Finally, it is possible to add a warning message in case the user enters invalid text.



MAIN PROPERTIES:

SelectionMode
The search mode.
It should be one of the constants provided by the class:
All = The list shown will contain all the words.
SubString = The list shown will contain only words whose contain the entered text.
Prefix = The list shown will contain only words whose prefix is equal to the entered text.

AddItemMsg, AddItemTitle, AddItemConfirm, AddItemCancel
are the texts shown to the user when he's entering a new item.

BackgroundGradient(Color1 As Int, Color2 As Int, Orientation As String)
Sets Background as GradientDrawable.
Constants are provided by the class.

OpenSetImages(EnabledBitmap As Bitmap, PressedBitmap As Bitmap, DisabledBitmap As Bitmap)
Sets the bitmaps for 3 states of button Open.

AddItemSetImages, same as OpenSetImages but for the AddItem button.

CaseSensitive
Sets if the comparison must be case sensitive.

DropDownOnly
Sets the DropDownOnly mode, that is the view works as a combobox.

InvalidValueMsg
The message to show when the user enters an invalid value.
Insert a ? as placeholder for the wrong value entered.
Default is: "? - invalid value"

InvalidValueTitle
the title of the message above.

ListGravity
Gravity of the texts of the list of items.

AllowAdd
Set the permission to add items at runtime.
Shows/Hides the Add Button.

AllowOpen
Set the permission to open the list of items.
Shows/Hides the Open Button.


MAIN METHODS:

AddItems(Items As Object, encrypt As Boolean, password As String, EmptyBefore As Boolean)
Adds a list of items to the ComboSearch.
Items can be a list of strings or a map, which keys are the texts and values are associated objects.

AddItem(Item As Object, encrypt As Boolean, password As String)
Same as AddItems but for a single item.

GetAssociatedList
Gets the list of objects associated to the list of items.

GetAssociatedObject
Same but for single item.

MapValueGetKey(MapObject As Object) As Object
Can be used to get the Text of the item when it is of type Map.

MapValueGetValue(MapObject As Object) As Object
Can be used to get the object associated with the item when it is of type Map.

LoadListFromDB(OpenDB As SQL, TableName As String, FieldName As String, RelatedFieldName As String, WhereClause As String)
ComboSearch can be filled with data from a SQLite database.
FieldName values will be the texts, RelatedFieldName values will be the related objects returned.

GetEditText [BETA 1]
Returns the EditText component, in order to change all its properties.

GetListView [BETA 1]
Returns the ListView component, in order to change all its properties.

EVENTS:

ItemClick(Position As Int, Value As Object)
Value will contain the normal text of the item selected or a map composed by a single pair:
the key of the map will be the selected text, the value of the map will be the related object.




NOTE:

Unfortunately, to manage the software keyboard, you should prepare the code of Activity
that contains the ComboSearch as follow:

1) add an IME object to the activity, initialize it and use its AddHeightChangedEvent:
B4X:
IME1.Initialize("Ime")
IME1.AddHeightChangedEvent

2) create a global variable that will contain the name of the current ComboSearch:
B4X:
Private CurrentComboSearch As ComboSearch

3) in the FocusChanged event of each ComboSearch, set that variable:
B4X:
Private Sub MyComboSearch_FocusChanged(HasFocus As Boolean)
    If HasFocus Then
        CurrentComboSearch = MyComboSearch
    Else
        CurrentComboSearch = Null
    End If
End Sub

4) delegate the HeightChanged event of IME:
B4X:
Private Sub Ime_HeightChanged (NewHeight As Int, OldHeight As Int)
    If Not(CurrentComboSearch = Null) Then
        CurrentComboSearch.Keyboard_HeightChanged(NewHeight, OldHeight)
    End If
End Sub



Tags: Combo, ComboBox, Spinner, SearchView, AutoCompleteEditText
 

Attachments

  • ComboSearch Test.zip
    13.4 KB · Views: 511
  • ComboSearch Beta Library.zip
    30.4 KB · Views: 444
  • ComboSearch Beta 1 Library.zip
    32.8 KB · Views: 487
Last edited:

LucaMs

Expert
Licensed User
Longtime User
In the attached example the functionality to add item at runtime seems to not work; in fact you can use this functionality only when the ComboSearch is in "normal mode", that is when it should not return an object: the user could not enter an object at runtime, in addition to the text!
 

georgelbs

Member
Licensed User
Longtime User
Hi LucasMs i am testing your ComboSearch, but i can´t set the properties of the listview (background ,textcolor, textsize ,etc)
 

georgelbs

Member
Licensed User
Longtime User
The only property for the listview that I can see is listgravity, all the other (textsize, textcolo, etc ) are properties for the edittext.
Another question, exist any method to clear the edittext and the selected item?
 

LucaMs

Expert
Licensed User
Longtime User
Most likely you're right (it's been almost 3 months and they are too many for my 2 bytes of memory in the brain :)).

Although it is not very correct and professional, I think I will explain both the EditText and the listview, so you can directly edit their properties, otherwise I should replicate all of them, spending a lot of time and extending the code.

Soon (today?).
 

georgelbs

Member
Licensed User
Longtime User
Hi LucaMs, i am using this library, the only problem is a delay in populate the combosearch from a database. Loading 500 records aprox. 20 secs or more. An less than 2 secs in searchview. Please help me to figure how to fix this issue. Is the source code available, maybe I can find the trouble.

thanks in advance
 

LucaMs

Expert
Licensed User
Longtime User
Hi LucaMs, i am using this library, the only problem is a delay in populate the combosearch from a database. Loading 500 records aprox. 20 secs or more. An less than 2 secs in searchview. Please help me to figure how to fix this issue. Is the source code available, maybe I can find the trouble.

thanks in advance


I have to find the time (and the source code!)

Are you sure the rest work? I fear to remember some big bugs.
 
Top