This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 5 years ago.
I made an incredibly simple app but for some reason it's just crashing. The first page is a simple login screen however the moment I click login it just crashes. The strange part is that there's not even any heavy code to make it do anything strange like that; I'm just making it travel between activities so far.
The main activity
package com.example.philip.lottery1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends AppCompatActivity {
Button loginButton;
EditText userNameField;
EditText passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginButton = (Button) findViewById(R.id.loginButton);
userNameField = (EditText) findViewById(R.id.userNameField);
passwordField = (EditText) findViewById(R.id.passwordField);
final String userName = userNameField.getText().toString();
String password = passwordField.getText().toString();
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), CreateTicket.class);
intent.putExtra("ClerkID", userName);
startActivity(intent);
}
});
}
}
The activity it leads to
package com.example.philip.lottery1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Random;
public class CreateTicket extends AppCompatActivity {
private Button randomButton;
private Button OKButton;
private Button searchButton;
private EditText[] lottoNumberFields;
private int[] lottoNumbers;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_ticket);
lottoNumberFields = new EditText[5];
lottoNumberFields[1] = (EditText)findViewById(R.id.num1);
lottoNumberFields[2] = (EditText)findViewById(R.id.num2);
lottoNumberFields[3] = (EditText)findViewById(R.id.num3);
lottoNumberFields[4] = (EditText)findViewById(R.id.num4);
lottoNumberFields[5] = (EditText)findViewById(R.id.num5);
randomButton = (Button) findViewById(R.id.randomButton);
OKButton = (Button) findViewById(R.id.OKButton);
searchButton = (Button) findViewById(R.id.searchButton);
randomButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
randommize();
}
});
OKButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createTicket();
}
});
searchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
search();
}
});
}
private void search()
{
//insert code here
Intent intent = new Intent (getBaseContext(),TicketActivity.class);
startActivity(intent);
}
private void randommize()
{
for (int i = 0 ; i < 5 ; i ++)
{
Random random = new Random();
int num = random.nextInt(49) + 1;
lottoNumberFields[i].setText(num+"");
}
}
private void createTicket()
{
for (int i = 0; i < 5; i++)
{
lottoNumbers[i] = Integer.parseInt(lottoNumberFields[i].getText().toString());
}
Intent intent = new Intent(getBaseContext(), FinalActivity.class);
intent.putExtra("lottoNumbers",lottoNumbers);
startActivity(intent);
}
}
Your lottoNumberFields array is of length 5, but you are going from 1 to 5 index values for it, instead of 0 to 4. Change the code as below:
lottoNumberFields = new EditText[5];
lottoNumberFields[0] = (EditText)findViewById(R.id.num1);
lottoNumberFields[1] = (EditText)findViewById(R.id.num2);
lottoNumberFields[2] = (EditText)findViewById(R.id.num3);
lottoNumberFields[3] = (EditText)findViewById(R.id.num4);
lottoNumberFields[4] = (EditText)findViewById(R.id.num5);
Related
I am Completely new to Android SDK and very new to writing code in general. I have been monkeying with the software and following a tutorial on how to make a login for my app but I just can't seem to get it to work. Below is my .Java Code. I can tell that the error is in line 32-42, I don't know why I it does not detect .settext or .SetOnClickListner. Any help would be great.
package com.example.inventory;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private EditText User;
private EditText Password;
private TextView Info;
private Button Login;
private int counter = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
User = (EditText) findViewById(R.id.etName);
Password = (EditText) findViewById(R.id.etPassword);
Info = (TextView) findViewById(R.id.tvInfo);
Login = (Button) findViewById(R.id.btnLogin);
}
Info.setText("No of attemps remaining: 5");
Login.SetOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View view){
validate(Name.getText().toString(), Password.getText().toString());
}
};
}
private void validate(String userName, String userPassword) {
if ((userName.equals("Admin")) && (userPassword.equals("Pass"))) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}else{
counter--;
if (counter== 0){
Login.setEnabled(false);
}
}
}
}
Your .setText() code is outside of the onCreate method. There is not a process to call the .setText() method. Just move the code inside of the bracket above it. Same goes for your OnClickListeners. You were also missing a parenthesis at the close of your OnClickListener.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
User = (EditText) findViewById(R.id.etName);
Password = (EditText) findViewById(R.id.etPassword);
Info = (TextView) findViewById(R.id.tvInfo);
Login = (Button) findViewById(R.id.btnLogin);
Info.setText("No of attemps remaining: 5");
Login.SetOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View view){
validate(Name.getText().toString(), Password.getText().toString());
}
});
}
I have an app where I would like to be able to click on a button, A, and show a certain set of information. Then click the back button and click on button B and show a different set of information. I have coded a test TextView into the Drinks.java file in order to begin the process by confirming what is being passed along. Currently whatever button I push first is getting stuck in the variable. So for example if I push button A, then push the back arrow and push button B, button A is still showing up in the textView. I tried making the Strings empty within the on click listener, to "clear them out" as it were, but that isn't working. Is there a way to wipe out what is in the variable and reassign something else? Or does my problem lie elsewhere?
Bar.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class Bar extends Activity{
String setBarTest = MainActivity.setBar;
String barNameHolder, picHolder, barContactHolder, barPhoneHolder;
int imageInt, textInt1,textInt2, textInt3;
TextView setBarName, setBarContact,setBarPhone;
ImageView barPic;
Button viewAll, beer, wine, mixedDrinks, other, getTaxi;
static String setDrinkType = "";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bar);
Button viewAll = (Button)findViewById(R.id.btnviewAll);
Button beer = (Button)findViewById(R.id.btnBeer);
Button wine = (Button)findViewById(R.id.btnWine);
Button mixedDrinks = (Button)findViewById(R.id.btnMixedDrinks);
Button other = (Button)findViewById(R.id.btnOther);
Button getTaxi = (Button)findViewById(R.id.btnTaxi);
barPic = (ImageView) findViewById(R.id.barPic);
String picHolder = "drawable/"+setBarTest;
int imageInt = getResources().getIdentifier(picHolder, null, getPackageName());
barPic.setImageResource(imageInt);
setBarName = (TextView)findViewById(R.id.barName);
String barNameHolder = "#string/"+setBarTest;
int textInt1 = getResources().getIdentifier(barNameHolder, null, getPackageName());
setBarName.setText(textInt1);
setBarContact = (TextView)findViewById(R.id.barContact);
String barContactHolder = "#string/"+setBarTest+"Contact";
int textInt2 = getResources().getIdentifier(barContactHolder, null, getPackageName());
setBarContact.setText(textInt2);
setBarPhone = (TextView)findViewById(R.id.barPhone);
String barPhoneHolder = "#string/"+setBarTest+"Phone";
int textInt3 = getResources().getIdentifier(barPhoneHolder, null, getPackageName());
setBarPhone.setText(textInt3);
viewAll.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = (new Intent(Bar.this, Drinks.class));
startActivity(i);
}
});
beer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
setDrinkType = "";
setDrinkType = "Beer";
Intent i = (new Intent(Bar.this, Drinks.class));
startActivity(i);
}
});
wine.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
setDrinkType = "";
setDrinkType = "Wine";
Intent i = (new Intent(Bar.this, Drinks.class));
startActivity(i);
}
});
mixedDrinks.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
setDrinkType = "";
setDrinkType = "Mixed Drink";
Intent i = (new Intent(Bar.this, Drinks.class));
startActivity(i);
}
});
other.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
setDrinkType = "";
setDrinkType = "Other";
Intent i = (new Intent(Bar.this, Drinks.class));
startActivity(i);
}
});
getTaxi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = (new Intent(Bar.this, Taxi.class));
startActivity(i);
}
});
}
}
Drinks.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Drinks extends Activity{
TextView drinkHolder;
public static String drinkType = Bar.setDrinkType;
String drinkTestHolder="";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drinks);
drinkTestHolder = drinkType;
drinkHolder = (TextView)findViewById(R.id.drinkTest);
//String barNameHolder = "#string/"+drinkType;
//int textInt1 = getResources().getIdentifier(barNameHolder, null, getPackageName());
drinkHolder.setText(drinkTestHolder);
}
}
Please, use instead for instance the intent sent to the launching Activity:
How do I get extra data from intent on Android?
In my project i several onClick listeners, and all of them are fine, but one, i can not find and error in the code, if i delete the code and retype it and save it, it is fine without errors, if i close and eclipse and comeback later, variable cant be resolved again.
This is where it cant be resolved in the code:
Button webButton = (Button) newStockRow.findViewById(R.id.webButton);
webButton.setOnClickListener(getStockFromWebClickListener);
and this is how i create it:
public OnClickListener getStockFromWebClickListener = new OnClickListener(){
#Override
public void onClick(View arg0) {
TableRow tableR = (TableRow) arg0.getParent();
TextView stock = (TextView) tableR.findViewById(R.id.stockSymbolTextView);
String stockSymbol = stock.getText().toString();
String stockURL = getString(R.string.yahoo_stock_url) + stockSymbol;
Intent getStockWebPage = new Intent(Intent.ACTION_VIEW, Uri.parse(stockURL));
startActivity(getStockWebPage);
}
};
Code for full File:
package com.gscore.quotestock;
import java.util.Arrays;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class StockQ extends Activity {
public final static String STOCK_SYMBOL = "com.gscore.quotestock.STOCK";
private SharedPreferences stockSymbolsEntered;
private TableLayout stockTableScrollView;
private EditText stockSymbolET;
Button enterStockSymbolButton;
Button deleteStocksButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stock_q);
// Get user stock list
stockSymbolsEntered = getSharedPreferences("stockList", MODE_PRIVATE);
// Initialize UI components
stockTableScrollView = (TableLayout) findViewById(R.id.stockTableLayout);
stockSymbolET = (EditText) findViewById(R.id.stockSymbolEditText);
enterStockSymbolButton= (Button) findViewById(R.id.enterButton);
deleteStocksButton= (Button) findViewById(R.id.deleteAllButton);
// Set ClickListeners
enterStockSymbolButton.setOnClickListener(enterButtonClickListener);
deleteStocksButton.setOnClickListener(deleteButtonClickListener);
updateSavedStockList(null);
}
private void updateSavedStockList(String newStockSymbol){
String[] stocks = stockSymbolsEntered.getAll().keySet().toArray(new String[0]);
Arrays.sort(stocks, String.CASE_INSENSITIVE_ORDER);
if (newStockSymbol != null){
insertStockInStockTable(newStockSymbol, Arrays.binarySearch(stocks, newStockSymbol));
} else {
for(int i = 0; i < stocks.length; i++){
insertStockInStockTable(stocks[i], i);
}
}
}
private void saveStockSymbol(String newStock){
String isTheStockNew = stockSymbolsEntered.getString(newStock, null);
SharedPreferences.Editor preferencesEditor = stockSymbolsEntered.edit();
preferencesEditor.putString(newStock, newStock);
preferencesEditor.commit();
if(isTheStockNew == null){
updateSavedStockList(newStock);
}
}
private void insertStockInStockTable(String stock, int arrayIndex){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newStockRow = inflater.inflate(R.layout.stock_quote_row, null);
TextView newStockTextView = (TextView) newStockRow.findViewById(R.id.stockSymbolTextView);
newStockTextView.setText(stock);
Button stockQuoteButton = (Button) newStockRow.findViewById(R.id.stockQuoteButton);
stockQuoteButton.setOnClickListener(getStockActivityListener);
Button webButton = (Button) newStockRow.findViewById(R.id.webButton);
webButton.setOnClickListener(getStockFromWebClickListener);
stockTableScrollView.addView(newStockRow, arrayIndex);
}
public OnClickListener enterButtonClickListener= new OnClickListener(){
#Override
public void onClick(View v) {
if(stockSymbolET.getText().length() > 0){
saveStockSymbol(stockSymbolET.getText().toString());
stockSymbolET.setText("");
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(stockSymbolET.getWindowToken(), 0);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(StockQ.this);
builder.setTitle(R.string.invalid_stock_symbol);
builder.setPositiveButton(R.string.ok, null);
builder.setMessage(R.string.missing_stock_symbol);
AlertDialog theAlertDialog = builder.create();
theAlertDialog.show();
}
}
};
private void deleteAllStocks(){
stockTableScrollView.removeAllViews();
}
public OnClickListener deleteButtonClickListener = new OnClickListener(){
#Override
public void onClick(View v) {
deleteAllStocks();
SharedPreferences.Editor preferencesEditor = stockSymbolsEntered.edit();
preferencesEditor.clear();
preferencesEditor.commit();
}
};
public OnClickListener getStockActivityListener = new OnClickListener(){
#Override
public void onClick(View v) {
TableRow tableR = (TableRow) v.getParent();
TextView stock = (TextView) tableR.findViewById(R.id.stockSymbolTextView);
String stockSymbol = stock.getText().toString();
Intent intent = new Intent(StockQ.this, StockInfoActivity.class);
intent.putExtra(STOCK_SYMBOL, stockSymbol);
startActivity(intent);
}
};
public OnClickListener getStockFromWebClickListener = new OnClickListener(){
#Override
public void onClick(View arg0) {
TableRow tableR = (TableRow) arg0.getParent();
TextView stock = (TextView) tableR.findViewById(R.id.stockSymbolTextView);
String stockSymbol = stock.getText().toString();
String stockURL = getString(R.string.yahoo_stock_url) + stockSymbol;
Intent getStockWebPage = new Intent(Intent.ACTION_VIEW, Uri.parse(stockURL));
startActivity(getStockWebPage);
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.stock_q, menu);
return true;
}
}
That's indeed an annoying problem with importing an inner class (or interface) on Eclipse.
What you have to do is instead of:
new OnClickListener()
Write:
new View.OnClickListener()
And make sure that android.view.View is imported.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
This is the .java file for a small application I am writing in Android through Eclipse, and I am having a minor error or syntax glitch.
at the end bracket marked by asterisks, eclipse is reporting the error 'Syntax error, insert ";" to complete Statement'. I have searched the code, and find nothing unaccounted for or out of place. Could someone please identify or tell me how to fix this? If you need other files, tell me in the comments. Thanks in advance :)!
package org.example.knittingframe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class KnittingFrame extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textview = (TextView)findViewById(R.id.textview);
final EditText op1 = (EditText)findViewById(R.id.NumBox1);
final EditText op2 = (EditText)findViewById(R.id.NumBox2);
final Button btnAdd = (Button)findViewById(R.id.addBox);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
**}** // Here is where the error occurs
}
}
The compiler is right. You need to replace
**}** // Here is where the error occurs
with
});
to finish the btnAdd.setOnClickListener method call.
This is a statement in the onCreate(xx) and its an anonymous class, because an anonymous class is a statement you MUST terminate the statement:
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
**}** // Here is where the error occurs
TO:
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});
Since you are using an annonymous inner class, you need to end the setOnClickListener method call with ");" as follows:
public class KnittingFrame extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textview = (TextView)findViewById(R.id.textview);
final EditText op1 = (EditText)findViewById(R.id.NumBox1);
final EditText op2 = (EditText)findViewById(R.id.NumBox2);
final Button btnAdd = (Button)findViewById(R.id.addBox);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});
}
}
You started a function call: setOnClickListener(). You need to finish it. Like so:
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});
If you indented properly, maybe you'd see it, but you're missing an extra parenthesis and semicolon:
package org.example.knittingframe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class KnittingFrame extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textview = (TextView)findViewById(R.id.textview);
final EditText op1 = (EditText)findViewById(R.id.NumBox1);
final EditText op2 = (EditText)findViewById(R.id.NumBox2);
final Button btnAdd = (Button)findViewById(R.id.addBox);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});
}
}
In main.xml I made a row containing a TextView, an EditText and a "+" and "-" button.
Underneath that I made an "Add" button that will help you create a new row When you click the add button, you get an EditText and a Submit and Cancel button.
On "Submit" it outputs the EditText value to the TextView and creates the same row as the first one.
The numeric value "NewValueBox" should +1 when the "+" button is pressed.
But because I call it in another function it is not recognized by createNewAddButton() function in which the button is set up.
So in short:
"How do I change the value of NewValueBox when I click NewAddButton?"
Here's the code:
package com.lars.MyApp;
import com.google.ads.*;
import com.lars.MyApp.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
public class DrinkRecOrderActivity extends Activity {
int currentValue1 = 0;
int currentValueNew = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText firstValue = (EditText) findViewById(R.id.firstValue);
Button valuePlus = (Button) findViewById(R.id.valuePlus);
Button valueMinus = (Button) findViewById(R.id.valueMinus);
final Button addValue = (Button) findViewById(R.id.add);
final TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);
final LinearLayout addValueRow = (LinearLayout) findViewById(R.id.addValueRow);
final EditText addNewValue = (EditText) findViewById(R.id.addNewValue);
final Button submitNewValue = (Button) findViewById(R.id.submitNewValue);
final Button cancelNewValue = (Button) findViewById(R.id.cancelNewValue);
// BEGIN ONCLICKLISTENERS
valuePlus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
plusValue();
firstValue.setText("" + currentValue1);
}
});
valueMinus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
minValue();
firstValue.setText("" + currentValue1);
}
});
addValue.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addValueRow.setVisibility(View.VISIBLE);
addValue.setVisibility(View.GONE);
}
});
cancelNewValue.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addValueRow.setVisibility(View.GONE);
addValue.setVisibility(View.VISIBLE);
}
});
submitNewValue.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tableLayout1.addView(createnewRow());
addValueRow.setVisibility(View.GONE);
addValue.setVisibility(View.VISIBLE);
addNewValue.setText("");
}
});
// END ONCLICKLISTENERS
// Look up the AdView as a resource and load a request.
AdView adView = (AdView) this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
}
public TableRow createNewRow() {
final TableRow newRow = new TableRow(this);
final EditText addNewValue = (EditText) findViewById(R.id.addNewValue);
newRow.addView(createNewTextView(addNewValue.getText().toString()));
newRow.addView(createNewValueBox());
newRow.addView(createNewAddButton());
newRow.addView(createNewMinusButton());
return newRow;
}
public TextView createNewTextView(String text) {
final TextView textView = new TextView(this);
textView.setText(text);
return textView;
}
public EditText createNewValueBox() {
EditText NewValueBox = new EditText(this);
NewValueBox.setHint("0");
NewValueBox.setInputType(InputType.TYPE_CLASS_NUMBER);
return NewValueBox;
}
public Button createNewAddButton() {
final Button NewAddButton = new Button(this);
NewAddButton.setText("+");
NewAddButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
plusNew();
//NewValueBox.setText("" + currentValueNew);
}
});
return NewAddButton;
}
public Button createNewMinusButton() {
final Button NewMinusButton = new Button(this);
NewMinusButton.setText("-");
return NewMinusButton;
}
// BEGIN PLUS AND MIN FUNCTIONS
public void plusNew() {
if (currentValueNew <= 999) {
currentValueNew = currentValueNew + 1;
}
}
public void plusValue() {
if (currentValue1 <= 999) {
currentValue1 = currentValue1 + 1;
}
}
public void minValue() {
if (currentValue1 >= 1) {
currentValue1 = currentValue1 - 1;
}
}
// END PLUS AND MIN FUNCTIONS
}
Add IDs for your Views so you can later reference them. Make 3 private static int field in your activity(the ID for NewValueBox, NewAddButton and NewMinusButton):
private static int edt = 1;
private static int add = 1001;
private static int minus = 2001;
Then in your createNewValueBox() method set the ID:
NewValueBox.setId(edt);
edt++;
Do the same for the NewAddButton and the NewMinusButton:
NewAddButton.setId(add);
add++;
NewMinusButton.setId(minus);
minus++;
Then in your listener for the buttons find out exactly which add button has been clicked and set the text in the corresponding EditText:
NewAddButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
plusNew();
int tmp = v.getId();
EditText temp = (EditText) findViewById(1 + (tmp - 1001));
temp.setText("" + currentValueNew);
}
Kind of hackish method.