Here is my edited code:
package com.gamesup.braingame;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Easy extends Activity implements OnClickListener{
EditText display;
// This Array says , I am an array that holds arrays
String [][] multiArray = {{"4 + 5", "9"},
{"20 * 3","60"},
{"99 - 9","90"}};
TextView displayExpression;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easy);
display = (EditText)findViewById(R.id.displayText);
display.setText("?");
displayExpression = (TextView) findViewById(R.id.expression);
Button generate = (Button) findViewById(R.id.random_gen);
generate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random ranGenerate = new Random ();
int random = ranGenerate.nextInt(4) ;
// Fetch your random question
String Rquestion = multiArray[random][0];
displayExpression.setText(Rquestion);
}
});
}
static boolean isEmpty = true;
public void num_Clicked(View v){
Button btn = (Button) findViewById(v.getId());
//getting the button object and using a view to get the id of the buttons
if (v.getId()== R.id.del_button){
String s = display.getText().toString();
s = s.substring(0, s.length() - 1);
display.setText(s);
return;
}
if(isEmpty){
display.setText(btn.getText());
isEmpty = false;
}
else{
display.append(btn.getText().toString());
}
}
public void hash_Clicked(View v){
if (v.getId()== R.id.hash_button){
// Get the Answer from your EditText
String answer = display.getText().toString();
// Using a for loop iterate on the base index
for(int i = 0; i < multiArray.length ; i++)
{
// if the answer is in position 1 of Array [i]
if(answer.equals(multiArray[i][1]))
{
// We have found the answer, Congratulate the User
displayExpression.setText("CORRECT");
}else{
displayExpression.setText("INCORRECT");
}
}
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
After the user enters the wrong answer and clicks the random generator button (random_gen) again my app closes with an error. Does this have anything to do with the multiArray being created in the onClick method? What would i have to do to resolve this?
EDIT:
Now when i press the hash button it does not display correct, Do i have to add a different if statement for each position?
Your ranGenerate will generate integers from 0 to 3, and your array indexes go from 0 to 2, so if random happens to be 3, you get an exception.
Change the maximal generated integer to 2 with the following:
int random = ranGenerate.nextInt(3); //this generates integers from 0 to 2
or even better - use the length of your array, so that if you add more to it later, you won't have to change the random generation code:
int random = ranGenerate.nextInt(multiArray.length);
Random ranGenerate = new Random ();
int random = ranGenerate.nextInt(4) ;
nextInt(4) can give values range 0 - 3. and your array have values from 0 - 2.
Change to int random = ranGenerate.nextInt(3) or add another item to array.
Related
In my game i have a hash button which acts like a submit button. when i press the hash button it displays INCORRECT even if the answer is correct, Do i have to add a different if statement for each position in the array? It displays INCORRECT each expression.
(Sorry i'm a new in this)
package com.gamesup.braingame;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Easy extends Activity implements OnClickListener{
EditText display;
// This Array says , I am an array that holds arrays
String [][] multiArray = {{"4 + 5", "9"},
{"20 * 3","60"},
{"99 - 9","90"}};
TextView displayExpression;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easy);
display = (EditText)findViewById(R.id.displayText);
display.setText("?");
displayExpression = (TextView) findViewById(R.id.expression);
Button generate = (Button) findViewById(R.id.random_gen);
generate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random ranGenerate = new Random ();
int random = ranGenerate.nextInt(multiArray.length) ;
// Fetch your random question
String Rquestion = multiArray[random][0];
displayExpression.setText(Rquestion);
}
});
}
static boolean isEmpty = true;
public void num_Clicked(View v){
Button btn = (Button) findViewById(v.getId());
//getting the button object and using a view to get the id of the buttons
if (v.getId()== R.id.del_button){
String s = display.getText().toString();
s = s.substring(0, s.length() - 1);
display.setText(s);
return;
}
if(isEmpty){
display.setText(btn.getText());
isEmpty = false;
}
else{
display.append(btn.getText().toString());
}
public void hash_Clicked(View v){
if (v.getId()== R.id.hash_button){
// Get the Answer from your EditText
String answer = display.getText().toString();
// Using a for loop iterate on the base index
for(int i = 0; i < multiArray.length ; i++)
{
// if the answer is in position 1 of Array [i]
if(answer.equals(multiArray[i][1]))
{
// We have found the answer, Congratulate the User
displayExpression.setText("CORRECT");
}else{
// Tell them how bad they are since they can't solve simple equations!
displayExpression.setText("INCORRECT");
}
}
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Put break statement after you show that the answer is correct, so that you don't iterate through the remaining answers in your array and compare with current, because if you have a correct answer at say index 0, you are still going through the remaining answers in an array, so your "CORRECT" gets replaced by "INCORRECT".
// if the answer is in position 1 of Array [i]
if(answer.equals(multiArray[i][1]))
{
// We have found the answer, Congratulate the User
displayExpression.setText("CORRECT");
break; //<---add this to exit the loop, because correct answer has been found
}
I'm trying to write code so that when the hints option is checked in settings it should display hints, but when I do s it says there is no getContext() method defined. What will this method do and where will i have to define this ?
Here is my code for the logic of my maths app:
package com.gamesup.braingame;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Easy extends Activity implements OnClickListener{
EditText display;
// This Array says , I am an array that holds arrays
String [][] multiArray = {{"4 + 5 = ", "9"},
{"20 * 3 - 1 = ","59"},
{"99 - 9 = ","90"},
{"50 / 2 + 18 = ","43"},
{"9 * 8 = ","72"},
{"4 + 20 - 20 = ","4"},
{"75 / 5 = ","15"},
{"99 - 1 * 3 = ","96"},
{"75 + 25 = ","100"}};
TextView displayExpression;
TextView displayAnswer;
TextView setAnswer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easy);
display = (EditText)findViewById(R.id.displayText);
display.setText("?");
displayExpression = (TextView) findViewById(R.id.expression);
displayAnswer = (TextView) findViewById(R.id.answer_status);
setAnswer = (TextView) findViewById(R.id.answer);
Button generate = (Button) findViewById(R.id.random_gen);
generate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random ranGenerate = new Random ();
int random = ranGenerate.nextInt(multiArray.length) ;
// Fetch your random question
String Rquestion = multiArray[random][0];
displayExpression.setText(Rquestion);
displayAnswer.setText("");
setAnswer.setText("?");
}
});
}
static boolean isEmpty = true;
public void num_Clicked(View v){
Button btn = (Button) findViewById(v.getId());
//getting the button object and using a view to get the id of the buttons
if (v.getId()== R.id.del_button){
String s = display.getText().toString();
s = s.substring(0, s.length() - 1);
display.setText(s);
return;
}
if(isEmpty){
display.setText(btn.getText());
isEmpty = false;
}
else{
display.append(btn.getText().toString());
}
}
//xml attribute to views called android:onclick, that can be used to handle
//clicks directly in the view's activity without need to implement any interface.
public void hash_Clicked(View v){
// Get the Answer from your EditText
String answer = display.getText().toString();
setAnswer.setText(answer);
// Using a for loop iterate on the base index
for(int i = 0; i < multiArray.length ; i++)
{
// if the answer is in position 1 of Array [i]
if(answer.equals(multiArray[i][1]))
{
// We have found the answer, Congratulate the User
displayAnswer.setTextColor(getResources().getColor(R.color.green));
displayAnswer.setText("CORRECT");
break;
}else{
// Tell them how bad they are since they can't solve simple equations!
displayAnswer.setTextColor(getResources().getColor(R.color.red));
displayAnswer.setText("INCORRECT");
//this is where i am getting the error
if(Prefs.getHints(getContext())){
}
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.brain_game, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.settings:
startActivity(new Intent(this,Prefs.class));
return true;
// more items go here if any
}
return false;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
And the Prefs class:
package com.gamesup.braingame;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.os.Bundle;
import android.content.Context;
public class Prefs extends PreferenceActivity {
//option names and default values
private static final String OPT_HINTS = "hints";
private static final boolean OPT_HINTS_DEF = true;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
/**Get the current value of the hints option*/
public static boolean getHints (Context context){
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(OPT_HINTS, OPT_HINTS_DEF);
}
}
Just use this, you already are in an Activity, which is a Context:
if(Prefs.getHints(this)){
Don't need to use other context because if you are calling that function in your Activity then you just need to pass this or yourActivityName.this which are also Context.
So change
if(Prefs.getHints(this)){
or
if(Prefs.getHints(YourActivityName.this)){
Hey guys I know there are many java programs for the factorial of a number...but I am facing problem with android. Following is my code....Thanks
package com.droidacid.apticalc.aptitudes;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.droidacid.apticalc.R;
public class AptiFactorial extends Activity implements android.view.View.OnClickListener{
EditText number;
TextView answer;
Button calculate;
long factorial = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.apti_factorial);
initialize();
calcFactorial();
}
private void initialize() {
number = (EditText) findViewById(R.id.et_apti_number);
answer = (TextView) findViewById(R.id.tv_apti_answer);
calculate = (Button) findViewById(R.id.b_apti_calc);
calculate.setOnClickListener(this);
}
private void calcFactorial() {
if (number.getText().toString().equals("")) number.setText("0");
int num = Integer.parseInt(number.getText().toString());
for(int i = 1; i<=num; i++){
factorial = i * factorial;
}
}
#Override
public void onClick(View v) {
calcFactorial();
answer.setText("Factorial of " + number.getText().toString() + " is : " + factorial);
}
}
This is my code and I need to know a way around for setting the hint instead of 0, but if I remove if (number.getText().toString().equals("")) number.setText("0");
then getting NullPointerException...
Also on the first go its calculating correctly but If I calculate again then getting wrong answer. I think some loop issue because I am directly using the value of factorial.
Please help thanks
Forget all that equal stuff, cover all basis and do it like this:
package com.droidacid.apticalc.aptitudes;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.droidacid.apticalc.R;
public class AptiFactorial extends Activity implements android.view.View.OnClickListener{
EditText number;
TextView answer;
Button calculate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.apti_factorial);
initialize();
}
private void initialize() {
number = (EditText) findViewById(R.id.et_apti_number);
number.setHint("Enter number to be factorialized :P")
answer = (TextView) findViewById(R.id.tv_apti_answer);
calculate = (Button) findViewById(R.id.b_apti_calc);
calculate.setOnClickListener(this);
}
private long calcFactorial() {
long factorial = 1;
try {
factorial = Long.parseLong(number.getText().toString());
for(int i=factorial-1; i>0; i--){
factorial = i * factorial;
}
} catch (NumberFormatException e) {
Toast.makeText(this, "Incorrect Input", Toast.LENGTH_LONG).show();
} finally {}
return factorial;
}
#Override
public void onClick(View v) {
answer.setText("Factorial of " + number.getText().toString() + " is : " + calcFactorial());
}
Do something like.
private void calcFactorial() {
int num = 0;
if (!number.getText().toString().equals(""))
num = Integer.parseInt(number.getText().toString());
for(int i = 1; i<=num; i++){
factorial = i * factorial;
}
}
Rather than using
(number.getText().toString().equals("")) number.setText("0");
Use
(number.getText().length==0) number.setText("0");
** first go its calculating correctly but If I calculate again then getting wrong answer**
try like this
private void calcFactorial() {
factorial = 1;///Check out the line..
int num = 0;
if (!number.getText().toString().equals(""))
num = Integer.parseInt(number.getText().toString());
for(int i = 1; i<=num; i++){
factorial = i * factorial;
}
}
You need to make sure variable factorial is reinitialise to 1 .. for getting factorial for next number..
private void fact_isClicked(View view) {
if(textField.length()>0){
int ifac = Integer.parseInt(textField.getText().toString());
int fact = factorial(ifac);
tvsec.setText(String.valueOf(fact));
textField.setText(ifac+"!");
}}
I am new to Android development and currently learning to design for a basic calculator app. I have already self designed the layout, but sourced Main Activity codes from different websites for the operations for 0 to 9, +, -, *, / and after incorporation, they work fine.
However, I do want to further modify the MainActivity with decimal point function.
While integer can be shown properly to the screen using "current = current * 10 + number", eg 53 = 5*10+3;
I am thinking applying the same approach for decimal point with a loop function, the idea like this:
1. current = current + remaining if dot button is pressed
2. create an integer i, i increases by 1 once any numerial button is clicked
3. so that when e.g. input 5.3, i =1, it will = 5 + 3/(10^i) = 5.3
4. 5.3 loop to here, then when e.g. input as 5.39, now i=2, it will = 5.3 + 9/(10^i) = 5.39
QUESTION >>
*Yet...really...I am so fresh that I do not know how to design the coding for the decimal button, would there be anyone can suggest the code?* first ignore the following addons where errors to be detected (such as delete the second dot if the dot is input twice or more, adding 0 in front of . if say, .5 is input)
The button id is as follows, and once clicked to refer to DecimalClickEvent
Button b_decimal = (Button) findViewById(R.id.decimal);
b_decimal.setOnClickListener(new DecimalClickEvent(???));}
Many many thanks in advance!! The codes are attached below for reference and your comments:
=========================MainActivity.java=====================================
package com.trial.newcalculator;
import java.io.Serializable;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
#SuppressLint("ParserError")
public class MainActivity extends Activity {
State s;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
s = new State();
int[] opNumbers = new int[] {
R.id.zero,
R.id.one,
R.id.two,
R.id.three,
R.id.four,
R.id.five,
R.id.six,
R.id.seven,
R.id.eight,
R.id.nine,
};
final TextView textView = (TextView) findViewById(R.id.ansEditText);
for (int i = 0; i < 10;i++){
final Button button = (Button) findViewById(opNumbers[i]);
button.setOnClickListener(new NumberClickEvent(textView,s,i));
}
int[] opButtons = new int[] { R.id.add, R.id.subtract, R.id.multiply, R.id.divide };
State.Operation[] states = new State.Operation[] {
State.Operation.PLUS,
State.Operation.MINUS,
State.Operation.MULTIPLY,
State.Operation.DIVIDE};
for(int i = 0; i < opButtons.length;i++){
Button b_op = (Button) findViewById(opButtons[i]);
b_op.setOnClickListener(new OperationClickEvent(textView, s, states[i]));
}
// Memory functions
int[] memButtons = new int[] { R.id.MC, R.id.MR, R.id.Mdeduct, R.id.Mplus};
State.Operation[] mstates = new State.Operation[] {
State.Operation.MEMORYCLEAR,
State.Operation.MEMORYCALL,
State.Operation.MEMORYMINUS,
State.Operation.MEMORYPLUS};
for(int i = 0; i < memButtons.length;i++){
Button b_mem = (Button) findViewById(memButtons[i]);
b_mem.setOnClickListener(new OperationClickEvent(textView, s, states[i]));
}
// Memory functions
//decimal
// Button b_decimal = (Button) findViewById(R.id.decimal);
// b_decimal.setOnClickListener(new DecimalClickEvent(textView, s, "."));
//decimal
Button b_eq = (Button) findViewById(R.id.equal);
b_eq.setOnClickListener(new EqualClickEvent(textView, s));
Button b_op = (Button) findViewById(R.id.ac);
b_op.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
s.clear();
textView.setText(s.getDisplay());
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putSerializable("STATE", s);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
Serializable serializable = savedInstanceState.getSerializable("STATE");
if(serializable!= null){
s = (State) serializable;
}
}
public void onPause(){
super.onPause();
}
}
===============================State.java==================================
package com.trial.newcalculator;
import java.io.Serializable;
import android.text.Editable;
import android.widget.TextView;
public class State implements Serializable {
private static final long serialVersionUID = -1231231231231233L;
public TextView output;
public enum Operation {
PLUS,
MINUS,
MULTIPLY,
DIVIDE,
MEMORYPLUS,
MEMORYMINUS,
MEMORYCALL,
MEMORYCLEAR,
}
public enum IOState{
INPUTTING,
DISPLAY_RESULT,
}
private Double accu = null;
private double current = 0;
private double memory = 0;
private Operation currentOp = null;
private IOState currentState = IOState.INPUTTING;
public Operation getCurrentOp() {
return currentOp;
}
public void setCurrentOp(Operation currentOp) {
if (currentState == IOState.INPUTTING){
if (accu != null && this.currentOp != null ){calculateResult();
}
else{accu = Double.valueOf(current);current = 0;
}
}
this.currentOp = currentOp;
if (currentState == IOState.INPUTTING){
currentState = IOState.DISPLAY_RESULT;
}
}
private void calculateResult() {
double res = accu.doubleValue();
switch (currentOp) {
case PLUS:
res += current;
break;
case MINUS:
res -= current;
break;
case MULTIPLY:
res *= current;
break;
case DIVIDE:
res /= current;
break;
case MEMORYPLUS:
memory += current;
break;
case MEMORYMINUS:
memory -= current;
break;
case MEMORYCLEAR:
memory = 0;
break;
case MEMORYCALL:
current = memory;
break;
}
accu = Double.valueOf(res);
current = 0;
}
public void number(int number) {
if (currentState == IOState.INPUTTING){
current = current *10 + number;
}
else if(currentState == IOState.DISPLAY_RESULT){
currentState = IOState.INPUTTING;
current = number;
}
}
public String getDisplay() {
String res;
Double d = getCurrentDisplayValue();
double doubleValue = d.doubleValue();
int intVal = (int)doubleValue;
if (intVal == doubleValue){
res = Integer.toString(intVal);
}
else{
res = d.toString();
}
return res;
}
private Double getCurrentDisplayValue() {
Double d = accu;
if (currentState == IOState.INPUTTING){
d = Double.valueOf(current);
}
return d;
}
public void clear() {
accu = null;
currentState = IOState.INPUTTING;
currentOp = null;
current = 0;
}
public void equal() {
if (accu == null || currentOp == null){
return;
}
calculateResult();
currentState = IOState.DISPLAY_RESULT;
currentOp = null;
current = getCurrentDisplayValue();
}
}
====================OperationClickEvent.java===============================
package com.trial.newcalculator;
import com.trial.newcalculator.State.Operation;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
final class OperationClickEvent implements OnClickListener {
private State s;
private TextView textView;
private Operation op;
public OperationClickEvent(TextView textView, State s, State.Operation op) {
super();
this.op = op;
this.s = s;
this.textView = textView;
}
public void onClick(View v) {
s.setCurrentOp(op);
textView.setText(s.getDisplay());
}
}
=================EqualClickEvent.java=======================================
package com.trial.newcalculator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
final class EqualClickEvent implements OnClickListener {
private State s;
private TextView textView;
public EqualClickEvent(TextView textView, State s) {
super();
this.s = s;
this.textView = textView;
}
public void onClick(View v) {
s.equal();
textView.setText(s.getDisplay());
}
}
======================NumberClickEvent.java==================================
package com.trial.newcalculator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
final class NumberClickEvent implements OnClickListener {
private int number;
private State s;
private TextView textView;
public NumberClickEvent(TextView textView, State s, int number) {
super();
this.number = number;
this.s = s;
this.textView = textView;
}
public void onClick(View v) {
s.number(number);
textView.setText(s.getDisplay());
}
}
Instead of creating new classes for negative click event and doublezeroclickevent, i suggest you to get their id's and do the proper functionality when the particular button is clicked .
Each OnClickListener can be a default anonymous class, that will be triggered only by one button.
So no need to name them "NegativeClickEvent", "DoublezeroClickEvent", etc.
You can keep a boolean variable if the "." was pressed, and accept another one only if not.
If you want a usable calculator, you need a "backspace" button as well. So note that the dot may be deleted. (need to keep track of its placement).
For the leading 0, you can use a simple "if dot was pressed first, insert 0.".
There are many ways to implement what you want, but to keep it simple, just handle the cases you need to handle.
Other ways to implement this may include Double.parseDouble(s) with catch (NumberFormatException e) on each key pressed, or using a double as your buffer, and using the buttons to perform mathematical operations on it directly (such as multiply by 10 and adding the new digit on each button press) - this will ensure input validity without the need to parse String, but it's more complicated to implement.
Me Doing "edu.sju.BlackJack" Is not causing updates that are later called to occur.
I reference the layout correctly and the calls that are supposed to update it are correct, so what do I put in for the package name?
I should add that my package name according to the manifest is the above.
This is the code I have now which currently doesn't update the screen (or i'm guessing change the value correctly).
RemoteViews name = new RemoteViews("edu.sju.BlackJack", R.layout.play_screen);
If that's not it.. would it then be this code?
name.setTextViewText(R.id.Dealer_Total, "0");
Dealer_Total is the id for the TextView that I want to change.. however again the Change is not occurring.
Thanks in advance for any and all assistance.
Here is the whole of my code that i'm talking about, first Playscreen.java
package edu.sju.BlackJack;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import java.util.*;
public class PlayScreen extends Activity implements OnClickListener {
/** Called when the activity is first created. */
GameEngine Engine = new GameEngine();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play_screen);
TextView TextDealer = (TextView)findViewById(R.id.Dealer_Total);
Engine.setView(TextDealer);
//Set up click listeners for all the buttons
View hitButton = findViewById(R.id.hit_button);
hitButton.setOnClickListener(this);
View standButton = findViewById(R.id.stand_button);
standButton.setOnClickListener(this);
//new preplay button (ML 10/24/10)
View prePlayButton = findViewById(R.id.prePlay_button);
prePlayButton.setOnClickListener(this);
Thread thread = new Thread(Engine);
thread.start();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.prePlay_button:
v.setVisibility(View.GONE);
System.out.println("Working?");
Engine.setGameStart(1);
break;
case R.id.hit_button:
Engine.gameHit(1);
break;
case R.id.stand_button:
Engine.gameStand(1);
break;
}
// More buttons go here (if any) ...
}
}
Now here's the GameEngine Thread
Not the Whole of it, just enough so you get the idea
package edu.sju.BlackJack;
import java.util.Random;
import android.widget.RemoteViews;
import android.widget.TextView;
public class GameEngine implements Runnable {
static int playerCount = 0; //keep record of which cards to change for player when hit is selected
static int dealerCount = 0; //keep record of which cards to change for dealer when dealer hits
static int win = 0; //keeps record of wins (JV 10/01/10)
static int lose = 0; //keeps record of loss (JV 10/01/10)
static int hit = 0; //let's engine know if hit button was selected (0 means it has not)
static int stand = 0; //let's engine know if stand button was selected (0 means it has not)
static int playerTotal = 0; //tells player's total (JV 10/01/10)
static int dealerTotal = 0; //tells dealer's total (JV 10/01/10)
static int playerTurn = 0; //activates buttons so that they do actions when clicked (as it's players turn)
static int startGame = 0; //starts the game when the start game button is pressed
TextView TextDealer;
RemoteViews name = new RemoteViews("edu.sju.BlackJack", R.layout.play_screen);
public void run() {
name.setTextViewText(R.id.Dealer_Total, "0");
//main();
}
public void setView(TextView a)
{
TextDealer = a;
}
public void setGameStart(int i)
{
startGame = i;
}
public void gameHit(int i)
{
if(playerTurn == 1)
hit = 1;
}
public void gameStand(int i)
{
if(playerTurn == 1)
stand = 1;
}
public void main()
{//Start Game
Deck mainDeck = new Deck();
fillDeck(mainDeck);
//TextView TextPlayer = (TextView)findViewById(R.id.Player_Total);
//TextDealer.setText("" + dealerTotal);
//TextPlayer.setText("" + playerTotal);
while(true)
{
if(startGame == 1)
{
if(mainDeck.getList().size() < 15){
mainDeck = emptyDeck();
fillDeck(mainDeck);
}
//RESET CARD VIEWS TO DEFAULT
//RESET DEALERCARD AND PLAYERCARD TOTALS TO 0
dealerTotal = 0;
playerTotal = 0;
playerCount = 0;
dealerCount = 0;
//playHand(mainDeck);
}
}
}
Whatever your problem is, I don't think it is what you think it is. If your layout is appearing in the app widget, then the package name is being handled properly. If the update (your setTextViewText() call) is not having an effect, then either R.layout.play_screen does not have R.id.Dealer_Total or you are not sending over a RemoteViews that contains the setTextViewText() instructions.