I'm trying to get a string value from a listview and put into another view. I've managed to do this successfully for my first listview but it won't seem to work for my third. ( I managed to get it to work but its stopped working now for reasons I cant understand) I am using the exact same method and the information i'm trying to retrieve is being retrieved as I have confirmed while running the problem is its just not getting changed.
Code
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statseditor);
dbHelper = new PlayerStatsDatabase(this);
dbHelper.open();
Bundle extras = getIntent().getExtras();
if(extras !=null){
//String value = extras.getString("Name");
}
//Clean all data
// dbHelper.deleteAllStats();
//Add some data
//dbHelper.insertSomeCountries();
//Generate ListView from SQLite Database
//displayListView();
// list = getListView();
PlayerNameText = extras.getString("Name");
btnSave2 = (Button) findViewById(R.id.btnSaveStats);
btnClear = (Button) findViewById(R.id.btnClearStats);
txtGoalsScored = (EditText) findViewById(R.id.txtGoal);
txtMinutesPlayed = (EditText) findViewById(R.id.txtMinPlayed);
txtSubstituteIn = (EditText) findViewById(R.id.txtSubIn);
txtSubstituteOut = (EditText) findViewById(R.id.txtSubOut);
radioSubIn = (RadioButton) findViewById (R.id.rdoSubIn);
radioSubOut = (RadioButton) findViewById (R.id.rdoSubOut);
playerRating = (RatingBar) findViewById (R.id.ratingBar1);
//playerTitle = (TextView) findViewById (R.id.textTitle);
playerName = (TextView) findViewById (R.id.textView1);
playerName.setText(PlayerNameText);
//playerTitle.setText (PlayerNameText);
checkBox = (CheckBox) findViewById (R.id.checkBox1);
if (checkBox.isChecked()){
checkBox.append("Booked");
}
final CharSequence checkText = checkBox.getText();
//final Context context =
btnSave2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PlayerStatsDatabase db = new PlayerStatsDatabase(getApplicationContext());
db.open();
db.createStats(txtGoalsScored.getText().toString(), txtMinutesPlayed.getText().toString(),txtSubstituteIn.getText().toString(),txtSubstituteOut.getText().toString(), checkText.toString());
db.close();
dipsplayPlayerName();
displayListView();
//Intent intent = new Intent(context, ListItemsActivity.class);
// startActivity(intent);
}
});
}
In this first instance where I use this method playerName actually gets changed to PlayerNameText
But further down when I try to use it again it doesnt work
private void dipsplayPlayerName() {
setContentView (R.layout.statslist);
Bundle extras = getIntent().getExtras();
PlayerNameText = extras.getString("Name");
playerTitle = (TextView) findViewById (R.id.textTitle);
playerTitle.setText (PlayerNameText);
}
Any ideas? The is no LogCat as the app doesnt crash
Put the text data you received into a field and use it instead of getting it from the intent again.
Also, check that displayListView() doesn't change the textView.
Related
I get "non-static variable variable cannot be referenced from a static context" warning eventhough I didn't use static expression at Android Studio. I don't understand why. Could you please help?
I've tried to share code but because it's too long I couldn't.
Problem starts after onClick method.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// gameStart = (Button) findViewById(R.id.start);
newRoundButton = (Button) findViewById(R.id.newRound);
hitButton = (Button) findViewById(R.id.hit);
newGameButton = (Button) findViewById(R.id.newGame);
passButton = (Button) findViewById(R.id.pass);
playerC1 = (ImageView) findViewById(R.id.playerC1);
newRoundButton.setOnClickListener(this);
hitButton.setOnClickListener(this);
newGameButton.setOnClickListener(this);
passButton.setOnClickListener(this);
//hitButton.setVisibility(View.INVISIBLE);
//newGameButton.setVisibility(View.INVISIBLE);
// passButton.setVisibility(View.INVISIBLE);
dealer = (TextView) findViewById(R.id.dealer);
player = (TextView) findViewById(R.id.player);
playerTotal = (TextView) findViewById(R.id.playerTotal);
dealerDeckTable = (TextView) findViewById(R.id.dealerDeckTable);
playerDeckTable = (TextView) findViewById(R.id.playerDeckTable);
skor = (TextView) findViewById(R.id.score);
gameMessage = (TextView) findViewById(R.id.gameMessage);
dealerTotal = (TextView) findViewById(R.id.dealerTotal);
playerSkor = (TextView) findViewById(R.id.playerSkor);
dealerSkor = (TextView) findViewById(R.id.dealerSkor);
kontrol = (TextView) findViewById(R.id.kontrol);
playerDeckTable = (TextView) findViewById(R.id.playerDeckTable);
dealerDeckTable = (TextView) findViewById(R.id.dealerDeckTable);
playerC1.setImageDrawable((ContextCompat.getDrawable(getApplicationContext(),R.drawable.nocard)));
}
public void onClick(View v) {
if (v.getId() == newGameButton.getId()) {
gameMessage.setText(Cards.firstStart());
playerSkor.setText(Cards.pskorS);
dealerSkor.setText(Cards.dskorS);
playerTotal.setText(Cards.pValS);
dealerTotal.setText("N/A");
playerDeckTable.setText(Cards.pHand);
dealerDeckTable.setText(Cards.dHand2);
newGameButton.setVisibility(View.INVISIBLE);
kontrol.setText(Cards.kontrolS);
code continues...
Cards class does not contain any static variable and I get that warning lines getting information from Cards class.
I hope details are enough to explain the problem..
The Prolem is, that u call Cards.firstStart() like a static method. You should initialize your Cardobject with Cards cards = new Cards()
I'm creating a simple Android app with 2 Activities. Activity B displays a bunch of data passed from A. When the user press the back button from B I need to clear some variables in order to not duplicate the data shown on B, but both variables I'm erasing on onResume() aren't clearing at all. Here's my code:
public class MainActivity extends AppCompatActivity {
List<Camarera> camareras = new ArrayList<Camarera>();
List<String> resultados = new ArrayList<String>();
int camHabs = 0;
Button boton;
TextView dinero;
TextView camarera1, camarera2, camarera3, camarera4, camarera5;
EditText hora1, hora2, hora3, hora4, hora5;
EditText resta1, resta2, resta3, resta4, resta5;
Switch switch1, switch2, switch3, switch4, switch5;
#Override
public void onResume()
{
super.onResume();
resultados = new ArrayList<>();
camHabs = 0;
Toast.makeText(getApplicationContext(),"Resumiendo", Toast.LENGTH_LONG).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
boton = (Button) findViewById(R.id.Boton);
camHabs = 0;
boton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Calcular();
}
});
}
void Calcular(){
dinero = (TextView) findViewById(R.id.Dinero);
camarera1 = (TextView) findViewById(R.id.Camarera1);
camarera2 = (TextView) findViewById(R.id.Camarera2);
camarera3 = (TextView) findViewById(R.id.Camarera3);
camarera4 = (TextView) findViewById(R.id.Camarera4);
camarera5 = (TextView) findViewById(R.id.Camarera5);
hora1 = (EditText) findViewById(R.id.C_Horas1);
hora2 = (EditText) findViewById(R.id.C_Horas2);
hora3 = (EditText) findViewById(R.id.C_Horas3);
hora4 = (EditText) findViewById(R.id.C_Horas4);
hora5 = (EditText) findViewById(R.id.C_Horas5);
resta1 = (EditText) findViewById(R.id.C_Resta1);
resta2 = (EditText) findViewById(R.id.C_Resta2);
resta3 = (EditText) findViewById(R.id.C_Resta3);
resta4 = (EditText) findViewById(R.id.C_Resta4);
resta5 = (EditText) findViewById(R.id.C_Resta5);
switch1 = (Switch) findViewById(R.id.C_Switch1);
switch2 = (Switch) findViewById(R.id.C_Switch2);
switch3 = (Switch) findViewById(R.id.C_Switch3);
switch4 = (Switch) findViewById(R.id.C_Switch4);
switch5 = (Switch) findViewById(R.id.C_Switch5);
List<TextView> nombres = Arrays.asList(camarera1, camarera2, camarera3, camarera4, camarera5);
List<EditText> horas = Arrays.asList(hora1, hora2, hora3, hora4, hora5);
List<EditText> restas = Arrays.asList(resta1, resta2, resta3, resta4, resta5);
List switches = Arrays.asList(switch1, switch2, switch3, switch4, switch5);
int resultado;
int maxHoras = 0;
int Dinero = Integer.parseInt(dinero.getText().toString());
for (int i = 0; i < nombres.size(); i++) {
Camarera cam = new Camarera();
cam.SetNombre(nombres.get(i).getText().toString());
cam.SetHoras(horas.get(i).getText().toString());
maxHoras += cam.GetHoras();
cam.SetResta(restas.get(i).getText().toString());
Switch var = (Switch) switches.get(i);
cam.activa = var.isChecked();
camareras.add(cam);
}
for (int i = 0; i < camareras.size(); i++) {
Camarera cam = camareras.get(i);
if (cam.activa) {
camHabs++;
if (cam.error) {
Toast.makeText(getApplicationContext(),cam.error_S, Toast.LENGTH_LONG).show();
return;
}
resultado = ((cam.GetHoras() * Dinero) / maxHoras) - cam.GetResta();
resultados.add(cam.GetNombre() + " se lleva " + resultado + "€");
}
}
Intent intent = new Intent(MainActivity.this, Tabla.class);
intent.putExtra("lista", (ArrayList<String>) resultados);
intent.putExtra("habs", camHabs);
startActivity(intent);
}
Could you help me please? Been stuck with this the whole day.
Try to equal them to null into on Resume and then after that recreate the empty value as you've already done. Let me know if it works.
From the docs on OnResume:
Be aware that the system calls this method every time your activity comes into the foreground, including when it's created for the first time.
It is even called when you press home button/switch apps, so I don't think it is a good idea to do this here.
Why not just do it before you call your startActivity call:
Intent intent = new Intent(MainActivity.this, Tabla.class);
intent.putExtra("lista", (ArrayList<String>) resultados);
intent.putExtra("habs", camHabs);
//Reset variables here
resultados = new ArrayList<>();
camHabs = 0;
startActivity(intent);
Actually, the error wasn't on those two variables, it was on a third one called "camareras" I wasn't clearing it and because I use it in each cycle it would add again and again to "camHabs".
Thanks anyways!
I have three different classes and when the ImageButton in class1 is clicked I want that the TextView in class3 should change to "50". On the other hand when the ImageButton in class2 is clicked I want that the TextView in class3 should change to "0".
class1:
ImageButton button1 = (ImageButton) this.findViewById(R.id.imageButton);
if (button1 != null) {
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent passdata_intent1 = new Intent(class1.this, class3.class);
String data1 = "50";
Bundle bundle1 = new Bundle();
bundle1.putString("firstdata", data1);
passdata_intent1.putExtras(bundle1);
startActivity(passdata_intent1);
}
});
}
class2:
ImageButton button1 = (ImageButton) this.findViewById(R.id.imageButton);
if (button1 != null) {
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent passdata_intent2 = new Intent(class2.this, class3.class);
String data2 = "0";
Bundle bundle2 = new Bundle();
bundle2.putString("seconddata", data2);
passdata_intent2.putExtras(bundle2);
startActivity(passdata_intent2);
}
});
}
class3:
TextView score = (TextView) findViewById(R.id.textViewscore);
Bundle bundle1 = getIntent().getExtras();
String data_1 = bundle1.getString("firstdata");
score.setText(data_1);
Bundle bundle2 = getIntent().getExtras();
String data_2 = bundle2.getString("seconddata");
score.setText(data_2);
So my problem is that when I start the application and I click on the ImageButton in class2 the TextView in class3 changes. But when I click the ImageButton in class1 nothing changes in class3.
From the snippets of code I see the problem seems to be that you first check for "firstdata" extra in the intent set it to the text view and then you check for "seconddata" extra and override the value in the text view with it.
When you pass the firstdata to the activity the seconddata (if not passed) should be null and thus you set the score text to null and erase the first data value from it.
There is no need to user 2 different names for the extras in order to pass data to the same textview from 2 different entry points.
Use "firstdata" extra name for both class1 and class2 to pass the data and it should work.
Yo uare overriding the score value in both cases. If else logic will work fine.
if(getIntent().hasExtras("firsdata")){
Bundle bundle1 = getIntent().getExtras();
String data_1 = bundle1.getString("firstdata");
score.setText(data_1);
} else{
Bundle bundle2 = getIntent().getExtras();
String data_2 = bundle2.getString("seconddata");
score.setText(data_2);
}
problem: I set point breaks at the following code:
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_NAME, workoutName);
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_DAYS, workoutDays);
, both showed up as null when I ran the app in debug mode. workoutName contains a simple String that is passed to a new activity, whereas workoutDays constains an array of String.
the full code is provided below:
public class CreateWorkoutActivity extends Activity {
public final String TAG = this.getClass().getSimpleName();
protected String[] workoutDays = new String[7];
protected String workoutName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_workout);
Button mNextButton = (Button) findViewById(R.id.next_button1);
CheckBox satBox = (CheckBox) findViewById(R.id.sat_checkbox);
CheckBox sunBox = (CheckBox) findViewById(R.id.sun_checkbox);
CheckBox monBox = (CheckBox) findViewById(R.id.mon_checkbox);
CheckBox tuesBox = (CheckBox) findViewById(R.id.tues_checkbox);
CheckBox wedBox = (CheckBox) findViewById(R.id.wed_checkbox);
CheckBox thursBox = (CheckBox) findViewById(R.id.thurs_checkbox);
CheckBox friBox = (CheckBox) findViewById(R.id.fri_checkbox);
final EditText mWorkoutName = (EditText) findViewById(R.id.workout_name1);
workoutName = mWorkoutName.getText().toString();
Log.i(TAG, workoutName);
if (satBox.isChecked()) {
workoutDays[0] = new String(satBox.getText().toString());
}
if (sunBox.isChecked()) {
workoutDays[1] = new String(sunBox.getText().toString());
}
if (monBox.isChecked()) {
workoutDays[2] = new String(monBox.getText().toString());
}
if (tuesBox.isChecked()) {
workoutDays[3] = new String(tuesBox.getText().toString());
}
if (wedBox.isChecked()) {
workoutDays[4] = wedBox.getText().toString();
}
if (thursBox.isChecked()) {
workoutDays[5] = satBox.getText().toString();
}
if (friBox.isChecked()) {
workoutDays[6] = friBox.getText().toString();
Log.i(TAG, workoutDays[6]);
}
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, workoutDays.toString());
Intent intent = new Intent(CreateWorkoutActivity.this, WorkoutRoutinesActivity.class);
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_NAME, workoutName);
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_DAYS, workoutDays);
Log.i(TAG, workoutDays.toString());
startActivity(intent);
}
});
}
The problem is not in the intent, but in the way you obtain workoutName (this is the null value). You create the activity, set up final EditText mWorkoutName = (EditText) findViewById(R.id.workout_name1); and then immediately ask for the input value through workoutName = mWorkoutName.getText().toString();, but at this time the user still hasn't entered anything. You should put that second line in the listener below (so its activated only after the user presses mNextButton. It's a good idea to put some check after it and send a message to user that they need to fill in that field (if it is indeed necessary).
Looks like the values for workoutName and workoutDays are not filled in initially when the view is created. You should move retrieving the value from the text fields to your onClickListener function.
you checking the CheckBox and EditText in onCreate, absolutely the EditText will be empty and all CheckBox it not checked
I'm trying to get some values from a dialog. I have an activity called Splash that shows a dialog with a form. It displays the form wel but i gives me an error when i try to get that values (nullpointer).
I have the folowing code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//datos objeto
nombre = (TextView) findViewById(R.id.tv_nombre);
padre = (TextView) findViewById(R.id.tv_nom_padre);
madre = (TextView) findViewById(R.id.tv_nom_madre);
rgSexo = (RadioGroup) findViewById(R.id.rg_sexo);
fecha_nac = (DatePicker) findViewById(R.id.dp_fecha_nac);
rb_m = (RadioButton) findViewById(R.id.rb_masc);
rb_f = (RadioButton) findViewById(R.id.rb_fem);
perfil = new PerfilCRUD(this);
perfil.open();
//first time
if (perfil.primeraVez()){
//muestro el popup
Log.v(TAG, "prim vez");
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_perfil);
dialog.setTitle("Datos del usuario");
dialog.show();
Button botOk = (Button) dialog.findViewById(R.id.b_ok);
botOk.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Log.v(TAG, "boton ok");
Log.v(TAG, nombre.getText().toString());
}
});
It gives me the error when i try to get the nombre.getText().toString() value. It happens that with all variables/controls values. It forces to close the application.
Thanks!!
Its because nombre is in your original layout. Either call a TextView in the layout that you inflate in the dialog or create a class level variable String name = ""; then in
if (perfil.primeraVez()){
name = nombre.getText().toString();
then you can use name in your dialog