Hey guys so I have 3 imageButton icons/tags I want to set. So I do a loop and go through them. Now the user can press a number of tags (such as a food tag, retail tag, housing tag, etc) and that adds to a Global ArrayList. Now if the user only pressed 1 tag and there's three imageButtons that need to be set, I want to set the first imagebutton to the only tag they picked and set the rest to a white blank image, but I keep getting the following error:
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
here is my loop I dunno what I'm doing wrong here.
private void getIcons()
{
iconArray.add(icon1);
iconArray.add(icon2);
iconArray.add(icon3);
for(int i = 0; i < iconArray.size(); i++)
{
ImageView button= iconArray.get(i);
if(Global_Class.getInstance().getValue().tags.size() == 1)
{
if(Global_Class.getInstance().getValue().tags.get(i) == null)//Here is where Its giving me an ERROR!!!!!
{
button.setImageResource(R.drawable.icon_blank);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "food")
{
button.setImageResource(R.drawable.white_small_icon_food);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "bar")
{
button.setImageResource(R.drawable.white_small_icon_bar);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "club")
{
button.setImageResource(R.drawable.white_small_icon_club1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "housing")
{
button.setImageResource(R.drawable.white_small_icon_housing);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "sports")
{
button.setImageResource(R.drawable.white_small_icon_sports);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "retail")
{
button.setImageResource(R.drawable.white_small_icon_retail);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "outdoors")
{
button.setImageResource(R.drawable.white_small_icon_outdoors1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "education")
{
button.setImageResource(R.drawable.white_icon_education);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "landmark")
{
button.setImageResource(R.drawable.white_small_icon_landmark);
}
}
else if(Global_Class.getInstance().getValue().tags.size() == 2)
{
if(Global_Class.getInstance().getValue().tags.get(i) == null)
{
button.setImageResource(R.drawable.icon_blank);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "food")
{
button.setImageResource(R.drawable.white_small_icon_food);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "bar")
{
button.setImageResource(R.drawable.white_small_icon_bar);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "club")
{
button.setImageResource(R.drawable.white_small_icon_club1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "housing")
{
button.setImageResource(R.drawable.white_small_icon_housing);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "sports")
{
button.setImageResource(R.drawable.white_small_icon_sports);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "retail")
{
button.setImageResource(R.drawable.white_small_icon_retail);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "outdoors")
{
button.setImageResource(R.drawable.white_small_icon_outdoors1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "education")
{
button.setImageResource(R.drawable.white_icon_education);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "landmark")
{
button.setImageResource(R.drawable.white_small_icon_landmark);
}
else
{
//
}
}
else
{
if(Global_Class.getInstance().getValue().tags.get(i) == "food")
{
button.setImageResource(R.drawable.white_small_icon_food);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "bar")
{
button.setImageResource(R.drawable.white_small_icon_bar);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "club")
{
button.setImageResource(R.drawable.white_small_icon_club1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "housing")
{
button.setImageResource(R.drawable.white_small_icon_housing);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "sports")
{
button.setImageResource(R.drawable.white_small_icon_sports);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "retail")
{
button.setImageResource(R.drawable.white_small_icon_retail);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "outdoors")
{
button.setImageResource(R.drawable.white_small_icon_outdoors1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "education")
{
button.setImageResource(R.drawable.white_icon_education);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "landmark")
{
button.setImageResource(R.drawable.white_small_icon_landmark);
}
else
{
//
}
}
}
}
Here is where i make tags arraylist:
public class GlobalVariables
{
public ArrayList<String> tags = new ArrayList<>();
}
And I add to it in another class:
public void onClick(View v)
{
switch (v.getId())
{
case R.id.imageButton:
if(food_pressed)
{
ImageButton food_button = (ImageButton) findViewById(R.id.imageButton);
food_button.setImageResource(R.drawable.pressed_food);
tags.add("food");
food_pressed = false;
break;
}
else
{
ImageButton food_button = (ImageButton) findViewById(R.id.imageButton);
food_button.setImageResource(R.drawable.icon_food);
tags.remove("food");
food_pressed = true;
break;
}
case R.id.imageButton9:
ImageButton done_button = (ImageButton) findViewById(R.id.imageButton9);
done_button.setImageResource(R.drawable.pressed_done);
Global_Class.getInstance().getValue().tags = tags;
//Toast.makeText(getApplicationContext(),Global_Class.getInstance().getValue().tags.toString(),Toast.LENGTH_SHORT).show();
startActivity(toDescription);
break;
etc...
}
Related
What should the return value at the bottom be? By setting null I can make my app work. However, I would like to get rid of the warning in Android Studio without suppressing it.
Android Studio is obviously telling me "'null' is returned by the method declared as #NonNull".
Is there a better way to return my fragments?
#NonNull
#Override
public Fragment createFragment(int position) {
String className = mContext.getClass().getSimpleName();
// Check which Activity is using the ViewHolder and return the appropriate Fragments.
switch (className) {
case "VancouverActivity":
if (position == 0) {
return new VancouverMainFragment();
} else if (position == 1) {
return new VancouverAttractionsFragment();
} else if (position == 2) {
return new VancouverCoffeeFragment();
} else if (position == 3) {
return new VancouverRestaurantFragment();
} else {
return new VancouverActivitiesFragment();
}
case "SaskatoonActivity":
if (position == 0) {
return new SaskatoonMainFragment();
} else if (position == 1) {
return new SaskatoonAttractionsFragment();
} else if (position == 2) {
return new SaskatoonCoffeeFragment();
} else if (position == 3) {
return new SaskatoonRestaurantFragment();
} else {
return new SaskatoonActivitiesFragment();
}
case "TorontoActivity":
if (position == 0) {
return new TorontoMainFragment();
} else if (position == 1) {
return new TorontoAttractionsFragment();
} else if (position == 2) {
return new TorontoCoffeeFragment();
} else if (position == 3) {
return new TorontoRestaurantFragment();
} else {
return new TorontoActivitiesFragment();
}
case "StJohnsActivity":
if (position == 0) {
return new StJohnsMainFragment();
} else if (position == 1) {
return new StJohnsAttractionsFragment();
} else if (position == 2) {
return new StJohnsCoffeeFragment();
} else if (position == 3) {
return new StJohnsRestaurantFragment();
} else {
return new StJohnsActivitiesFragment();
}
}
return null;
}
I tried code from JFreeChart - horizontal stacked bar chart with date axis
private CategoryDataset createDataset() {
.....
// if data event time is in the range of the chart then show it
// THIS DOES NOT WORK PROPERLY!!!!
if (eventTime >= chartStartDate.getTime() && eventTime < chartEndDate.getTime()) {
// create series and categories
if (es.getStatus() == STANDBY_SERIES_INDEX) {
dataset.addValue(new Double(es.getTime()), STANDBY_SERIES, es.getName());
} else if (es.getStatus() == HEATING_SERIES_INDEX) {
dataset.addValue(new Double(es.getTime()), HEATING_SERIES, es.getName());
} else if (es.getStatus() == HOLDING_SERIES_INDEX) {
dataset.addValue(new Double(es.getTime()), HOLDING_SERIES, es.getName());
} else if (es.getStatus() == COOLING_SERIES_INDEX) {
dataset.addValue(new Double(es.getTime()), COOLING_SERIES, es.getName());
} else if (es.getStatus() == LOWERING_SERIES_INDEX) {
dataset.addValue(new Double(es.getTime()), LOWERING_SERIES, es.getName());
} else {
dataset.addValue(chartRange.getUpperBound() - chartRange.getLowerBound(), STANDBY_SERIES, es.getName());
}
} else {
continue;
}
}
}
}
} else {
plot.setNoDataMessage("NO DATA AVAILABLE");
}
return dataset;
}
But These range time data is incorrectly rendered
Please help me how to get it
Currently, I am making an android app that is going to be a very simple memory game where 1 random button is going to be highlighted, then the user must click the button that was highlighted after the button goes back to normal. If the user gets the button correct the original button that was highlighted the first time will light up, then another random button will light up after just like the first time and they have to click them in order. For further clarification if your unsure its kind of like Simon (The game).
Currently the game is just going to the next random button instead of repeating AND THEN going to a new one and i'm unsure how to change that. Any help would be greatly appreciated!
package com.MakeItMobile.fixmymemory;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import android.R.drawable;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainScreen extends Activity implements OnClickListener {
Button buttonRed, buttonYellow, buttonOrange, buttonBlack, buttonGreen,
buttonPurple, buttonPink, buttonLime, buttonDarkBlue;
Random randNumber;
List<Integer> whichButton;
int userInput[] = {};
int counter = 0;
int compareCounter = 0;
int n = 0;
Boolean yourTurn = false;
Boolean aiTurn = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
//Getting the buttons
buttonRed = (Button) findViewById(R.id.buttonRed);
buttonYellow = (Button) findViewById(R.id.buttonYellow);
buttonOrange = (Button) findViewById(R.id.buttonOrange);
buttonBlack = (Button) findViewById(R.id.buttonBlack);
buttonGreen = (Button) findViewById(R.id.buttonGreen);
buttonPurple = (Button) findViewById(R.id.buttonPurple);
buttonPink = (Button) findViewById(R.id.buttonPink);
buttonLime = (Button) findViewById(R.id.buttonLime);
buttonDarkBlue = (Button) findViewById(R.id.buttonDarkBlue);
//Setting them clickable
buttonRed.setOnClickListener(this);
buttonYellow.setOnClickListener(this);
buttonOrange.setOnClickListener(this);
buttonBlack.setOnClickListener(this);
buttonGreen.setOnClickListener(this);
buttonPurple.setOnClickListener(this);
buttonPink.setOnClickListener(this);
buttonLime.setOnClickListener(this);
buttonDarkBlue.setOnClickListener(this);
//Giving them a tag for easier comparison in onClick
buttonRed.setTag(1);
buttonYellow.setTag(2);
buttonOrange.setTag(3);
buttonBlack.setTag(4);
buttonGreen.setTag(5);
buttonPurple.setTag(6);
buttonPink.setTag(7);
buttonLime.setTag(8);
buttonDarkBlue.setTag(9);
//Showing a would you like to play dialog
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch (which) {
case (DialogInterface.BUTTON_POSITIVE):
whenStarted();
break;
case (DialogInterface.BUTTON_NEGATIVE):
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Would you like to begin?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
// Main loop
public void whenStarted() {
if (aiTurn) {
whichButton = new ArrayList<Integer>();
repeatBack();
randomNumber();
n = randomNumber();
if (n == 1) {
delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (n == 2) {
delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (n == 3) {
delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (n == 4) {
delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (n == 5) {
delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (n == 6) {
delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (n == 7) {
delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (n == 8) {
delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (n == 9) {
delay(R.drawable.buttonblueclicked, R.drawable.buttonblue);
}
whichButton.add(n);
yourTurn = true;
} else if (yourTurn) {
}
}
// Repeating back what buttons were clicked each turn
public void repeatBack() {
for (int i = 0; i < whichButton.size(); i++) {
if (whichButton.get(i) == 1) {
delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (whichButton.get(i) == 2) {
delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (whichButton.get(i) == 3) {
delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (whichButton.get(i) == 4) {
delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (whichButton.get(i) == 5) {
delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (whichButton.get(i) == 6) {
delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (whichButton.get(i) == 7) {
delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (whichButton.get(i) == 8) {
delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
} else if (whichButton.get(i) == 9) {
delayRepeat(R.drawable.buttonblueclicked, R.drawable.buttonblue);
}
System.out.println("Which button size and number is "
+ whichButton.size());
System.out.println(i);
}
}
// For repeating back the buttons
// On start this method sets the button to the color and waits 1 second
// On finish it changes back to the original image
public void delayRepeat(final int newStartID, final int endID) {
final int time = 1000;
new CountDownTimer(time, 1000) {
#Override
public void onFinish() {
// TODO Auto-generated method stub
for (int i = 0; i < whichButton.size(); i++) {
if (whichButton.get(i) == 1) {
buttonRed.setBackgroundResource(endID);
} else if (whichButton.get(i) == 2) {
buttonYellow.setBackgroundResource(endID);
} else if (whichButton.get(i) == 3) {
buttonOrange.setBackgroundResource(endID);
} else if (whichButton.get(i) == 4) {
buttonBlack.setBackgroundResource(endID);
} else if (whichButton.get(i) == 5) {
buttonGreen.setBackgroundResource(endID);
} else if (whichButton.get(i) == 6) {
buttonPurple.setBackgroundResource(endID);
} else if (whichButton.get(i) == 7) {
buttonPink.setBackgroundResource(endID);
} else if (whichButton.get(i) == 8) {
buttonLime.setBackgroundResource(endID);
} else if (whichButton.get(i) == 9) {
buttonDarkBlue.setBackgroundResource(endID);
}
}
new CountDownTimer(time, 1000) {
#Override
public void onFinish() {
for (int i = 0; i < whichButton.size(); i++) {
// TODO Auto-generated method stub
if (whichButton.get(i) == 1) {
buttonRed.setBackgroundResource(newStartID);
} else if (whichButton.get(i) == 2) {
buttonYellow.setBackgroundResource(newStartID);
} else if (whichButton.get(i) == 3) {
buttonOrange.setBackgroundResource(newStartID);
} else if (whichButton.get(i) == 4) {
buttonBlack.setBackgroundResource(newStartID);
} else if (whichButton.get(i) == 5) {
buttonGreen.setBackgroundResource(newStartID);
} else if (whichButton.get(i) == 6) {
buttonPurple.setBackgroundResource(newStartID);
} else if (whichButton.get(i) == 7) {
buttonPink.setBackgroundResource(newStartID);
} else if (whichButton.get(i) == 8) {
buttonLime.setBackgroundResource(newStartID);
} else if (whichButton.get(i) == 9) {
buttonDarkBlue
.setBackgroundResource(newStartID);
}
}
}
#Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
}.start();
}
#Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
}.start();
}
// creating a blinking color button for each specific random number
// On start this method sets the button to the color and waits 1 second
// On finish it changes back to the original image
public void delay(final int newStartID, final int endID) {
final int time = 1000;
new CountDownTimer(time, 1000) {
#Override
public void onFinish() {
// TODO Auto-generated method stub
if (n == 1) {
buttonRed.setBackgroundResource(endID);
} else if (n == 2) {
buttonYellow.setBackgroundResource(endID);
} else if (n == 3) {
buttonOrange.setBackgroundResource(endID);
} else if (n == 4) {
buttonBlack.setBackgroundResource(endID);
} else if (n == 5) {
buttonGreen.setBackgroundResource(endID);
} else if (n == 6) {
buttonPurple.setBackgroundResource(endID);
} else if (n == 7) {
buttonPink.setBackgroundResource(endID);
} else if (n == 8) {
buttonLime.setBackgroundResource(endID);
} else if (n == 9) {
buttonDarkBlue.setBackgroundResource(endID);
}
new CountDownTimer(time, 1000) {
#Override
public void onFinish() {
// TODO Auto-generated method stub
if (n == 1) {
buttonRed.setBackgroundResource(newStartID);
} else if (n == 2) {
buttonYellow.setBackgroundResource(newStartID);
} else if (n == 3) {
buttonOrange.setBackgroundResource(newStartID);
} else if (n == 4) {
buttonBlack.setBackgroundResource(newStartID);
} else if (n == 5) {
buttonGreen.setBackgroundResource(newStartID);
} else if (n == 6) {
buttonPurple.setBackgroundResource(newStartID);
} else if (n == 7) {
buttonPink.setBackgroundResource(newStartID);
} else if (n == 8) {
buttonLime.setBackgroundResource(newStartID);
} else if (n == 9) {
buttonDarkBlue.setBackgroundResource(newStartID);
}
}
#Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
}.start();
}
#Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
}.start();
}
//Used to generate a different random number each time
public int randomNumber() {
randNumber = new Random();
int n = randNumber.nextInt(9) + 1;
return n;
}
// On click for when they click each button
// Buttons are set to specific tags in the onCreate
// Checks if the tag is equal to what the output of whichButton.get(x) is
// If it isn't they fail
public void onClick(View v) {
// TODO Auto-generated method stub
int tag = (Integer) v.getTag();
for (int x = 0; x < whichButton.size(); x++) {
if (tag == 1 && tag == whichButton.get(x)) {
aiTurn = true;
whenStarted();
} else if (tag == 2 && tag == whichButton.get(x)) {
aiTurn = true;
whenStarted();
} else if (tag == 3 && tag == whichButton.get(x)) {
aiTurn = true;
whenStarted();
} else if (tag == 4 && tag == whichButton.get(x)) {
aiTurn = true;
whenStarted();
} else if (tag == 5 && tag == whichButton.get(x)) {
aiTurn = true;
whenStarted();
} else if (tag == 6 && tag == whichButton.get(x)) {
aiTurn = true;
whenStarted();
} else if (tag == 7 && tag == whichButton.get(x)) {
aiTurn = true;
whenStarted();
} else if (tag == 8 && tag == whichButton.get(x)) {
aiTurn = true;
whenStarted();
} else if (tag == 9 && tag == whichButton.get(x)) {
aiTurn = true;
whenStarted();
} else {
aiTurn = false;
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch (which) {
case (DialogInterface.BUTTON_POSITIVE):
aiTurn = true;
whenStarted();
break;
case (DialogInterface.BUTTON_NEGATIVE):
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You failed, would you like to play again?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
}
}
}
In your onClick(...) handler you're calling whenStarted() each time.
In the whenStarted() method you do this...
whichButton = new ArrayList<Integer>();
...which basically sets whichButton to a new empty ArrayList.
Move that line elsewhere in your code so it only initializes the ArrayList at the start of each game and not at every turn.
The way you are going about this is overcomplicated. Instead of using else-if blocks to pick the button from a random number, put the buttons in a list or array and get a random buttons like this:
private final Button[] buttons = new Button[] {
findViewById(R.id.buttonRed),
findViewById(R.id.buttonYellow),
/* etc, etc */
};
public Button getRandomButton() {
return buttons[new Random().nextInt(buttons.length)];
}
The reason the sequence isn't playing back correctly is because you aren't storing the sequence as an instance variable. The easiest way to store the sequence would be to store a list of integers. The integers would represent the position of the buttons in your array and you would add a new entry every turn.
private final List<Integer> currentSequence = new ArrayList<>();
public void nextTurn() {
// first play the previous buttons
for (int i : currentSequence) {
Button b = buttons[i];
// then play it here
}
// play a new button and add it to the sequence
Button randomButton = getRandomButton();
// play it here
currentSequence.add(Arrays.asList(buttons).indexOf(randomButton)));
}
Then when you're done with the current game just remember to clear the sequence with.
currentSequence.clear();
I am working on an application in which have two navigation button namely 'previous and next' which on tap load stories respectively. When i tap these buttons gently they work fine button when i tap next button to the last index continuously without any break then the previous button does not work or similarly when i start tapping previous button to the very first index i am unable to move forward.
Remember this only happens at the extreme cases, onClick is not called, don't know why.
My code is as follows, please help me out. Thanks in advance.
this the code of onClick, which works fine in all cases except the extreme cases, when buttons are not tapped gently.
public void onClick(View v) {
if (visible == true) {
Log.e("visible", "true");
return;
}
visible = true;
try {
Log.e("Now", "On Click");
pDialog = new ProgressDialog(StoriesListController.this);
pDialog.setIndeterminate(true);
pDialog.setIcon(R.drawable.icon_small);
pDialog.setCancelable(true);
if (isFavoriteList == true) {
pDialog.setMessage("Loading favorites...");
} else {
pDialog.setMessage("Loading data...");
}
pDialog.setTitle("Please wait");
pDialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface arg0) {
if (cancelableHeavyWorker != null) {
cancelableHeavyWorker.setHandler(null);
}
finish();
}
});
pDialog.show();
if (v == next) {
if (ind < items.size() - 1) {
ind++;
loadAndShowNextActivity();
}
} else if (v == previous) {
if (ind > 0) {
ind--;
loadAndShowNextActivity();
}
}
// realeaseMemoryIfneededofStory(ind);
} catch (Exception e) {
Log.e("Error", "--.OnClink Message" + e.toString());
e.printStackTrace();
}
}
Moreover, the boolean variable "visible" is being set to false in the callback function.
The Code of Call Back Function is as follows:
private void fetchTopStoryDetailCallback(Object resultVector) {
System.out.println("fetchTopStoryDetailCallback");
try {
Vector<?> v = (Vector<?>) resultVector;
boolean completedOrFailed = ((Boolean) v.elementAt(0)).booleanValue();
if (completedOrFailed == true) {
boolean slideshow = ((Boolean) v.elementAt(1)).booleanValue();
Object result = v.elementAt(2);
String res[] = (String[]) result;
if (slideshow) {
if (Utils.topStorySlidesArrayList != null && Utils.topStorySlidesArrayList.size() > 0) {
Intent articleActivityIntent = new Intent(this, SlideShowActivity.class);
articleActivityIntent.putExtra("storyData", (String[]) result);
articleActivityIntent.putExtra("back", back);
articleActivityIntent.putStringArrayListExtra("sid", slideids);
// articleActivityIntent.putExtra("contentType", )
articleActivityIntent.putExtra("addkey", ADDS_KEY);
showActivityInContoller(articleActivityIntent);
hidePrgressgingDailog();
} else {
hidePrgressgingDailog();
this.closeActivity = true;
showMessage("Error", "This story encounter an error while opening. Check your Internet Connection and try later");
}
} else {
if (res[3] != null && (!(res[3].equalsIgnoreCase("null")) && (!res[3].equals("")))) {
Intent slideshowActivtyIntent = new Intent(this, ArticleActivity.class);
slideshowActivtyIntent.putExtra("storyData", (String[]) result);
slideshowActivtyIntent.putExtra("back", back);
slideshowActivtyIntent.putExtra("addkey", ADDS_KEY);
showActivityInContoller(slideshowActivtyIntent);
hidePrgressgingDailog();
} else {
hidePrgressgingDailog();
this.closeActivity = true;
showMessage("Error", "This story encounter an error while opening. Check your Internet Connection and try later");
}
}
} else {
showMessage("Error", "This story encounter an error while opening. Check your Internet Connection and try later");
hidePrgressgingDailog();
}
} catch (Exception e) {
Log.e("StoriesController", "Message = " + e.toString());
e.printStackTrace();
}
finally {visible = false; }
}
this is how visiblity of the buttons is set...!!!!
private void adjustButtonsVisibility() {
try {
if (items.size() == 1) {
next.setEnabled(false);
next.setImageResource(R.drawable.navigator_next_disable);
previous.setEnabled(false);
previous.setImageResource(R.drawable.navigator_previous_disable);
//hidePrgressgingDailog();
return;
}
if (ind == 0) {
previous.setEnabled(false);
next.setEnabled(true);
previous.setImageResource(R.drawable.navigator_previous_disable);
next.setImageResource(R.drawable.story_next_arrow);
hidePrgressgingDailog();
} else if (ind > 0 && ind < items.size() - 1) {
previous.setEnabled(true);
next.setEnabled(true);
previous.setImageResource(R.drawable.story_previous_arrow);
next.setImageResource(R.drawable.story_next_arrow);
}
if (ind == items.size() - 1) {
previous.setEnabled(true);
next.setEnabled(false);
previous.setImageResource(R.drawable.story_previous_arrow);
next.setImageResource(R.drawable.navigator_next_disable);
hidePrgressgingDailog();
}
} catch (Exception e) {
Log.e("Error", "--,adjustButtonsVisibility Message = " + e);
e.printStackTrace();
}
}
Do this
if (ind == 0) {
previous.setEnabled(false);
previous.setImageResource(R.drawable.navigator_previous_disable);
next.setImageResource(R.drawable.story_next_arrow);
hidePrgressgingDailog();
}
else if (ind > 0 && ind < items.size() - 1) {
previous.setEnabled(true);
next.setEnabled(true);
previous.setImageResource(R.drawable.story_previous_arrow);
next.setImageResource(R.drawable.story_next_arrow);
}
else if (ind == items.size() - 1) {
previous.setEnabled(true);
next.setEnabled(false);
previous.setImageResource(R.drawable.story_previous_arrow);
next.setImageResource(R.drawable.navigator_next_disable);
hidePrgressgingDailog();
}
I am trying to set up a SAX Handler to parse XML with the following structure:
<Hours>
<Set name="BUSINESS">
<MO>
<Open>09:00:00</Open>
<Close>17:00:00</Close>
</MO>
<TU>
<Open>09:00:00</Open>
<Close>17:00:00</Close>
</TU>
<WE>
<Open>09:00:00</Open>
<Close>17:00:00</Close>
</WE>
<TH>
<Open>09:00:00</Open>
<Close>17:00:00</Close>
</TH>
<FR>
<Open>09:00:00</Open>
<Close>17:00:00</Close>
</FR>
<SA/>
<SU/>
</Set>
<Set name="LASTCOLLECTION">
<MO>
<Close>17:00:00</Close>
</MO>
<TU>
<Close>17:00:00</Close>
</TU>
<WE>
<Close>17:00:00</Close>
</WE>
<TH>
<Close>17:00:00</Close>
</TH>
<FR>
<Close>17:00:00</Close>
</FR>
<SA/>
<SU/>
</Set>
</Hours>
I am running into some problems, however. My code will only really get the value of OPEN and CLOSE in MO, not in any of the other days of the week. Can anyone find a flaw in my logic? Here is my handler code:
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equalsIgnoreCase("Set")
&& attributes.getValue("name").equalsIgnoreCase(
"BUSINESS")) {
inBusiness = true;
} else if (localName.equalsIgnoreCase("Set")
&& attributes.getValue("name").equalsIgnoreCase(
"LASTCOLLECTION")) {
inLastCollection = true;
} else if (localName.equalsIgnoreCase("MO")) {
inMonday = true;
} else if (localName.equalsIgnoreCase("TU")) {
inTuesday = true;
} else if (localName.equalsIgnoreCase("WE")) {
inWednesday = true;
} else if (localName.equalsIgnoreCase("TH")) {
inThursday = true;
} else if (localName.equalsIgnoreCase("FR")) {
inFriday = true;
} else if (localName.equalsIgnoreCase("SA")) {
inSaturday = true;
} else if (localName.equalsIgnoreCase("SU")) {
inSunday = true;
} else if (localName.equalsIgnoreCase("Open")) {
inOpen = true;
} else if (localName.equalsIgnoreCase("Close")) {
inClose = true;
}
}
public void characters(char ch[], int start, int length)
throws SAXException {
value = value + new String(ch, start, length).trim();
}
public void endElement(String uri, String name, String qName)
throws SAXException {
if (inBusiness) {
bHours = new BusinessHours();
if (inMonday) {
if (inOpen) {
bHours.setMondayOpen(value);
inOpen = false;
} else if (inClose) {
bHours.setMondayClose(value);
inClose = false;
}
inMonday = false;
} else if (inTuesday) {
if (inOpen) {
bHours.setTuesdayOpen(value);
inOpen = false;
} else if (inClose) {
bHours.setTuesdayClose(value);
inClose = false;
}
inTuesday = false;
} else if (inWednesday) {
if (inOpen) {
bHours.setWednesdayOpen(value);
inOpen = false;
} else if (inClose) {
bHours.setWednesdayClose(value);
inClose = false;
}
inWednesday = false;
} else if (inThursday) {
if (inOpen) {
bHours.setThursdayOpen(value);
inOpen = false;
} else if (inClose) {
bHours.setThursdayClose(value);
inClose = false;
}
inThursday = false;
} else if (inFriday) {
if (inOpen) {
bHours.setFridayOpen(value);
inOpen = false;
} else if (inClose) {
bHours.setFridayClose(value);
inClose = false;
}
inFriday = false;
} else if (inSaturday) {
if (inOpen) {
bHours.setSaturdayOpen(value);
inOpen = false;
} else if (inClose) {
bHours.setSaturdayClose(value);
inClose = false;
}
inSaturday = false;
} else if (inSunday) {
if (inOpen) {
bHours.setSundayOpen(value);
inOpen = false;
} else if (inClose) {
bHours.setSundayClose(value);
inClose = false;
}
inSunday = false;
}
myLoc.setBusinessHours(bHours);
inBusiness = false;
} else if (inLastCollection) {
cHours = new LastCollectionHours();
if (inMonday) {
if (inOpen) {
cHours.setMondayOpen(value);
inOpen = false;
} else if (inClose) {
cHours.setMondayClose(value);
inClose = false;
}
inMonday = false;
} else if (inTuesday) {
if (inOpen) {
cHours.setTuesdayOpen(value);
inOpen = false;
} else if (inClose) {
cHours.setTuesdayClose(value);
inClose = false;
}
inTuesday = false;
} else if (inWednesday) {
if (inOpen) {
cHours.setWednesdayOpen(value);
inOpen = false;
} else if (inClose) {
cHours.setWednesdayClose(value);
inClose = false;
}
inWednesday = false;
} else if (inThursday) {
if (inOpen) {
cHours.setThursdayOpen(value);
inOpen = false;
} else if (inClose) {
cHours.setThursdayClose(value);
inClose = false;
}
inThursday = false;
} else if (inFriday) {
if (inOpen) {
cHours.setFridayOpen(value);
inOpen = false;
} else if (inClose) {
cHours.setFridayClose(value);
inClose = false;
}
inFriday = false;
} else if (inSaturday) {
if (inOpen) {
cHours.setSaturdayOpen(value);
inOpen = false;
} else if (inClose) {
cHours.setSaturdayClose(value);
inClose = false;
}
inSaturday = false;
} else if (inSunday) {
if (inOpen) {
cHours.setSundayOpen(value);
inOpen = false;
} else if (inClose) {
cHours.setSundayClose(value);
inClose = false;
}
inSunday = false;
}
myLoc.setLastCollectionHours(cHours);
inLastCollection = false;
}
}
UPDATED CODE:
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
value = new String();
if (localName.equalsIgnoreCase("Set")
&& attributes.getValue("name").equalsIgnoreCase("BUSINESS")) {
currentElement = 1;
bHours = new BusinessHours();
} else if (localName.equalsIgnoreCase("Set")
&& attributes.getValue("name").equalsIgnoreCase(
"LASTCOLLECTION")) {
currentElement = 2;
cHours = new LastCollectionHours();
} else if (localName.equalsIgnoreCase("MO")) {
day = 1;
} else if (localName.equalsIgnoreCase("TU")) {
day = 2;
} else if (localName.equalsIgnoreCase("WE")) {
day = 3;
} else if (localName.equalsIgnoreCase("TH")) {
day = 4;
} else if (localName.equalsIgnoreCase("FR")) {
day = 5;
} else if (localName.equalsIgnoreCase("SA")) {
day = 6;
} else if (localName.equalsIgnoreCase("SU")) {
day = 7;
} else if (localName.equalsIgnoreCase("Open")) {
open = 1;
} else if (localName.equalsIgnoreCase("Close")) {
open = 2;
}
}
public void characters(char ch[], int start, int length)
throws SAXException {
value = value + new String(ch, start, length).trim();
} // end Method Characters
public void endElement(String uri, String name, String qName)
throws SAXException {
if (name.equalsIgnoreCase("Set") && currentElement == 1) {
myLoc.setBusinessHours(bHours);
} else if (name.equalsIgnoreCase("Set") && currentElement == 2) {
myLoc.setLastCollectionHours(cHours);
}
if (currentElement == 1) {
if (day == 1) {
if (open == 1) {
bHours.setMondayOpen(value);
} else if (open == 2) {
bHours.setMondayClose(value);
}
} else if (day == 2) {
if (open == 1) {
bHours.setTuesdayOpen(value);
} else if (open == 2) {
bHours.setTuesdayClose(value);
}
} else if (day == 3) {
if (open == 1) {
bHours.setWednesdayOpen(value);
} else if (open == 2) {
bHours.setWednesdayClose(value);
}
} else if (day == 4) {
if (open == 1) {
bHours.setThursdayOpen(value);
} else if (open == 2) {
bHours.setThursdayClose(value);
}
} else if (day == 5) {
if (open == 1) {
bHours.setFridayOpen(value);
} else if (open == 2) {
bHours.setFridayClose(value);
}
} else if (day == 6) {
if (open == 1) {
bHours.setSaturdayOpen(value);
} else if (open == 2) {
bHours.setSaturdayClose(value);
}
} else if (day == 7) {
if (open == 1) {
bHours.setSundayOpen(value);
} else if (open == 2) {
bHours.setSundayClose(value);
}
}
} else if (currentElement == 2) {
if (day == 1) {
if (open == 1) {
cHours.setMondayOpen(value);
} else if (open == 2) {
cHours.setMondayClose(value);
}
} else if (day == 2) {
if (open == 1) {
cHours.setTuesdayOpen(value);
} else if (open == 2) {
cHours.setTuesdayClose(value);
}
} else if (day == 3) {
if (open == 1) {
cHours.setWednesdayOpen(value);
} else if (open == 2) {
cHours.setWednesdayClose(value);
}
} else if (day == 4) {
if (open == 1) {
cHours.setThursdayOpen(value);
} else if (open == 2) {
cHours.setThursdayClose(value);
}
} else if (day == 5) {
if (open == 1) {
cHours.setFridayOpen(value);
} else if (open == 2) {
cHours.setFridayClose(value);
}
} else if (day == 6) {
if (open == 1) {
cHours.setSaturdayOpen(value);
} else if (open == 2) {
cHours.setSaturdayClose(value);
}
} else if (day == 7) {
if (open == 1) {
cHours.setSundayOpen(value);
} else if (open == 2) {
cHours.setSundayClose(value);
}
}
}
}
Thanks!
On Line 116 You are setting inBusiness = false - which will get set after monday's open/close. Next time around it fails the if(inBusiness) on line 44.
Instead of using a inBusiness boolean, use a int indicator as follows:
private int curElement = 0;
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equalsIgnoreCase("Set")
&& attributes.getValue("name").equalsIgnoreCase(
"BUSINESS")) {
curElement = 1;
} else if (localName.equalsIgnoreCase("Set")
&& attributes.getValue("name").equalsIgnoreCase(
"LASTCOLLECTION")) {
curElement = 2;
}
......
}
public void endElement(String uri, String name, String qName)
throws SAXException {
if (curElement == 1) {
....
//Remove inBusiness= false;
} else if (curElement == 2) {
...
//Remove inLastCollection = false;
}
}