I am performing a search over Strings in an ArrayList. If the Term is found I need to highlight that term and return the a string with the word where the term is present plus a word before and after that word!
Example:
Term = “some”
Searched String = “This is my awesome test String!”
Result = “my awesome test” (“some” should be highlighted here)
First off I am useless with RegEx and wouldn’t know where to start and secondly I’m not sure how to highlight text in a ListView, there are 3 TextViews per Row and I pass an Array with Data objects to the Adapter. Can I just give the Data Object Spanndable’s for highlighting?!
After some trial and error and some help i got this which seems to do the trick
(^|\S*\s)\S*term\S*($|\s\S*)
Related
I have a simple form containing TstringGrid with 2 columns, a TStringColumn and TCheckColumn added. I have seen many examples of saving the contents to file if the cells contain text or numbers. I have not seen any examples of saving with a TCheckColumn. I am assuming that I must check each CheckColumn cell, determine its state and assign a value that can be saved to file. Or maybe there is a more elegant way to do this.
As for sorting - again many examples using strings or numbers but none with TCheckColumn. I have HeaderClick enabled. On the TStringColumn I would like to sort Alphabetically - On the TCheckColumn - I would like checked items at the top of the column.
I am using Delphi 10.2.1 and will compile for Android.
Without saying you shouldn't start from here - I will just answer the specific questioN;
To keep it simple, I would:
Save: iterate through the rows and take the state of the checkboxes and prefix the string item with BoolToStr(theCheckValue)+':'+theContents of the string.
Then save the stringList.
To Load:
load into the stringList and then iterate and break the string apart using pos on the ':' and StrToBool the left portion, setting the checked item based on this.
Not got an IDE up, so haven't tested, but that would be my approach as a bit of a hack.
II would like to create a scrollable list of items that can be sorted according to different criteria that can be chosen by the user from the action bar; I was thinking of a button that says:"Sort by...". I am working on an Android app.
I am developing a tourist guide, or rather, making an app out of a paper tourist guide I have previously written, thus I have a list of monuments through which the user can browse.
It would be great if the user could sort the items of the list in alphabetical order, or according to the rating of the monuments or on the basis of a tag indicating their type (historical building, museum, etc.).
Would it be even possible to display under the name of each item, like a sort of subtitle, a series of dots or stars indicating the above-mentioned rating?
At last but not the least, would it be possible, only when the monuments are sorted by name or rating, to automatically group them by another tag?
Like in some song player apps, when you choose an artist, you get all of his/her songs displayed often grouped by album. In my case the grouping tag would represent the zone in the city of the monuments.
Despite my experience in publishing, I am a newbie in Android (followed just a few courses) and the API docs made me very agoraphobic about coding. I know I am asking much so I don't demand a detailed explanation but a few suggestions about what could be a solution, a bunch of helpful guidelines and some names of specific API docs about Array Lists and sorting methods I should look at.
Thanks already for your help.
Firstly, you need to have a custom Java Object for a particular item
for example:
public class CustomItem{
private String Name;
private int Rating;
private String Tag;
// getters and setters
}
Then you can create a Listview with Input consisting of a List of type CustomItem. Here is a good ListView tutorial: http://www.vogella.com/tutorials/AndroidListView/article.html
You can then have somethibng like a Spinner, in which you can add the sort criteria. When the user selects one sort criteria, you can sort the Input List for the ListView and refresh the listview by doing: listView.getAdapter().notifyDataSetChanged();
Hope this helps.
How can I search through an String-Array? I've got an dictionary app and the words are saved in a String-Array and it would be user-friendlier, if you could search for the word you want to look up, instead of looking its way to the word. Can somebody help?
Thanks.
You can try using an ArrayList instead. Then you can see if the word is in your 'dictionary' by using the contains method, ex:
ArrayList <String> myDictionary = new ArrayList<String>();
myDictionary.add(new String("foo"));
myDictionary.add(new String("bar"));
...
// To check if the word exists in your dictionary.
if (myDictionary.contains(new String("word_to_look_up")))
{
}
Not entirely sure what you are looking for- do you want to see if the word is in the array, or what is the goal? If you want the user to get to the word faster, changing the string-array will not help the UI jump to the right place.
If you want it to be like Google suggest you could take your array and make a tree data object, which each node representing a letter in a word. Then if the user types a, you go into that node and offer possible words.
You may use the Arrays.binarySearch() method to search an element from the sorted array.
A stupid question that Im sure is really really simple - ive been trying to fiddle to solve it myself, but its all very new to me so any wisdom would be REALLY appreciated!
With an android application (using eclipse) if I want to compile text for a text view using some data that is stored in a sqlite database (I have a working dbhelper and table I want to use), what do I do?
For example, I want a text box to say something like:
"hello"+#user_name+"its been"+#days+"since your last visit!"
where #user_name and #days are data in a table which I can currently retreive in a list (only 1 value in the list though).
How do i compile a string to be shown in the text view (and assign this string to the view)?
Help!
String textViewValue="hello "+nameList.get(0)+" its been "+daysList.get(0)+" since youy last visit";
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(textViewValue);
You said you have stored it in arryalist right?
I am not sure if you have one ArrayList or two for Name and Days. (it would be good if you paste some of your code).
If your ArrayList is arrayList then you can get the name atleast like
"hello"+arrayList.get(0); // will retrive first element
Or best way use cursor and fire a query which will store output of following in curser
select Name,Days from your_table where Name='xyz';
then you can use folloing code to retrive data form curser (I am assuming there is only one row in cursor here)
if(cursor.moveToFirst()){
String s = "hello"+cursor.getString(0)+"Its been"+cursor.getString(1)+"days since your last visit!!";
}
now set this s in your textbox.
I have a working android app using TextView, some formatting (line breaks, rows of dashes) and Linkify to generate a primitive "ListView-like" display with clickable URLs in each "row". I'd like to move up to a real ListView, but I'm just not finding the sample/explanation that I need to take that next step.
I have successfully reproduced the HelloListView sample, starting with the hardcoded string array, and moving to a string array defined in my res/values/strings.xml. I've taken one small step toward my goal by adding my HttpClient code to retrieve a set of data from a service, parse the results into a String Array and feed that into setListAdapter() such that my text and links show up as text-only in ListView items.
I want to move to the next step which is to make each "row" in my ListView launch the browser to the URL contained in the data, either by
(A) clicking anywhere in the row, or
(B) clicking a hyperlink displayed within the row data
For option (A), it appears that I need to have my onItemClick() method issue an intent that launches the browser. That's straightforward, but I don't get how to associate the URL with the item (currently its just one part of the string content for each "row" of text). How do I separate my URL from the rest of the text, such that I can launch a browser to the corresponding URL? Do I need to replace my String Array with an array of custom objects?
For option (B), can I use Linkify? It seems that my string array elements get converted to individual TextViews (inferring from the way the Toast text is generated in the HelloListView sample). Do I have access to that TextView to run Linkify against? Do I need to replace my String Array with a TextView Array and run Linkify myself? Am I completely off base?
Thanks to anyone who can help explain back to me what I'm trying to do, in a way that helps to find my way around the SDK, samples and other helps!
How do I separate my URL from the rest of the text, such that I can launch a browser to the corresponding URL?
Use a regular expression (java.util.regex) to find the URL.
For option (B), can I use Linkify?
Yes.
Do I have access to that TextView to run Linkify against?
Yes. Override getView() in your ArrayAdapter. Chain to the superclass and get your TextView from the result of super.getView().
Even better would be to use Linkify on your strings before putting them in the array in the first place.
Do I need to replace my String Array with a TextView Array and run Linkify myself?
No, and that is really not a good idea. Here is a free excerpt from one of my books that goes into more detail on tailoring the individual rows of a ListView, in case this helps.