For example I have Hello123456 string and I want to show it as just Hello in a TextView.
I need those numbers in my code and I can't erase them.
EDIT:
All answer are providing a way that I erase the numbers and then set it to textview, But I want them to be there.
I get the "hello" part from user and I add the number part to it and this will be the name of a listview Item. but I want to show just the hello part in listview and also if I checked if that listview clicked item ends with "123456" it returns true.
I want to show it as just "Hello" in a TextView.
Then just put Hello in your TextView without cutting your string or create an temporary string to hold the "Hello" String.
If your string is like this "HELLOHI12345" then you need a regex to eliminate all the number string within it.
sample:
textview.setText(s.replaceAll("[0-9]+", ""));
Also take note that string are immutable so the original String wont get replaced after executing replaceAll
Use this dude ! :) You can use contextDescription to access the actual text
textView.setText(yourText.replaceAll("[0-9]",""));
textView.setContentDescription(yourText);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String str = ((TextView) v.getContentDescription()).toString();
}
});
A TextView can have a tag attached to it i.e. some object that is stored as extra data to the view itself. Check out the documentation here:
View.setTag(Object)
In your case, you can assign the "Hello" to the displayed text (using the method below), and set the tag to the full text.
textView.setText(getDisplayText(helloString));
textView.setTag("TAG", helloString);
When the user clicks on your view, you can get the tag from the view and do what you need with it.
This way, there is more data stored than what you see.
(original answer before the edit)
How about a method that does what you need:
private String getDisplayText(String input)
{
return input.substring(0, 5);
}
And then just use that method when you set the value of your TextView.
textView.setText(getDisplayText(helloString));
use \\w+ regex with Pattern and Matcher classes. This will also work if you don't know the String length. The String can be hello123 or olo123
Its not possible to hide the characters in a string. You have to solve your problem in a different way. My suggestion is to implement a separate class which contains the string:
I would write an class which contains the string. Then add two methods to this class which returns the string for display and for internal use. E.g.
class DataObject {
private String data;
public String forTextView() {
//code from one of the answers above, may be the regex version
}
public Stringg getStringWithNumbers() {
return data;
}
}
This allows you to show the string without the numbers and still have them ready when you use them.
Related
I am working on an android project where it needs to get data from database as grid view (contains multiple buttons). I have already done up to that part. Now I need to compare those data with a given string.
Here, I have tagged the status from the database to the button before put it in to the grid view.
holder.btn.setTag(data.get(position).getStatus());
the following code has shown that how I am trying to compare those values.
String x = "NA";
String y = holder.btn.getTag().toString();
if (x.equals(y)) {
holder.btn.setEnabled(false);
}
But it is not working. Please Help me to solve this issue.
Additionally, in my database there is a column call status....it contains values such as A and NA (Available and Not Available). I have already got that values from data base and set it to the item objects array list call data. in that item object i have declared field call status and then I have assigned that data base values to that.
Thanks in advance.
The method getTag() returns an Object, not a String. So you have to cast it to a String when you retrieve it. Try this:
String x = "NA";
String y = (String) holder.btn.getTag();
if (x.equals(y)) {
holder.btn.setEnabled(false);
}
Also keep in mind that if you are originally setting the Tag with something other than a String, then you'll have to convert it to a String when you use getTag(). So, for example, if your line holder.btn.setTag(data.get(position).getStatus()); sets an int as the tag, you would have to do String y = (String) Integer.toString(holder.btn.getTag()); or something similar.
Maybe the problem is at line if getStatus() returns no string
holder.btn.setTag(data.get(position).getStatus());
You can try with
holder.btn.setTag(data.get(position).getStatus().toString());
Situation: I'm working on legacy code and trying to improve readability. The following example should visualize the intent:
private static final String CONSTANT_1 = "anyValue";
private static final String CONSTANT_2 = "anyValue";
private static final String CONSTANT_3 = "anyValue";
private static final String CONSTANT_4 = "anyValue";
private static final String CONSTANT_5 = "anyValue";
private final SomeType someField = new SomeType();
private void contentOfSomeMethods(){
someMethod(someField, CONSTANT_1, true);
someMethod(someField, CONSTANT_2, true);
someMethod(someField, CONSTANT_3, true);
someMethod(someField, CONSTANT_4, false);
someMethod(someField, CONSTANT_5, false);
}
private void someMethod(SomeType type, String value, boolean someFlag) { }
Imagine, there are about 50 calls of someMethod using about 50 constants. I want to do safe automatical refactorings on that code so that the contentOfSomeMethods method changes to
private void contentOfSomeMethods(){
doItWith(CONSTANT_1);
doItWith(CONSTANT_2);
doItWith(CONSTANT_3);
doItNotWith(CONSTANT_4);
doItNotWith(CONSTANT_5);
}
and two additional methods are generated:
private void doItWith(String value) {
someMethod(someField, value, true);
}
private void doItNotWith(String value) {
someMethod(someField, value, false);
}
The naive way is to extract all constants in contentOfSomeMethods inside local variables and use then the extract method refactoring to create the desired methods. And afterwards to inline back the local variables. But this solution doesn't scale up.
Another way is to use search and replace with regular expressions, but this is not a safe refactoring, so I could break the code without noticing it.
Do you have any better suggestions? Do you know some plugins for Eclipse that allow that?
I don't know of any utility that would do this directly.
I think using a regular expression is the only to go. First, you will need to create the two target methods doItWith and doItNotWith. Then, you can highlight the contents of the method contentOfSomeMethods, hit Ctrl+F, and use the following regular expressions:
Find: someMethod\(someField, (\w*), true\);
Replace with: doItWith(\1);
and then
Find: someMethod\(someField, (\w*), false\);
Replace with: doItNotWith(\1);
Be sure to check "Regular Expressions" and "Selected lines". Here's a picture of it:
The regular expressions match the constant that is used inside the function call with (\w*) and then it is used during the replacement with \1. Using this regular expression only on the selected lines minimizes the chance of breaking unrelated code.
Do it with a regular expression and verify it.
I'm assuming that each call to someMethod spans only one line. If not this method is still useful but slower.
Copy the original file.
Use ctrl+alt+h to show the Callers of someMethod and get a count of them.
Do regex search and replaces restricted to the proper area :
Find : someMethod(someField,([ ]*CONSTANT_[0-9]+)[ ]*,[ ]*true[ ]*)[ ]*;
Replace : doItWith("$1");
Find : someMethod(someField,([ ]*CONSTANT_[0-9]+)[ ]*,[ ]*false[ ]*)[ ]*;
Replace : doItNotWith("$1");
Make a diff of the original file and the new file showing only the lines of the original file which have changed.
diff --changed-group-format='%<' --unchanged-group-format='' original.java refactored.java | wc
You should get the same number of lines as you got in the callers of someMethod.
If the calls to someMethod are multiline, or if you want greater verification, just drop | wc to see the lines which were modified in the original file to ensure that only the correct lines have been modified.
Alas I know nothing in Eclipse that allows to do this today.
This is something I would like to achieve one day in AutoRefactor: https://github.com/JnRouvignac/AutoRefactor/issues/8
However the road to get there is quite long.
The only ways I know today are to extract local variables then extract method (as you suggested) or use regexes (as somebody else suggested).
I have an array list declared like this:
val aName = new ArrayList
I add names in this array via the add().
When I print them, I only want to print specific names (e.g. all with names "Charlie" and working at the department Finance).
In my for loop, I have this:
for (m: aName.filter[!CDirectoryFacade::instance.isNameUsed(toString)])
{
print(m)
}
The loop above did not print the name at all.
Because my function isNameUsed() did not receive the string as I expect, but rather it receives the address as a String
org.generator.myDsl.myDslGenerator#67bd0a26
However, I do not seem to have problem when I do not use filter().
for (m: aName)
{
if (!CDirectoryFacade::instance.isNameUsed(m))
{
print(m)
}
}
Can anyone suggest on how to use the filter() with Strings?
It's hard to tell because your code snippet doesn't provide any types and it is not clear where name comes from in the print expression.
But the code above should probably read like this:
for (m: aName.filter[!CDirectoryFacade::instance.isNameUsed(toString)])
{
print(m.name) // instead of print(name)
}
I could be even more concise:
aName.filter[!CDirectoryFacade::instance.isNameUsed(toString)].forEach[ print(name) ]
Hello I have 3 Type of textViews in my Layout with same Dynamic text but there is only one Difference between them i.e. . Postion . I wanted to show and hide them according to Button Click . but the problem problem occurs when I have to write the same code for five Different TextViews with only one TEXT . Kindly Suggest me Efficient way which could reduce the number of line in my JAVA code.
temperature.setText(temp);
txt.setText(Name);
descptxt.setText(descp);
temperature3.setText(temp);
txt3.setText(Name);
descptxt3.setText(descp);
temperature4.setText(temp);
txt4.setText(Name);
descptxt4.setText(descp);
the number of TextViews will be increase in Future . I am worried about writing same Boiler Plate Code again and Agian
create a function like this:
void setText(String name,String temp,String descp){
for(int i=0;i<txt.size();i++){
temperature.get(i).setText(temp);
txt.get(i).setText(Name);
descptxt.get(i).setText(descp);
}
}
Also instead of creating different variables for each textview create an array of it.
ArrayList<TextView> temperature=new ArrayList<TextView>();
temperature.add((TextView)findViewById(<id>));
//Same for rest of them
And when you want to change text:
setText("","","");
Store each objects in a array ie. temperature objects in a separate array. txt objects in a separate array and descp objects in a separate array.
create a method like
public void setTexts(String temp,String name,String desc){
for(TextView temperature : temperatureArray){
//Change for loop declaration according to your code
//set text for temperature
temperature.setText(temp);
}
for(TextView txt : txtArray){
//Change for loop declaration according to your code
//set text for txt
txt.setText(name);
}
for(TextView descp : descpArray){
//Change for loop declaration according to your code
//set text for descp
descp.setText(desc);
}
}
If your TextViews are logically grouped together, you might want to create one compound View containing all of them:
public class TemperatureView extends ViewGroup { //or whichever layout you use
private TextView textView1;
private TextView textView2;
private TextView textView3;
public void setData(TemperatureData data) {
textView1.setText(...);
textView2.setText(...);
textView3.setText(...);
}
}
In my opinion, this is the most elegant solution in such situation.
You can use alternative in which u can apply loop over childcount of parent layout instead of finding using findViewById.and then using instance of operator ,you can find textviews on which u want to perform the task.
I have a weird problem that I am trying to solve,
I have a enum with a string something like this
public enum Enumm{
Item1("i1"),
Item2("i2");
private String itemText;
private Enumm(String inText){
this.setitemText(inText);
}
// getter and setter for itemText not typing
}
now in the other class I want to check if a string inputString contains either "i1" or "i2" but the problem is I cannot use a loop.
I know I can do this
for(Enumm item: Enumm.values()){
if (inputString.contains(item.getItemText)) {
// do something
}
}
but the constraints of my problem do not allow me to do it this way
I am looking for something like
if(inputString.contains(Enumm.Item1.getItemText||Enumm.Item2.getItemText)){
//do something
}
but is also dynamic such that it finds all the items in the enum
could anyone help me find a solution ?? Thanks in advance.
You could add a static method to your enum called something like findInString(String s) that does the loops over the values() for you and returns a boolean if it finds it. But it depends on why you are trying to avoid the loop. Obviously, this does nothing except squirrels the loop away where you can't see it.
You can make an Item list/array and then parse through the array to check for i1 or i2.
Item[] string = {"i1","i2"}
if(string.contains("i1")||string.contains("i2"))
{
}
Maybe this could help?
List<String> list = Collections.list(enumeration);
boolean b = list.contains("Searchstring");