Android Question B4X Searchtemplate

harinder

Active Member
Licensed User
Longtime User
In the B4X library post


on pressing Search Dialog button, B4X Searchtemplate has option of NEW, but I am not able to make a new entry which I have typed in the Search area of the template, and so template closes and btnsearch shows blank on Activity1.
I want the list to capture what I have typed in the search area on pressing NEW, and the same returned on btnsearch in Activity1. Thanks
 

Mahares

Expert
Licensed User
Longtime User
I want the list to capture what I have typed in the search area
If you want to capture the typed text in a string, you can do this:
B4X:
Dim strSearch As String = SearchTemplate.SearchField.Text
        Log(strSearch)
If for instance you typed chi the captured string is chi after you click on one of the list items that has chi in it. If I did not understand your question, please ignore or explain with a clear example.
 
Upvote 0

harinder

Active Member
Licensed User
Longtime User
Ok..If I type casablanca, and it is completely not there in the list, and then I press NEW, I want list to be updated with casablanca alphabetically and casablanca displayed on search button in Activity1..Thanks
 
Last edited:
Upvote 0

harinder

Active Member
Licensed User
Longtime User
So essentially 3 things happening on pressing NEW:
1. File.writelist of casablanca to colors.txt
2. File.readlist of colors.text to template list
3. Display of casablanca on searchdialog btn in Activity1
 
Upvote 0

harinder

Active Member
Licensed User
Longtime User
In a nutshell, if Result on selecting one of the clv items is considered as dialogresponse_positive, then how do I implement clicking of NEW?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Your best bet is to zip and Export a small project to the forum, because you cannot write to colors.txt file while being an asset file. It has to be copied to Internal or another store. There is no NEW ??? in Erel's example. Perhaps someone with a better understanding of what you like to achieve can help you.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Upvote 0

harinder

Active Member
Licensed User
Longtime User
B4X:
Sub btnSearch_Click
    Wait For (Dialog.ShowTemplate(SearchTemplate, "", "NEW", "CANCEL")) Complete (Result As Int)
    If Result = XUI.DialogResponse_Positive Then
        btnSearch.xLBL.Text = SearchTemplate.SelectedItem
    else if Result = XUI.DialogResponse_Negative Then
        Dim s As String= SearchTemplate.SearchField.Text
        items.Add(s)
        items.Sort(True)
        File.writeList(File.DirDefaultExternal, "colors.txt",items)
        SearchTemplate.SetItems(Items)
        btnSearch.xLBL.Text = S
    End If
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I used NEW as DialogResponse_Negative
Add this line to the activity_Create:
B4X:
SearchTemplate.MaxNumberOfItemsToShow=500
You will see casablanca at the end of the file or sorted if you typed Casablanca
because in the lib class Erel has it set at a default of 100
File.DirDefaultExternal shoud be File.DirInternal or use runtime permissions to use other stores
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I used NEW as DialogResponse_Negative
The below code works:
B4X:
SearchTemplate.MaxNumberOfItemsToShow=500  'in activity_create

Sub btnSearch_Click
    Wait For (Dialog.ShowTemplate(SearchTemplate, "", "NEW", "CANCEL")) Complete (Result As Int)
    If Result = XUI.DialogResponse_Positive Then
        btnSearch.xLBL.Text = SearchTemplate.SelectedItem
    else if Result = XUI.DialogResponse_Negative Then
        Dim s As String= SearchTemplate.SearchField.Text
        Items.Add(s)
        Items.SortCaseInsensitive(True)  'if you want to mix cases
        File.writeList(File.dirinternal, "colors.txt",Items)
        SearchTemplate.SetItems(Items)
        btnSearch.xLBL.Text = S
    End If
End Sub
 
Upvote 0

harinder

Active Member
Licensed User
Longtime User
You will see casablanca at the end of the file or sorted if you typed Casablanca
because in the lib class Erel has it set at a default of 100
Yes. I have changed it to 500 in searchtemplate.bas in XUI views.

File.DirDefaultExternal shoud be File.DirInternal or use runtime permissions to use other stores
Yea..thank you..


My requirement is to have 3 buttons..NEW, DELETE and CANCEL placed under the clv in the searchtemplate. I have used dialogresponse_negative for NEW. How can I implement routine for DELETE button?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
My requirement is to have 3 buttons..NEW, DELETE and CANCEL placed under the clv in the searchtemplate. I have used dialogresponse_negative for NEW. How can I implement routine for DELETE button?
B4X:
Sub btnSearch_Click
    Wait For (Dialog.ShowTemplate(SearchTemplate, "DELETE", "NEW", "CANCEL")) Complete (Result As Int)
    If Result = XUI.DialogResponse_Positive Then
        Log("size before delete: " & Items.Size)
        Dim str As String =SearchTemplate.SelectedItem
        btnSearch.xLBL.Text = str
        Items.RemoveAt(Items.IndexOf(str))
        Log("size after delete: " & Items.Size)
    else if Result = XUI.DialogResponse_Negative Then  'corresponds to NEW
        Dim s As String= SearchTemplate.SearchField.Text
        Items.Add(s)
        Items.SortCaseInsensitive(True)  'if you want to mix cases
        File.writeList(File.dirinternal, "colors.txt",Items)
        SearchTemplate.SetItems(Items)
        btnSearch.xLBL.Text = s
    End If
End Sub
 
Upvote 0

harinder

Active Member
Licensed User
Longtime User
But with this, selecting a choice from template is now deleting that choice. I want selecting, adding,deleting
 
Last edited:
Upvote 0

harinder

Active Member
Licensed User
Longtime User
Requesting a reply from Experts to type a solution from their left hand, while sipping coffee from the right..thanks
B4X:
If lefthanded then
   righthanded=true
End if
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I want selecting, adding,deleting

Maybe something like this:
B4X:
Sub btnSearch_Click
    Wait For (Dialog.ShowTemplate(SearchTemplate, "SEL/DEL", "NEW", "CANCEL")) Complete (Result As Int)
    If Result = XUI.DialogResponse_Positive Then
        Dim str As String =SearchTemplate.SelectedItem       
        Msgbox2Async("SELECT, DELETE or CANCEL? " & str, "B4X Search Template Example", "SELECT", "CANCEL", "DELETE", Null, False)
        Wait For Msgbox_Result (Result As Int)
        If Result = DialogResponse.POSITIVE Then
            btnSearch.xLBL.Text = str
        else if Result=DialogResponse.NEGATIVE Then
            Log("size before delete: " & Items.Size)
            btnSearch.xLBL.Text = str
            Items.RemoveAt(Items.IndexOf(str))
            Log("size after delete: " & Items.Size)
        End If
    else if Result = XUI.DialogResponse_Negative Then  'corresponds to NEW
        Dim s As String= SearchTemplate.SearchField.Text
        Items.Add(s)
        Items.SortCaseInsensitive(True)  'if you want to mix cases
        File.writeList(File.dirinternal, "colors.txt",Items)
        SearchTemplate.SetItems(Items)
        btnSearch.xLBL.Text = s
    End If
End Sub
 
Last edited:
Upvote 0

harinder

Active Member
Licensed User
Longtime User
Thank you Mahares.. A great workaround..just changed it a bit:
B4X:
Sub btnSearch_Click
    Wait For (Dialog.ShowTemplate(SearchTemplate, "", "NEW", "CANCEL")) Complete (Result As Int)
    If Result = XUI.DialogResponse_Positive Then
        Dim str As String =SearchTemplate.Selecteditem
        Msgbox2Async(""& str, "B4X Search Template Example","SELECT","   CANCEL","DELETE", Null, False)
        Wait For Msgbox_Result (Result As Int)
        If Result = DialogResponse.POSITIVE Then
            If str<>"" Then
            btnSearch.xLBL.Text = str
                ToastMessageShow("" & str& " Selected",True)
            Else
                ToastMessageShow("Enter Something",True)
            End If
        else if Result=DialogResponse.NEGATIVE Then
            If str<>"" Then
            Log("size before delete: " & items2.Size)
            btnSearch.xLBL.Text = "Search Dialog"
            items2.RemoveAt(items2.IndexOf(str))
                ToastMessageShow("" & str& " Deleted",True)
            Log("size after delete: " & items2.Size)
            Else
                ToastMessageShow("Enter Something",True)
            End If
        End If
    else if Result = XUI.DialogResponse_negative Then
        Dim str As String =SearchTemplate.SearchField.text
        If str <>"" Then
        Dim sf As StringFunctions
        sf.Initialize
        Dim s As String= sf.Proper(str)
        items2.Add(s)
        items2.SortCaseInsensitive(True)
        'File.writeList(File.DirDefaultExternal, "colors.txt",items)
        SearchTemplate.SetItems(items2)
            ToastMessageShow("" & str& " Added",True)
        btnSearch.xLBL.Text = S
        Else
            ToastMessageShow("Enter Something",True)
        End If
    End If
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…