I am a basic beginner at android studio and I am developing a weather app. I want to put conditionals for icon images to weather condition.
For example:
sun = sun image
rain = rain image
snow = snow image
I understand a switch case might be useful:
switch (id) {
case "Raining":
icon = "rainIcon";
break;
case "Snowing":
icon = "snowIcon";
break;
case "Drizzling":
icon = "drizzlingIcon";
break;
case "Foggy":
icon = "fogIcon";
break;
case "Thunderstorm":
icon = "thunderIcon;";
break;
case "Sunny":
icon = "sunIcon";
break;
}
However, I am confused how to implement it into my code as I am retrieving my data from my AsyncTask and need to implement the image change in that class but there is no proper method to change my ImageView variable there.
I understand there are ways to implement this on the onCreateView method but this is the way my code is implemented right now
I suggest to create an enum of type Weathers.
public enum Weathers {
RAINING(R.drawable.ic_raining),
SNOWING(R.drawable.ic_snowing);
private int iconResId;
private Weathers(int iconResId) {
this.iconResId = iconResId;
}
public int getIconResId() {
return iconResId;
}
}
This might lessen the conditionals (The switch case above). To display into imageview, get an instance of the Weather instance by using Weathers.valuesOf(String) where String is upper-cased.
Weathers snowing = Weathers.valueOf("Snowing".toUpperCase(Locale.ENGLISH));
imageViewIcon.setImageResource(snowing.getIconResId());
Related
So I have a POJO with a question, and in my recycler I wanna show a question itself with answers and correct answer should have the background color.
When I try to add background color, items get colored based on some serious magic.
First few items do not get any color, and closer to the bottom of the recycler here is what happens.
enter image description here
The further the better, when I scroll from the bottom to the top, items at the top get the color as well.
public void bind(Question question) {
int correctAnswerNumber = question.getCorrectAnswer();
switch (correctAnswerNumber) {
case 1:
answer1.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
case 2:
answer2.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
case 3:
answer3.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
case 4:
answer4.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
}
}
The context here is passed from the activity, and I have to ideas what to do.
I tried to remove the logic to the constructor, change the context, address items through holder, didn't change much.
You have a classic case of "stale state from a recycled view item".
Your Problem
You only set the background for a correct answer. When that item view is recycled and used for an incorrect answer, you are not updating it and it keeps the old background that was set on it.
The Solution
Always explicitly set the full state of a recycler item view. In this case, set the background for whatever it should be when it's not a correct answer.
public void bind(Question question) {
int correctAnswerNumber = question.getCorrectAnswer();
// Reset all backgrounds to default before setting the current correct one
answer1.setBackground(getDefaultBackgroundBorder());
answer2.setBackground(getDefaultBackgroundBorder());
answer3.setBackground(getDefaultBackgroundBorder());
answer4.setBackground(getDefaultBackgroundBorder());
switch (correctAnswerNumber) {
case 1:
answer1.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
case 2:
answer2.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
case 3:
answer3.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
case 4:
answer4.setBackground(AppCompatResources.getDrawable(context, R.drawable.correct_answer_border));
break;
}
}
the activity parts.java has the following arraylist within that i wish to update in the activity loot.java
public void inventory(){
ArrayList<ImageView> inv = new ArrayList<ImageView>();
}
loot.java has the following information within the class
int commonvalue = random.nextInt(6);
switch(commonvalue){
case 1:
Intent i = new Intent(this, parts.class);
startActivityForResult(i, 100);
case 2:
case 3:
case 4:
case 5:
}
i wish to append a different item to the array within the parts class based on the result from the random in the loot class. There may be several mistakes in my code so i apologise as i am very new to android studio but your help is much appriciated.
I managed to change the color of a certain text with a spinner. But now I wanted to change the size of the same text with another spinner. I put the color cases inside a switch function to change the color.
like that
switch (i) {
case 0:
description.setText(des[i]);
preferences.edit().putInt(SELECTED_COLOR, Color.WHITE).apply();
break;
case 1:
description.setText(des[i]);
preferences.edit().putInt(SELECTED_COLOR, Color.BLUE).apply();
break;
I wrote all down but I dont know how to call the TextSize inside the case.
I thought it would work like that:
switch (d) {
case 0:
description2.setText(des2[d]);
preferences2.edit().putInt(SELECTED_SIZE, ????); <==
}
}
But I cant use TextSize or something like that.
To change the color I used Color.BLUE/RED/GREEN/... but now I want to change the TextSize... It is as always kind of difficult to explain my problem^^ sry for that.
If you want to use shared preferences and a switch statement like you do for your colors, you can do something similar to the following:
In your switch statement, similar to how you are handling color, add the text size associated with the selected spinner index to your shared preferences:
switch(i) {
case 0:
preferences.edit().putInt(SELECTED_SIZE, 16).apply();
break;
case 1:
preferences.edit().putInt(SELECTED_SIZE, 18).apply();
break;
// other cases go here...
}
Then, in the activity that includes the TextView whose text size you want to change (in this example, named textViewToChange), retrieve the saved value, and use it to set the text size:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
int selectedTextSize = preferences.getInt(SELECTED_SIZE, 0);
textViewToChange.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectedTextSize);
I am new to android and trying to develop a game. I am trying to replace images in my ImageView on recursive function call after some delay. The problem I am facing is that if once an image is set to the ImageView it is not changing though the function is being called every time.
I have used both setImageResource() and setImageDrawable() but still could not achieve desired result.
Can someone help me?
The function I am using to do this is:
type and laneNumber are passed from the calling function play()
int images[] = {R.drawable.blue, R.drawable.bluesq, R.drawable.red, R.drawable.redsq};
void play()
{
type = random.nextInt(2);
laneNumber = random.nextInt(4);
createBlock(type,laneNumber);
if(--count>0)
{
play();
}
}
void createBlock(int type, int laneNumber) {
switch (laneNumber)
{
case 0:
selector = 0;
objectLane1.setImageResource(images[type+selector]);
animateDown(objectLane1);
break;
case 1:
selector = 0;
objectLane1.setImageResource(images[type+selector]);
animateDown(objectLane1);
break;
case 2:
selector =2;
objectLane1.setImageResource(images[type+selector]);
animateDown(objectLane1);
break;
case 3:
selector = 2;
objectLane1.setImageResource(images[type+selector]);
animateDown(objectLane1);
break;
}
}
Since there is no waiting period between recursive calls, there is no way to see ImageView change, even if it does occur.
Solution:
Wait for the animation to finish by using AnimationListener. This way next recursive will not execute before animation finishes.
Hi I'm trying to write a grid filter in Vaadin framework. My aim is that a user has a combo box with dropdown list and can choose that he wants to see only entries from 3, 6, 9 weeks ago. I wrote it but I have a problem becasue it doesnt work as default filter. I mean if I open a page the grid is displayed without filter. I need first click on the button. Is it possible to filter grid by default, showing only 3 weeks entries?
How can I manage it?
final ComboBox<Integer> timePeriodSelector = new ComboBox<>("Time Filter", Arrays.asList(3, 6, 9);
timePeriodSelector.setEmptySelectionAllowed(false);
timePeriodSelector.setTextInputAllowed(false);
timePeriodSelector.setValue(3);
timePeriodSelector.addValueChangeListener(this::onWeekChange);
...
private void onWeekChange(HasValue.ValueChangeEvent<Integer> event) {
switch (event.getValue()) {
case 3:
dateToCompare = LocalDate.now().minusDays(21);
break;
case 6:
dateToCompare = LocalDate.now().minusDays(42);
break;
case 9:
dateToCompare = LocalDate.now().minusDays(63);
break;
default:
dateToCompare = LocalDate.now().plusDays(21);
}
ListDataProvider<CcEvent> dataProvider = (ListDataProvider<CcEvent>) eventsOverview.getDataProvider();
dataProvider.setFilter(CcEvent::getReportingDate,
date -> compareDates(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(), dateToCompare));
}
private Boolean compareDates(LocalDate dateFromTable, LocalDate dateFromFilter) {
return dateFromTable.isBefore(dateFromFilter);
}
that piece of code looks nice and is how the Grid filter is intented.
However all you have to do is switch your setValue call with your addValueChangeListener call.
Setting the listener before setting the value ensures that the ValueChangedEvent is triggered!