Random string puller for a quotation viewer - android

so I am trying to create an area which shows quotes in my app. The quotes are saved within a string file. I am currently using a code like
public void Quotes(){
int randNum= random.nextInt((100+1)-1)+1;
String randQuote="";
switch (randNum){
case 1 :
randQuote=getString(R.string.quoteA);
}
}
}
Here I have to numerate any and all strings and represent them as cases. I have a hundred quotes.
Is there any method to complete the same task in a less redundant manner?
If yes then what method would you advise me.
Thank you in advance.

Related

String to text field in android app

I have the following data scanned from a pdf417 and need to extract certain text to certain text fields (already created), not sure how to go about this... Data scanned with manatee works plugin and android app using android studio.
All help will be appreciated.
Data that was returned from scan -
%MVL1CC18%0154%4025M003%4025012RP01C%DC62XBGP%NISSAN%SILVER/SILWER%
Each part between the %'s need to go to a text field. I know that I need to make use of String substr=mysourcestring.substring(startIndex,endIndex); but this will work up to the first 2 % signs. How do I continue to the next few?
Thanks.
If you want to split string based on a delimiter, use the following
String delimitter="%";
String[] parts = inputString.split(delimitter);
Why not use String.split()?
In your case it would look something like this:
String[] extractedStrings = mysourcestring.split("%");
You can work on your string by using split method:
String yourString = "%MVL1CC18%0154%4025M003%4025012RP01C%DC62XBGP%NISSAN%SILVER/SILWER%";
String[] split = yourString.split("%");
In this way you will get an array where each item is a substring between two % chars.

Android Search fast performance in listview

I am making a search application in android, my searching purpose is that when i type word in edittext then when the textchange, it will jump to select the position of the word that begin with that letter in listview.
This is my code. it work well but the speed of text when i type or delete in edittext seems slow, not smoothly.
What can i do to make it faster?
I have over 20,000 entries from database.
txtword.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count){
// TODO Auto-generated method stub
for(int i =0;i<list.size();i++)
{
if(list.get(i).toLowerCase().startsWith(s.toString()))
{
pos = i;
break;
}
}
lv.setSelection(pos);
}
You can use the patricia trie data structure to perform the search:
Here are the typical steps:
Populate your Strings into the patricia trie.
Perform look-up for strings starting with the entered characterd in onTextChanged().It will return you a sub-trie. For next character entered, search in that sub-trie.
When a text is removed from the editText, go back one level in the trie.
Here is the reference:
https://code.google.com/p/patricia-trie/
And, here is a sample example:
https://code.google.com/p/patricia-trie/wiki/Examples
you can user search filter in baseAdapter and arrayadapter for Listview.
use this
1)https://stackoverflow.com/a/2726348/942224
2)https://stackoverflow.com/a/14359161/942224
to get detail information.
You may consider that iterating an ArrayList with over 20000 may take a lot of resource. I think you have to reevaluate the problem and how you can solve it.
Why don't you try to research via SQL request ? You can use the "LIKE" operator.
SQLite Android Doc
Like operator example

output text line by line

I write app for Android such gets data from server in JSON format. Now I get this value in string, but in my application it must look like:
Route:
1)first point
2)secon point
3).....
n) n point
I read that in Android in textView I can do it if string will be with html tags but I think it is not the best variant. After Android I must do it in iPhone now I don't know how to do that there. Send Routes as Array is not good variant too. Can you say what is the best way to decide this problem?
Have a look here you will have to find the good pattern .
Hence you have separated strings just use a list View with an ArrayAdapter.
I am not so good with regex but i think it should like : [1-9][0-9]) [[a-f][0-9]]+
I couldn't comment b/c of rep, sorry. Could you provide an example of returned JSON string. I think JSON format can be parsed with ease.
If this the case you can parse it in a loop (or another way. I'm not that good at it)
String[] parseIt (String JSON){
String[] list=JSON.split("\\d\\)");
String[] rlist=new String[list.length-1];
for(int i=0;i<list.length-1;i++){
rlist[i]=list[i+1].trim();
}
return rlist;
}
This might do trick. But you should edit result. I didn't test yet
Edit: I edited code. It simply return the address now with leading whitespace. You can get rid off them using. String trim() method like;
list[1].trim();
Do it in loop and don't care about first element (index 0).
Edit 2: Now it should work

Why does this trigger a force close on Android?

Why does this code trigger a force close in Android?
`score.setText(Integer.parseInt((String) score.getText())+1);`
score is a TextView, and I am simply increasing the number by 1. I have predefined a String resource to be the initial number in the score TextView.
I am quite frustrated.
First off you should try breaking down your code so you can actually see what is going on with it.
Instead of
score.setText(Integer.parseInt((String) score.getText())+1);
try
String tmp = score.getText().toString();
int score;
score = Integer.parseInt(tmp) + 1;
score.setText(String.valueOf(score));
EDIT: Upon further reading of the documentation, setText has several overloads, one of which DOES take an int, but it takes the int of a resource ID. My guess is that your score is not a valid resource ID, thus crashing your application.
public final void setText (int resid)
Oh and as far as the frequent FC's when beginning Android Dev, it happens to the best of us. The key is to learn WHY the FC's happen, and have a LOT of patience.
mostly u need to do this
score.setText(Integer.parseInt(score.getText().toString())+1);
coz.. getText() returns a Editable Object which cannot be parsed to Integer. So it give NumberFormat Exception.
AndMake sure to set TextView,s Text to an integer initially..
try this way
score.setText(String.valueOf(Integer.parseInt(score.getText().toString())+1));
as you can pass the integer value that's why getting force the application
TextEdit.setText takes a CharSequence as input.
You are supplying an integer through Integer.parseInt((String) score.getText())+1
See, if converting it back to string and using it in setText helps.
You can convert an integer to string using Integer.toString.
PS: I am new to java myself.
The compiler should have ideally caught this error.
It's possible java uses some implicit type conversions from string to int.

How to get a string from string.xml in android when constructing name at run time?

In my android app, I have a number of buttons in a grid (basically a 2-D array of components). When long pressing these components I need to display a string to the user, with each array location having different strings.
For an actual app where I am using this, please see :
RIFT Assistant : https://market.android.com/details?id=com.gopalshalu.rift.assistant
In the app, start up a soul tree .
Question
Is there a way to dynamically formulate the name of string, and get the strings value.
Something like…
Int row = 0;
String target_string_name = “”;
for (int col=0;i<1;i++)
{
target_string_name = “teststring_” + row + “_” + col; // we have dynamically created the name
How do we get the actual string value here, using string name in target_string_name variable?
}
How do we get the actual string value here, using string name in target_string_name variable?
Example
String to be displayed when pressing grid location (0,0) - Hello, test string at 0,0
String to be displayed when pressing grid location (0,1) - World!.. test string at 0,1
I have a string.xml file, with the following naming convension:
<string name=’teststring_row_column’>string contents</string>
So, for the above example, the string.xml will look like:
<string name=”teststring_0_0”>Hello, test string at 0,0</string>
<string name=”teststring_0_1”>World!... test string at 0,1</string>
Thanks in advance for your time and responses.
If I understand you correctly, I believe you're looking for getIdentifier().
Here's a demonstration of its use.
I think you might consider making use of setTag() also if you want to associate a particular piece of data with a view. I also started a Rift Calculator (never finished) and for display the spell tooltips (I assume that's what you're doing?) I just associated the tooltip data with the tag for that view.

Categories

Resources