Infinite loop somewhere in a CYOA game - android

Why is the code breaking?
Intro: trying to make a "Choose Your Own Adventure" game.
Layout is composed by:
bPage = button saying what page the user is on. Click to manually change page (not implemented yet)
bOne: go through first path
bTwo: go through second path
tvPageTitle: page title, at the top
mainText: main text body (story explained here)
there's also a splash screen, which works perfectly, but when you click on "start", it loads this layout, which hangs (possible infinite loop)
Can someone tell me why? Tried to put as many comments as I could, so it should be self explanatory
Please ask as many questions as you want :)
package com.assignement.cyoa;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Remember extends Activity {
// Declaring variables
TextView mainText;
int page = 1;
MediaPlayer pageflip;
Boolean gameOver = false;
int currentPage = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remember);
// Enable manual scrolling text
mainText = (TextView) findViewById(R.id.mainText);
mainText.setMovementMethod(new ScrollingMovementMethod());
// Enable sound when choosing path
pageflip = MediaPlayer.create(Remember.this, R.raw.pageflip);
//TODO: customise story to insert player name. Remember to import method
//EditText userName = LoginActivity.userName;
// Link XML Views with Java
final TextView mainText = (TextView) findViewById(R.id.mainText);
final TextView tvPageTitle = (TextView) findViewById(R.id.tvPageTitle);
final Button bOne = (Button) findViewById(R.id.bOne);
final Button bTwo = (Button) findViewById(R.id.bTwo);
final Button bPage = (Button) findViewById(R.id.bPage);
// Array creation
final String[] storyText = new String[22];
final String[] pageTitle = new String[22];
final CharSequence[] pathA = new CharSequence[22];
final CharSequence[] pathB = new CharSequence[22];
///// MAIN TEXT AND POSSIBLE PATHS ARRAYS \\\\\
// Page 0 - Error
storyText[0] = "Path broken";
pageTitle[0] = "Path broken";
pathA[0]= "Path broken";
pathB[0]= "Path broken";
// Page 1
storyText[1] = "\tYou lift open your eyes. All you can see is a bright light. You get up and take a look around you. You are in a very tight room, with no windows and one door. Everything is painted in white. Beside the door is a small walkie talkie which looks brand new.\n\tYou begin to think, where are you? Who are you? You steady yourself and stumble towards the door. You tightly grab the knob and begin to twist it open. It's locked. Damn. You take another spin around the place. You don't know where you are, you don't know who you are, and you are trapped inside this small room. You pace slowly around the room with your mind thinking intensely on what to do.\n\tYou pick up the walkie talkie. You press the button for the microphone and speak.\n\t''Hello? Is anyone there?''\n\tThe room remains silent until a voice springs out of the device.\n\t''Ahh yes, hello!'' The voice is very high pitched, seemingly happy, yet, sinister. ''Do you remember who you are?''\n\tYou think once more, trying to remember who you are, but nothing manages to spring up.\n\t''Hello? Do you remember-''\n\t''No,'' you answer unhappily. ''I don't''\n\tSilence fills the room once more, but the voice comes back.\n\t''Good, this should make things much more interesting.''\n\tYou don't know whether you should be scared or happy. Questions pop up in your mind.";
pageTitle[1] = "Your New Home";
pathA[1]= "You brush it off and go anyway";
pathB[1]= "Decide it's not worth it and leave";
// Page 2
storyText[2] = "Test";
pageTitle[2] = "Not worth it";
pathA[2]= "Go back";
pathB[2]= "";
// Page 3
storyText[3] = "Test";
pageTitle[3] = "You brush it off";
pathA[3]= "Go upstairs and explore the mysterious shadow";
pathB[3]= "Go to the kitchen to start unpacking";
// Page 4
storyText[4] = "Test";
pageTitle[4] = "You go upstairs";
pathA[4]= "Check it out";
pathB[4]= "Go to the kitchen to start unpacking";
// Page 5 - End
storyText[5] = "Test";
pageTitle[5] = "Checking it out";
pathA[5]= "Try Again";
pathB[5]= "";
// Page 6
storyText[6] = "Test";
pageTitle[6] = "The Kitchen";
pathA[6]= "It's probably just a cat, continue unpacking";
pathB[6]= "Check it out";
// Page 7
storyText[7] = "Test";
pageTitle[7] = "Unpacking";
pathA[7]= "Check out the basement";
pathB[7]= "";
// Page 8
storyText[8] = "Test";
pageTitle[8] = "Checking it out";
pathA[8]= "Check out the basement";
pathB[8]= "";
// Page 9
storyText[9] = "Test";
pageTitle[9] = "The Basement";
pathA[9]= "Forget the light! Get out of here!";
pathB[9]= "Turn the light on anyways";
// Page 10 - End
storyText[10] = "Test";
pageTitle[10] = "Leaving";
pathA[10]= "Try Again";
pathB[10]= "";
// Page 11
storyText[11] = "Test";
pageTitle[11] = "Lights on";
pathA[11]= "Chase the mysterious shadow";
pathB[11]= "Scream and run upstairs";
// Page 12
storyText[12] = "Test";
pageTitle[12] = "The Mysterious Shadow";
pathA[12]= "News report";
pathB[12]= "";
// Page 13 - END
storyText[13] = "Test";
pageTitle[13] = "News Report";
pathA[13]= "Try Again";
pathB[13]= "";
// Page 14
storyText[14] = "Test";
pageTitle[14] = "Upstairs";
pathA[14]= "Just stay where you are";
pathB[14]= "Answer the door";
// Page 15 - End
storyText[15] = "Test";
pageTitle[15] = "Stay where you are";
pathA[15]= "Try Again";
pathB[15]= "";
// Page 16
storyText[16] = "Test";
pageTitle[16] = "The Door";
pathA[16]= "Go to the Attic";
pathB[16]= "";
// Page 17
storyText[17] = "Test";
pageTitle[17] = "The Attic";
pathA[17]= "Leave it alone";
pathB[17]= "Play with the Ouija Board";
// Page 18
storyText[18] = "Test";
pageTitle[18] = "Left Alone";
pathA[18]= "Jump out of the window";
pathB[18]= "Accept your fate";
// Page 19
storyText[19] = "Test";
pageTitle[19] = "The Ouija Board";
pathA[19]= "Jump out of the window";
pathB[19]= "Accept your fate";
// Page 20 - End
storyText[20] = "Test";
pageTitle[20] = "Out the Window";
pathA[20]= "Try Again";
pathB[20]= "";
// Page 21 - End
storyText[21] = "Test";
pageTitle[21] = "Fate Accepted";
pathA[21]= "Try Again";
pathB[21]= "";
// Loop until Game Over
do {
// Populate fields and play page flip sound
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
currentPage = page;
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
/*
TODO: Enable debug. Manually change page when clicking on the page button
bPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
???
}
});
*/
// Operate first path
bOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 3;
break;
case 2: page = 1;
break;
case 3: page = 4;
break;
case 4: page = 5;
break;
case 5: page = 1;
break;
case 6: page = 7;
break;
case 7: page = 9;
break;
case 8: page = 9;
break;
case 9: page = 10;
break;
case 10: page = 1;
break;
case 11: page = 12;
break;
case 12: page = 13;
break;
case 13: page = 1;
break;
case 14: page = 15;
break;
case 15: page = 1;
break;
case 16: page = 17;
break;
case 17: page = 18;
break;
case 18: page = 20;
break;
case 19: page = 20;
break;
case 20: page = 1;
break;
case 21: page = 1;
break;
default: page = 0;
}
}
});
// Operate second path
bTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 2;
break;
case 2: page = 0;
break;
case 3: page = 6;
break;
case 4: page = 6;
break;
case 5: page = 0;
break;
case 6: page = 8;
break;
case 7: page = 0;
break;
case 8: page = 0;
break;
case 9: page = 11;
break;
case 10: page = 0;
break;
case 11: page = 14;
break;
case 12: page = 0;
break;
case 13: page = 0;
break;
case 14: page = 16;
break;
case 15: page = 0;
break;
case 16: page = 0;
break;
case 17: page = 19;
break;
case 18: page = 21;
break;
case 19: page = 21;
break;
case 20: page = 0;
break;
case 21: page = 0;
break;
default: page = 0;
}
}
});
} while (gameOver == false);
//TODO: Game Over screen
}
}
EDIT2: adopted new suggestion, made according changes. now layout loads, but clicking a button makes the app crash
package com.assignement.cyoa;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Remember extends Activity {
// Declaring variables
int page;
int currentPage;
MediaPlayer pageflip;
TextView mainText;
TextView tvPageTitle;
Button bOne;
Button bTwo;
Button bPage;
// Array creation
String[] storyText = new String[22];
String[] pageTitle = new String[22];
CharSequence[] pathA = new CharSequence[22];
CharSequence[] pathB = new CharSequence[22];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remember);
// Link XML Views with Java
TextView mainText = (TextView) findViewById(R.id.mainText);
TextView tvPageTitle = (TextView) findViewById(R.id.tvPageTitle);
Button bOne = (Button) findViewById(R.id.bOne);
Button bTwo = (Button) findViewById(R.id.bTwo);
Button bPage = (Button) findViewById(R.id.bPage);
// Enable manual scrolling text
mainText = (TextView) findViewById(R.id.mainText);
mainText.setMovementMethod(new ScrollingMovementMethod());
// Enable sound when choosing path
pageflip = MediaPlayer.create(Remember.this, R.raw.pageflip);
///// MAIN TEXT AND POSSIBLE PATHS ARRAYS \\\\\
// Page 0 - Error
storyText[0] = "Path broken";
pageTitle[0] = "Path broken";
pathA[0]= "Path broken";
pathB[0]= "Path broken";
// Page 1
storyText[1] = "\tYou lift open your eyes. All you can see is a bright light. You get up and take a look around you. You are in a very tight room, with no windows and one door. Everything is painted in white. Beside the door is a small walkie talkie which looks brand new.\n\tYou begin to think, where are you? Who are you? You steady yourself and stumble towards the door. You tightly grab the knob and begin to twist it open. It's locked. Damn. You take another spin around the place. You don't know where you are, you don't know who you are, and you are trapped inside this small room. You pace slowly around the room with your mind thinking intensely on what to do.\n\tYou pick up the walkie talkie. You press the button for the microphone and speak.\n\t''Hello? Is anyone there?''\n\tThe room remains silent until a voice springs out of the device.\n\t''Ahh yes, hello!'' The voice is very high pitched, seemingly happy, yet, sinister. ''Do you remember who you are?''\n\tYou think once more, trying to remember who you are, but nothing manages to spring up.\n\t''Hello? Do you remember-''\n\t''No,'' you answer unhappily. ''I don't''\n\tSilence fills the room once more, but the voice comes back.\n\t''Good, this should make things much more interesting.''\n\tYou don't know whether you should be scared or happy. Questions pop up in your mind.";
pageTitle[1] = "Your New Home";
pathA[1]= "You brush it off and go anyway";
pathB[1]= "Decide it's not worth it and leave";
// Page 2
storyText[2] = "Test";
pageTitle[2] = "Not worth it";
pathA[2]= "Go back";
pathB[2]= "";
// Page 3
storyText[3] = "Test";
pageTitle[3] = "You brush it off";
pathA[3]= "Go upstairs and explore the mysterious shadow";
pathB[3]= "Go to the kitchen to start unpacking";
// Page 4
storyText[4] = "Test";
pageTitle[4] = "You go upstairs";
pathA[4]= "Check it out";
pathB[4]= "Go to the kitchen to start unpacking";
// Page 5 - End
storyText[5] = "Test";
pageTitle[5] = "Checking it out";
pathA[5]= "Try Again";
pathB[5]= "";
// Page 6
storyText[6] = "Test";
pageTitle[6] = "The Kitchen";
pathA[6]= "It's probably just a cat, continue unpacking";
pathB[6]= "Check it out";
// Page 7
storyText[7] = "Test";
pageTitle[7] = "Unpacking";
pathA[7]= "Check out the basement";
pathB[7]= "";
// Page 8
storyText[8] = "Test";
pageTitle[8] = "Checking it out";
pathA[8]= "Check out the basement";
pathB[8]= "";
// Page 9
storyText[9] = "Test";
pageTitle[9] = "The Basement";
pathA[9]= "Forget the light! Get out of here!";
pathB[9]= "Turn the light on anyways";
// Page 10 - End
storyText[10] = "Test";
pageTitle[10] = "Leaving";
pathA[10]= "Try Again";
pathB[10]= "";
// Page 11
storyText[11] = "Test";
pageTitle[11] = "Lights on";
pathA[11]= "Chase the mysterious shadow";
pathB[11]= "Scream and run upstairs";
// Page 12
storyText[12] = "Test";
pageTitle[12] = "The Mysterious Shadow";
pathA[12]= "News report";
pathB[12]= "";
// Page 13 - END
storyText[13] = "Test";
pageTitle[13] = "News Report";
pathA[13]= "Try Again";
pathB[13]= "";
// Page 14
storyText[14] = "Test";
pageTitle[14] = "Upstairs";
pathA[14]= "Just stay where you are";
pathB[14]= "Answer the door";
// Page 15 - End
storyText[15] = "Test";
pageTitle[15] = "Stay where you are";
pathA[15]= "Try Again";
pathB[15]= "";
// Page 16
storyText[16] = "Test";
pageTitle[16] = "The Door";
pathA[16]= "Go to the Attic";
pathB[16]= "";
// Page 17
storyText[17] = "Test";
pageTitle[17] = "The Attic";
pathA[17]= "Leave it alone";
pathB[17]= "Play with the Ouija Board";
// Page 18
storyText[18] = "Test";
pageTitle[18] = "Left Alone";
pathA[18]= "Jump out of the window";
pathB[18]= "Accept your fate";
// Page 19
storyText[19] = "Test";
pageTitle[19] = "The Ouija Board";
pathA[19]= "Jump out of the window";
pathB[19]= "Accept your fate";
// Page 20 - End
storyText[20] = "Test";
pageTitle[20] = "Out the Window";
pathA[20]= "Try Again";
pathB[20]= "";
// Page 21 - End
storyText[21] = "Test";
pageTitle[21] = "Fate Accepted";
pathA[21]= "Try Again";
pathB[21]= "";
/*
TODO: Enable debug. Manually change page when clicking on the page button
bPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
???
}
});
*/
// Populate fields according to first page
page = 1;
currentPage = page;
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
// Operate first path
bOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 3;
break;
case 2: page = 1;
break;
case 3: page = 4;
break;
case 4: page = 5;
break;
case 5: page = 1;
break;
case 6: page = 7;
break;
case 7: page = 9;
break;
case 8: page = 9;
break;
case 9: page = 10;
break;
case 10: page = 1;
break;
case 11: page = 12;
break;
case 12: page = 13;
break;
case 13: page = 1;
break;
case 14: page = 15;
break;
case 15: page = 1;
break;
case 16: page = 17;
break;
case 17: page = 18;
break;
case 18: page = 20;
break;
case 19: page = 20;
break;
case 20: page = 1;
break;
case 21: page = 1;
break;
default: page = 0;
}
setPage(page);
}
});
// Operate second path
bTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 2;
break;
case 2: page = 0;
break;
case 3: page = 6;
break;
case 4: page = 6;
break;
case 5: page = 0;
break;
case 6: page = 8;
break;
case 7: page = 0;
break;
case 8: page = 0;
break;
case 9: page = 11;
break;
case 10: page = 0;
break;
case 11: page = 14;
break;
case 12: page = 0;
break;
case 13: page = 0;
break;
case 14: page = 16;
break;
case 15: page = 0;
break;
case 16: page = 0;
break;
case 17: page = 19;
break;
case 18: page = 21;
break;
case 19: page = 21;
break;
case 20: page = 0;
break;
case 21: page = 0;
break;
default: page = 0;
}
}
});
}
private void setPage(int curPage)
{
// Populate fields and play page flip sound
page = curPage;
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
currentPage = page;
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
}
}

Part of your problem is definitely an infinite loop. You have
} while (gameOver == false);
but you never make gameOver == true. But I would reconsider your design. Instead of having this in a loop, it looks like you could have a function that sets the text of your TextViews and do that work and call that from your onClicks passing the function the int that it needs to select the right pages, etc... There is no reason to continually set your OnClickListeners in a loop. It can be done once in onCreate().
Edit
private void setPage(int curPage)
{
page = curPage;
mainText.setText(storyText[page]);
tvPageTitle.setText(pageTitle[page]);
bOne.setText(pathA[page]);
bTwo.setText(pathB[page]);
bPage.setText("" + page);
pageflip.start();
currentPage = page;
// Hide button 2 if it doesn't have a possible option
if (pathB[page] == "")
bTwo.setVisibility(View.INVISIBLE);
else
bTwo.setVisibility(View.VISIBLE);
}
then in your onClick() call the function
bOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (currentPage) {
case 1: page = 3;
break;
case 2: page = 1;
break;
case 3: page = 4;
break;
...
}
setPage(page);
Declare your variables as member variables before (onCreate()) but initialize them in onCreate()
public class Remember extends Activity {
TextView mainText;
TextView tvPageTitle;
Button bOne;
Button bTwo;
Button bPage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remember);
mainText = (TextView) findViewById(R.id.mainText);
tvPageTitle = (TextView) findViewById(R.id.tvPageTitle);
bOne = (Button) findViewById(R.id.bOne);
bTwo = (Button) findViewById(R.id.bTwo);
bPage = (Button) findViewById(R.id.bPage);
You can't initialize your Views before you have inflated the layout with setContentView()
You have
TextView mainText = (TextView) findViewById(R.id.mainText);
inside onCreate(). Change that to
mainText = (TextView) findViewById(R.id.mainText);
remove the TextView otherwise you are creating a local variable and your member variables don't have a value so you will get NPE. Do the same with all of your Views.

If i followed your code correctly, it seems you start on page 1, and then just loop.
Your seem to be relying on break; to exit the do-while, but you are just setting the onclicklistener. Are you actually clicking anything? bOne or bTwo i mean. If you dont click them, infinite loop.

Related

Android how to make images load from HTTP every few seconds

My images is loaded from HttpURLConnection.
I have 3 images and want to change them every 2 seconds in one ImageView.
For example, image1 -> image2 -> image3 -> image1 -> image2 ...
Current, my code is as below:
Picasso.with(this).load(BASE_URL + "admin/"+image1).fit().centerInside().into(ivImage);
Picasso.with(this).load(BASE_URL + "admin/"+image2).fit().centerInside().into(ivImage);
Picasso.with(this).load(BASE_URL + "admin/"+image3).fit().centerInside().into(ivImage);
Use the following method...
private void repeatTask(int counter) {
switch (counter) {
case 0:
Picasso.with(this).load(BASE_URL + "admin/"+image1).fit().centerInside().into(ivImage);
break;
case 1:
Picasso.with(this).load(BASE_URL + "admin/"+image2).fit().centerInside().into(ivImage);
break;
case 2:
Picasso.with(this).load(BASE_URL + "admin/"+image3).fit().centerInside().into(ivImage);
break;
default:
//not possible
break;
}
final int newCounter = counter + 1;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
repeatTask(newCounter == 3 ? 0 : newCounter);
}
}, 2000);
}
This will continue to change your image every 2 sec. Call this method like this in your onCreate() method to start the task...
repeatTask(0);

Can't determine the clicked item in android ViewGroup

I am working with ViewGroup . I want to make a click effect on the item of the group so that User can detect which item is currently clicked. Can anyone please help me?
private View getSubCategoryListItemView(final SubCategoryItem si, double dwPercentage, final int cat_id)
{
LayoutInflater li = LayoutInflater.from(this);
View vv = li.inflate(R.layout.sub_cat_list_item, llCatListHolder, false);
ImageView ivIcon = (ImageView) vv.findViewById(R.id.iv_sub_cat_icon);
final TextView tvName = (TextView) vv.findViewById(R.id.tv_sub_cat_name);
ivIcon.setImageResource(AppConstants.ALL_CAT_MARKER_ICONS[cat_id-1]);
ViewGroup.LayoutParams lpIv = ivIcon.getLayoutParams();
lpIv.width = (int) (primaryIconWidth * dwPercentage);
ivIcon.setLayoutParams(lpIv);
tvName.setText(si.getSubcatHeader());
tvName.setTextSize((float) (VIEW_WIDTH * .10 * dwPercentage));
/**************************
*
* This OnClickListener will be called for clicking subcategory items from the top list
*
* Toast.makeText(getApplicationContext(), "Entertainment ", Toast.LENGTH_SHORT).show();
* ************************/
// tvName.setTextColor(Color.WHITE);
Toast.makeText(getApplicationContext(), "BackTrack ", Toast.LENGTH_SHORT).show();
vv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<SubCategoryItem> subCategoryItems;
subCategoryItems = getSubCategoryList(cat_id);
for(SubCategoryItem si : subCategoryItems)
{
// tvName.setTextColor(Color.WHITE);
}
tvName.setTextColor(Color.RED);
/*code for category*/
/*following code will be different for each category*/
/*category id 1 means education.
* category id 2 means health
* category id 3 means entertainment
* category id 4 means government
* category id 5 means legal
* category id 6 means financial
* category id 7 means job*/
// tvName.setTextColor(Color.WHITE);
switch (currentCategoryID) {
case AppConstants.EDUCATION:
Toast.makeText(getApplicationContext(), "Education Entrance ", Toast.LENGTH_SHORT).show();
ArrayList<EducationServiceProviderItem> eduItem;
eduItem = constructEducationListItemForHeader(cat_id, si.getSubcatHeader());
callMapFragmentWithEducationInfo(si.getSubcatHeader(), cat_id, eduItem);
break;
case AppConstants.HEALTH:
Toast.makeText(getApplicationContext(), "Helath Entrance ", Toast.LENGTH_SHORT).show();
//TODO write necessary codes for health
ArrayList<HealthServiceProviderItem> healthItem;
healthItem = constructHealthListItemForHeader(cat_id, si.getSubcatHeader());
callMapFragmentWithHealthInfo(si.getSubcatHeader(), cat_id, healthItem);
break;
case AppConstants.ENTERTAINMENT:
tvName.setTextColor(Color.GREEN);
Toast.makeText(getApplicationContext(), "Entertainment ", Toast.LENGTH_SHORT).show();
ArrayList<EntertainmentServiceProviderItem> entItem;
entItem = constructEntertainmentListItemForHeader(cat_id, si.getSubcatHeader());
callMapFragmentWithEntertainmentInfo(si.getSubcatHeader(), cat_id, entItem);
break;
//TODO write necessary codes for entertainment
case AppConstants.GOVERNMENT:
//TODO write necessary codes for government
break;
case AppConstants.LEGAL:
Toast.makeText(getApplicationContext(), "Legal ", Toast.LENGTH_SHORT).show();
ArrayList<LegalAidServiceProviderItem>legalItem;
legalItem = constructlegalaidListItemForHeader(cat_id,si.getSubcatHeader());
callMapFragmentWithLegalAidInfo(si.getSubcatHeader(),cat_id,legalItem);
break;
case AppConstants.FINANCIAL:
ArrayList<FinancialServiceProviderItem>financialItem;
financialItem = constructfinancialListItemForHeader(cat_id, si.getSubcatHeader());
callMapFragmentWithFinancialInfo(si.getSubcatHeader(), cat_id, financialItem);
break;
case AppConstants.JOB:
ArrayList<JobServiceProviderItem>jobItem;
jobItem = constructjobListItemForHeader(cat_id, si.getSubcatHeader());
callMapFragmentWithJobInfo(si.getSubcatHeader(), cat_id, jobItem);
break;
default:
break;
}
/*code for all*/
}
});
return vv;
}
flag
I have set an OnclickListener. But I can not detect which TextView is clicked now? I have tried to make Text color changed. Suppose I set Text color orange and commmon color black. When the first item has clicked then the text color is orange. When the another item clicked it has become also make be orange. Then two item is orange color. But previous item should be black after clicking next item and only current item should be orange.

In android dalvik invoke-xxx opcode, what if the argument size is more than 5

in the davik vm/mterp/out/InterpC-portable.cpp code, when interpret invokeMethod, I find it can only handle the case when count is less or equal than 5:
switch (count) {
case 5:
outs[4] = GET_REGISTER(vsrc1 & 0x0f);
case 4:
outs[3] = GET_REGISTER(vdst >> 12);
case 3:
outs[2] = GET_REGISTER((vdst & 0x0f00) >> 8);
case 2:
outs[1] = GET_REGISTER((vdst & 0x00f0) >> 4);
case 1:
outs[0] = GET_REGISTER(vdst & 0x0f);
default:
;
}
then what about the case when the argument size is more than 5?
Sorry I miss something, that the senario I mentioned is the case of non-range call, there is a range call that I missed:
*/
if (methodCallRange) {
// could use memcpy or a "Duff's device"; most functions have
// so few args it won't matter much
assert(vsrc1 <= curMethod->outsSize);
assert(vsrc1 == methodToCall->insSize);
outs = OUTS_FROM_FP(fp, vsrc1);
for (i = 0; i < vsrc1; i++)
outs[i] = GET_REGISTER(vdst+i);
....
so it's handled here!

Android Calculator getting Grand Total (GT)

As I am implementing an Android Calculator, I've come across another requirement of client.
I need to implement the feature in calculator that does the following:
2x3 =6, 2x2 = 4, 2x5 = 10.
Hit GT and will bring up 20(adds up all totals)
I've taken Buttons to perform this operations
Whatever button is pressed, I am taking its KeyCode to detect its number.
Then operator is taken as int operator which can take one of four values. i.e. 1=Add,2=Subtract, and all that.
here is a sample code to do such mathematical operation:
private void handleEquals(int newOperator) {
if (hasChanged) {
switch (operator) {
case 1:
num = num + Double.parseDouble(txtCalc.getText().toString());
break;
case 2:
num = num - Double.parseDouble(txtCalc.getText().toString());
break;
case 3:
num = num * Double.parseDouble(txtCalc.getText().toString());
String strNum = null;
strNum = Double.toString(num);
if(strNum.contains("E")){
strNum = strNum.substring(0, 6) + strNum.substring(strNum.indexOf("E"));
}
Log.i("MULTIPLICATION","Checking Precision");
System.out.println("New StrNum is: " + strNum);
num = Double.valueOf(strNum);
break;
case 4:
num = num / Double.parseDouble(txtCalc.getText().toString());
break;
}
Any suggestions or coding snippets would be greatly appreciated.
Thanks

Android TelephonyManager.getNetworkType() returned constant values in bearer speed order?

TelephonyManager.getNetworkType() returns one of the constant values.
It appears that the constant values have an integer order, by possible bearer link speed.
I know using constant values used in the following manner is generally bad,
however could one use this to determine a basic cutoff for application functionality and have it work between API levels? (in API-v1 there was nothing above 0x03)
if( telephonyManager.getNetworkType() > TelephonyManager.NETWORK_TYPE_EDGE )
{
return "3G! party on!";
}
else if( telephonyManager.getNetworkType() > TelephonyManager.NETWORK_TYPE_UNKNOWN )
{
return "2G, OK. just don't go nuts!";
}
else
{
return "No data sorry"
}
You cannot assume they are in order because they are not. For example, LTE is 13 while HSPAP (HSPA+) is 15. Those are not in order. I wrote a "speed ranking" piece of code, which assignes each network type its own speed rank
public static int getNetTypeSpeedRank(int t) {
switch (t) {
case -1:
t = -1;
case ContextManager.MDM_NETWORK_TYPE_UNKNOWN:
t = 0;
break;
case ContextManager.MDM_NETWORK_TYPE_IDEN:
t = 1;
break;
case ContextManager.MDM_NETWORK_TYPE_GPRS:
t = 2;
break;
case ContextManager.MDM_NETWORK_TYPE_EDGE:
t = 3;
break;
case ContextManager.MDM_NETWORK_TYPE_UMTS:
t = 4;
break;
case ContextManager.MDM_NETWORK_TYPE_CDMA:
t = 5;
break;
case ContextManager.MDM_NETWORK_TYPE_1xRTT:
t = 6;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_0:
t = 7;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_A:
t = 8;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_B:
t = 9;
break;
case ContextManager.MDM_NETWORK_TYPE_HSDPA:
t = 10;
break;
case ContextManager.MDM_NETWORK_TYPE_HSUPA:
t = 11;
break;
case ContextManager.MDM_NETWORK_TYPE_HSPA:
t = 12;
break;
case ContextManager.MDM_NETWORK_TYPE_HSPAP:
t = 13;
break;
case ContextManager.MDM_NETWORK_TYPE_EHRPD:
t = 14;
break;
case ContextManager.MDM_NETWORK_TYPE_LTE:
t = 15;
break;
default:
t = 16;
}
return t;
}
I really wouldn't count on that behavior.

Categories

Resources