setText() displaying address instead of a value in Android - android

I have just started to program in Android and I am still learning. I wanted to check if a year changes automatically when a nextMonth() method is used in case of December and January or whether I should change it with a few if statements. However, I canot display the value of that, instead I get an address. Here is my code:
TextView checkMonValue;
MonthDisplayHelper currentMonth = new MonthDisplayHelper(2012, 11);
MonthDisplayHelper nextMon = currentMonth;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkMonValue = (TextView)findViewById(R.id.monthValue);
checkMonValue.setText(String.valueOf(changeOfYear()));
}
public String changeOfYear(){
nextMon.nextMonth();
return nextMon + "" + nextMon.getYear();
}
And that is what get's displayed: Android.util.MonthDisplayHelper#44ee34e02013

Your are appending nextMon itself in your return value of changeOfYear() method. This way its returning the qualified name and address of nextMon as Android.util.MonthDisplayHelper#44ee34e0 appended with year as 2013.
Please correct to append nextMon.getMonth() and nextMon.getYear()
public String changeOfYear(){
nextMon.nextMonth();
return nextMon.getMonth() + "" + nextMon.getYear();
}

nextMon is an object as your return indicates. When you call nextMonth() you're issuing the command to increment the month but not actually retrieving anything.
Instead do this:
public String changeOfYear(){
nextMon.nextMonth();
return nextMon.getMonth() + " " + nextMon.getYear();
}
Note that I put a space in there where you only had "". You can even see this on your return: Android.util.MonthDisplayHelper#44ee34e02013

This is happening because nextMon is some type you defined, MonthDisplayHelper, and you haven't overridden the toString() method.
You can implement that method to return something meaningful, or, perhaps you meant to concatenate something different in this line:
return nextMon + "" + nextMon.getYear();
Probably something like this is what you want, nextMon.getMonth() or some method on nextMon.

Related

Android java compare xml string with edittext

I want to compare a xml string with a string from an edittext oder button.
first I set the text of the button:
button1.setText(getString(R.string.okey));
and now I want to check if the text from the button is the same as R.string.okey from the xml file. Like this I can leave out a new variable.
Is it possible to check if the strings are the same with something like this?
if (button1.getText().toString().equals(getString(R.string.okey))){
}
But that doesn't work for me.
Thank you in advance.
this must work, its just to simple. you must change somehow text on button or maybe getString returns different text (Locale changed?). use logging or debugger to check what is button1.getText().toString() and getString(R.string.okey) at the moment of comparison (equals call)
boolean areEqual = button1.getText().toString().equals(getString(R.string.okey));
Log.i("justChecking", "getString:" + getString(R.string.okey) +
", button1.getText:" + button1.getText().toString() +
", are equal:" + areEqual);
if (areEqual){
}
How to Write and View Logs with Logcat
Store them in variables
String a = button1.getText()+"";
String b = getString(R.string.str)+"";
if(a.equals(b)){ }

Check all TextViews in an Activity

I was wondering if it is possible to run a check against all the TextViews in an Activity/View and make ones which don't have any content in them invisible? I don't really want to wrap a if statement around each of the TextViews when setting text.
I currently have an activity that will populate a range of TextViews with content from a Database. However there is a chance that some fields in the database will not have any content and to keep a clean UI I want to simply make these fields invisible instead of them just sitting blank.
Here is where I am setting the content:
public void settingContent() {
eventTitle.setText(event.EventTitle);
eventLocation.setText(event.SiteName);
organiser.setText(event.Organiser);
eventType.setText(event.setEventTitle());
DateFormatter dateFormatter = new DateFormatter();
String startDate = dateFormatter.getFormattedDate(event.StartDateTime);
String endDate = dateFormatter.getFormattedDate(event.CloseDateTime);
String eventDates = startDate + " - " + endDate;
dates.setText(eventDates);
cost.setText(event.FeeDetails);
bookingContact.setText(event.BookingContactName);
stewardContact.setText(event.StewardContactName);
unitTypes.setText(event.MaxUnits);
mapReference.setText(event.MapReference);
eventNumber.setText(event.EventNumber);
otherInformation.setText(event.OtherInformation);
directions.setText(event.SiteRouting);
if(event.isTempHoliday()) {
eventIcon.setImageResource(R.drawable.icon_holidaysites_card);
} else {
eventIcon.setImageResource(R.drawable.icon_clubmeets_card);
}
}
So is this possible or is it a case of just having to check each individual TextView before setting?
List<TextView> lstTexts = new ArrayList<TextView>();
lstTexts.add(cost);
lstTexts.add(bookingContact);
lstTexts.add(stewardContact);
lstTexts.add(unitTypes);
lstTexts.add(mapReference);
lstTexts.add(eventNumber);
lstTexts.add(otherInformation);
lstTexts.add(directions);
//then you can run this method
boolean empty = checkAllTV(lstTexts);
This is the method for it.
public boolean checkAllTV(List<TextView> allTV){
for(TextView tv : allTV){
if(tv.getText().toString().equals("")||tv.getText().toString()==null)
return false;
}
return true;
}
I am on Mobile so I am not able to check the code! but you can get the idea.
Hope it Helps.

android: can I find view by its text?

In my program I have dynamic buttons each representing a letter and I need to find a button that
has a specific letter. Since the buttons' text change from time to time, I cannot use findViewById method, instead i need a way of finding view by its text. Is there one?
If not, suppose the button that has the letter I am searching for has id = B12 and I can get the number 12 in my program. How do I convert number 12 into R.id.B12 ?
I think you are looking for "find resource by name" functionality
String mButtonName = "button" + 12;
int resID = getResources().getIdentifier(mButtonName , "id", getPackageName());
That's the way you get in int(id) from string, but you can do the same using reflection which is supposed to be way faster.
public static int getId(String resourceName, Class<?> c) {
try {
Field idField = c.getDeclaredField(resourceName);
return idField.getInt(idField);
} catch (Exception e) {
throw new RuntimeException("No resource ID found for: "
+ variableName + " / " + c, e);
}
}
And you use it like this:
getId("button" + 12, R.id.class);
Hope it Helps!
Regards!

accepting value in edittext even if it has spaces

i have this code in my app. which accepts the value of the edittext
if (ans.equalsIgnoreCase("bag")) {
btnEnter.setEnabled(false);
int a=Integer.parseInt(textView2.getText().toString());
int b=a+10;
String s1 = String.valueOf(b);
textView2.setText(s1);
Toast.makeText(getApplicationContext(), "Correct",Toast.LENGTH_SHORT).show();
my problem is if the user puts a single space in the edittext then proceeds with the "bag" it still prompts wrong like this for example
" " = space
" " bag ----- wrong
bag ----- correct
how can i set that with space it can accept
String ans2 = ans.trim();
if (ans2.equalsIgnoreCase("bag")) {
btnEnter.setEnabled(false);
int a=Integer.parseInt(textView2.getText().toString());
int b=a+10;
String s1 = String.valueOf(b);
textView2.setText(s1);
Toast.makeText(getApplicationContext(), "Correct",Toast.LENGTH_SHORT).show();
trim() function
Returns a copy of the string, with leading and trailing whitespace
omitted.
Since #ρяσѕρєя K deleted his answer before I got back to delete mine I will add his simplified edit. Change
if (ans2.equalsIgnoreCase("bag")) {
to
if (ans.trim().equalsIgnoreCase("bag")) {
then no need for
String ans2 = ans.trim();
But using a second variable may be better for readibility or functionality in certain situations
Edit
To take care of in between spaces you might try
if (!ans.contains("") && ans.trim().equalsIgnoreCase("bag")) {
Not sure why that doesn't work but you can use the replace function for Strings
String ans2 = ans.replace(" ", "");
if (ans2.trim().equalsIgnoreCase("bag")) {

Program seems to be ignoring lines of code

I need help with this function.
I know that the if statement recognizes my input because it affects the program elsewhere, but I'm not sure what's going on because this particular Log doesn't display anything even in adb logcat.
Other Log statements in the same class file that this function is from display just fine, and the value update does seem to be changing ("show all" blanks it for some reason but I can figure that out after I get the log to work.)
I am unsure how to search for this problem because it is very specific and I have no idea what causes it (probably something simple that I didn't think of, though.)
void command(String input)
{
//do stuff here
//update = whatever
if(input.equalsIgnoreCase("show all"))
{
update=printAllRooms();
Log.i(input, update);
}
else update=input; //just for testing, will delete later
}
the printAllRooms function:
public String printAllRooms() //for debug purposes
{
String result = "";
for (Iterator<Room> iterator = rooms.iterator(); iterator.hasNext();) {
Room current = iterator.next();
result = result + current.toString()+"\n";
Log.i("printallrooms", current.toString());
}
return result;
}
A note on using Log.
The first argument sent to Log is typically a fixed string indicating the name of the class you are in.
So at the top of your class you might define:
private static final String TAG = "MyClassName";
Then you would use TAG for your log statements in that class.
Log.i(TAG, "My input was: " + input + " Update was: " + update;
To put it mildly, your function looks quite odd. Set a breakpoint at your Log statement, run the debugger and then inspect the variable value contained in update. Most likely, printAllRooms() is not doing what you think.
If the iterator doesn't work for you, try using the For-Each loop:
for (Room r : rooms) {
result = result + r.toString()+"\n";
Log.i("printallrooms", r.toString());
}

Categories

Resources