how to save String values as one array in android - java

I am getting the keyss from the firebase database as :
promoCodeKey = history.getKey();
it is giving me output as T20 Test10 etc as string
I want to save it in an array to check from the editText if the user enters any text and if the text matches any of the key
How can i achieve this?
I want to check from each key.

You could use String split function.
String keysString = history.getKey();
//removes all "Key promo is" text
String replaceString=keysString.replace("Key promo is","");
// removes all white spaces
String trimString = replaceString.replaceAll("\\s+","");
String[] keys = trimString.split(".");
for (int i = 0; i < keys.lenght; i++) {
if (keys[i] == editText.getText()) {
return true;
}
}

Related

How to get values from string in java?

My string: result = order=7781&state=1&value=&add=114GH;
I want to get values:
String order = "7781";
String state = "1";
String value = "";
String add = "114GH";
Please help me to solve this problem! Thanks!
I have done till here, but its not working.
String[] resDetails = result.split("&");
for(String pair : resDetails)
{
String[] entry = pair.split("=");
map.put(entry[0].trim(), entry[1].trim());
}
Its giving error because value is empty
Change split("=") to split("=", -1), so trailing empty strings will not be discarded.

Remove words in a string array until it becomes empty

I have "help","me","please" in a String array and I want to remove "help" from the array, i.e. after returning it as a string, remove "me" after returning it as a string, then remove the last one too, thereby making the string array empty.
I used this
public String showCurrentString(){
EditText ed = (EditText)findViewById(R.id.ed);
String edText = ed.getText().toString();
String nn =edText;
String[] blure = nn.split(" ");
int Index = 1;
for(String check : blure){
if(Arrays.asList(blure).contains(edText)){
PlaySound(StringName() + ".mp3");
Toast.makeText(getApplicationContext(),check,Toast.LENGTH_LONG).show();
But I don't know how to delete each word after toasting it.
So my main question is on how to delete a words that have been parsed or (that I have already used) from a string array.
I think you could delete the first word by doing :
nn = nn.substring(nn.indexOf(' '), nn.length);
(You can do it multiple time, just check that nn.trim().length != 0)
Suppose you have following values:
edText = "Help Me Please"
blure[0] = "Help"
blure[1] = "Me"
blure[2] = "Please"
Then this code should work fine for you:
public String showCurrentString(){
EditText ed = (EditText)findViewById(R.id.ed);
String edText = ed.getText().toString();
String nn =edText;
List<String> blure = new LinkedList<String>(Arrays.asList(nn.split("
")));
ListIterator<String> iter = blure.listIterator();
while(iter.hasNext())
{
String itemToBeRemoved = iter.next();
iter.remove();
System.out.println("Removed "+itemToBeRemoved+"!");
}
}

How can i get the last inserted record in Android?

I have created an activity in which i have a textView. In this textView i append some records from a mysql database. I want to set a different color, to the last inserted record, not to the last record from the JSONArray. How can i achieve that?
This is my code:
JSONArray users = response.getJSONArray("statistics");
for (int i = 0; i < users.length(); i++) {
JSONObject student = users.getJSONObject(i);
String userName = student.getString("username");
String score = student.getString("score");
statistics.append(userName + " " + score + " \n");
}
Thanks!
Get the last record from database and before appending that record to textview, do following to change color of text.
Spannable word = new SpannableString("record to be inserted");
word.setSpan(new ForegroundColorSpan(Color.BLUE), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
statistics.append(word);

Comparing text extracted from two different loops in selenium and assert whether they are equal or not

Comparing text extracted from two different loops in selenium and assert whether they are equal or not. Following is the selenium code, I need to compare two strings as project_text and actualVal:
driver.findElement(By.xpath(OR.getProperty("projects"))).click();
Thread.sleep(5000);
System.out.println("Selecting from list");
Select project_dropdown = new Select(driver.findElement(By.xpath(OR.getProperty("client"))));
project_dropdown.selectByVisibleText("Marketo");
System.out.println("Verifying the selection of the list");
String part1=OR.getProperty("project_start");
String part2=OR.getProperty("project_end");
String assign_part1=OR.getProperty("assign_users_start");
String assign_part2=OR.getProperty("assign_users_end");
int i=1;
String project_text = null;
String assign_text = null;
while(isElementPresent(part1+i+part2)){
project_text = driver.findElement(By.xpath(part1+i+part2)).getText();
assign_text = driver.findElement(By.xpath(assign_part1+i+assign_part2)).getText();
if (assign_text.contains("aaaa"))
System.out.println(project_text);
i++;
}
System.out.println("Project_text = " + project_text);
driver.findElement(By.xpath(OR.getProperty("calender"))).click();
Thread.sleep(5000);
driver.findElement(By.xpath(OR.getProperty("date_link"))).click();
Thread.sleep(5000);
Select project_dropdown1 = new Select(driver.findElement(By.xpath(OR.getProperty("client_select"))));
project_dropdown1.selectByVisibleText("Marketo");
WebElement project_droplist= driver.findElement(By.xpath(OR.getProperty("project_select")));
List<WebElement> droplist_cotents = project_droplist.findElements(By.tagName("option"));
System.out.println(droplist_cotents.size());
//System.out.println(droplist_cotents.get(3).getText());
String actualVal=null;
for(int j=0;j<droplist_cotents.size();j++){
actualVal = droplist_cotents.get(j).getText();
System.out.println(actualVal);
}
System.out.println("actualVal = " + actualVal);
Assert.assertEquals(project_text, actualVal);
As i can see from your code, your project_text and actualVal are in the form of strings. The best way to compare values in them is to store them as string array's as you are looping through many values and you need to store them somewhere to assert. Here's how to store the values in an array and compare them -
int i = 0;
String[] project_text = new String[100];
String[] actualVal = new String[100];
while(isElementPresent(part1+i+part2)){
project_text[i] = driver.findElement(By.xpath(part1+i+part2)).getText();
assign_text = driver.findElement(By.xpath(assign_part1+i+assign_part2)).getText();
if (assign_text.contains("aaaa"))
System.out.println(project_text[i]);
i++;
}
for(int j=0;j<droplist_cotents.size();j++){
actualVal[j] = droplist_cotents.get(j).getText();
System.out.println(actualVal[j]);
}
for(int i = 0;i<project_text.length();i++)
Assert.assertEquals(project_text[i], actualVal[i]);
However if you don't know the size of the array, use ArrayList and work it out. Here's a sample.
Hope this helps.

How to retain multiple occurrences of same string get selected within same styled text content?

How to retain multiple occurrences of same string get selected within same styled text content? Single occurrence can be selected using setSelection(). Is there any similar options?
Use StyleRange to set multiple occurrences of the string.
Snippet:
String searchKey = "hello";
String content = styledText.getText(); // StyledText instance
int index = content.indexOf(searchKey, 0);
do {
if (index != -1) {
StyleRange styleRange = new StyleRange(index, searchKey.length(), Display.getCurrent().getSystemColor(
SWT.COLOR_BLACK), Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW));
styledText.setStyleRange(styleRange);
index = content.indexOf(searchKey, index + 1);
} else {
System.out.println("End of search");
break;
}
} while (index != -1);
Refer this article an examples here on style ranges.

Categories