1

Ich verwende Intents, um Ansichten in meiner einfachen, 2-Ansicht Basketball-Scorekeeping-App zu wechseln. Die erste und Hauptseite ist die Seite, auf der die Punkte gespeichert werden, und die zweite Seite ist diejenige, die eine 5-Spieler-Aufstellung enthält (5 EditTexts). Alles funktioniert, aber das Ergebnis und die Aufstellung behalten ihre Informationen nicht bei, nachdem die Ansicht gewechselt wurde. Zum Beispiel wird der Punktestand auf 7-0 gesetzt, der Benutzer geht zur Lineup-Seite und aktualisiert sie mit Spielernamen, dann kehrt der Benutzer zu der Punkthaltungsseite zurück und der Punktestand wird 0-0. Ich verstehe, dass Fragmente das lösen können, aber ich weiß nicht, wie man Fragmente anwendet (obwohl ich die Dokumentation mehrmals gelesen habe), so wäre die beste Hilfe, die ich geben könnte, ein spezifisches Beispiel dafür, was ich ändern muss. Mein Code ist unten veröffentlicht und vielen Dank für die Hilfe.Information speichert nicht nach Ansicht wechseln mit Intents (Android)

//////////MainActivity.java (score keeping view)////// 

package com.example.ryan.basketballscorekeeper; 

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 android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.ViewGroup; 
import android.content.Intent; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 

    Button lineupButton= (Button) findViewById(R.id.lineup_button); 

     lineupButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(MainActivity.this,  Main2Activity.class); 
       startActivity(intent); 
      } 

     }); 
    TextView textView = (TextView) findViewById(R.id.homeTeamScore); 
    textView.setText(homeScore+""); 
    TextView textView2 = (TextView) findViewById(R.id.awayTeamScore); 
    textView.setText(awayScore+""); 



} 


int homeScore=0; //home team score 
int awayScore=0; //away team score 
String awayScoreString=awayScore+""; 
String homeScoreString=homeScore+""; 



public void onSaveInstanceState(Bundle savedInstanceState) { 
    super.onSaveInstanceState(savedInstanceState); 
} 

public void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onSaveInstanceState(savedInstanceState); 
} 

public void onStart(Bundle savedInstanceState){ 
    setContentView(R.layout.activity_main); 
    onRestoreInstanceState(savedInstanceState); 
} 

public void onPause (Bundle savedInstanceState){ 
    onSaveInstanceState(savedInstanceState); 
} 
public void onDestroy (Bundle savedInstanceState){ 
    onSaveInstanceState(savedInstanceState); 
} 

public void lineupsView(View view){ 
    Intent intent = new Intent(this, Main2Activity.class); 
    setContentView(R.layout.lineups); 

} 

public void mainView(View view){ 
    setContentView(R.layout.activity_main); 
} 

    ///////////////beginning of score-updating code//////////////// 

public void updateStrings(){ 
    homeScoreString=homeScore+""; 
    awayScoreString=awayScore+""; 
} 

public void threePointerAway(View view){ 
    TextView textView = (TextView) findViewById(R.id.awayTeamScore); 
    awayScore+=3; 
    updateStrings(); 
    textView.setText(awayScoreString); 
} 
public void threePointerHome(View view){ 
    TextView textView = (TextView) findViewById(R.id.homeTeamScore); 
    homeScore+=3; 
    updateStrings(); 
    textView.setText(homeScoreString); 
} 
public void twoPointerHome(View view){ 
    TextView textView = (TextView) findViewById(R.id.homeTeamScore); 
    homeScore+=2; 
    updateStrings(); 
    textView.setText(homeScoreString); 
} 
public void twoPointerAway(View view){ 
    TextView textView = (TextView) findViewById(R.id.awayTeamScore); 
    awayScore+=2; 
    updateStrings(); 
    textView.setText(awayScoreString); 
} 
public void freeThrowHome(View view){ 
    TextView textView = (TextView) findViewById(R.id.homeTeamScore); 
    homeScore+=1; 
    updateStrings(); 
    textView.setText(homeScoreString); 
} 
public void freeThrowAway(View view){ 
    TextView textView = (TextView) findViewById(R.id.awayTeamScore); 
    awayScore+=1; 
    updateStrings(); 
    textView.setText(awayScoreString); 
} 

public void reset(View view){ 
    TextView awayView = (TextView) findViewById(R.id.awayTeamScore); 
    TextView homeView = (TextView) findViewById(R.id.homeTeamScore); 
    homeScore=0; 
    awayScore=0; 
    updateStrings(); 
    awayView.setText(awayScoreString); 
    homeView.setText(homeScoreString); 
} 


} 
///////activity_main.xml (score keeping xml file)////// 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.ryan.basketballscorekeeper.MainActivity" 

>

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:src="@drawable/bballcourt" 
    android:scaleType="centerCrop" 
    /> 


<LinearLayout 

    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    > 
    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:layout_gravity="center" 
     android:orientation="vertical" 
     > 

     <TextView 
      android:textColor="#ffffff" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Home Team" 
      android:layout_margin="10dp" 
      android:layout_gravity="center" 
      android:textSize="15dp" 
      /> 
     <TextView 
      android:textColor="#ffffff" 
      android:id="@+id/homeTeamScore" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="0" 
      android:textSize="50dp" 
      android:layout_gravity="center" 
      /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="+3 Points" 
      android:background="#ffbf80" 
      android:layout_gravity="center" 
      android:onClick="threePointerHome" 
      android:layout_marginTop="20dp" 
      android:textSize="20dp" 
      android:padding="10dp" 
      /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="+2 Points" 
      android:background="#ffbf80" 
      android:layout_gravity="center" 
      android:onClick="twoPointerHome" 
      android:layout_marginTop="20dp" 
      android:textSize="20dp" 
      android:padding="10dp" 
      /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Free Throw" 
      android:background="#ffbf80" 
      android:layout_gravity="center" 
      android:onClick="freeThrowHome" 
      android:layout_marginTop="20dp" 
      android:textSize="17dp" 
      android:padding="10dp" 
      /> 
    </LinearLayout> 

    <View 
     android:layout_width="5dp" 
     android:layout_height="340dp" 
     android:background="#d9d9d9" 
     android:layout_marginTop="40dp" 

     /> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:layout_gravity="center" 
     android:orientation="vertical" 
     > 

     <TextView 
      android:textColor="#ffffff" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Away Team" 
      android:layout_margin="10dp" 
      android:layout_gravity="center" 
      android:textSize="15dp" 
      /> 
     <TextView 
      android:textColor="#ffffff" 
      android:id="@+id/awayTeamScore" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="0" 
      android:textSize="50dp" 
      android:layout_gravity="center" 
      /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="+3 Points" 
      android:background="#ffbf80" 
      android:layout_gravity="center" 
      android:onClick="threePointerAway" 
      android:layout_marginTop="20dp" 
      android:textSize="20dp" 
      android:padding="10dp" 
      /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="+2 Points" 
      android:background="#ffbf80" 
      android:layout_gravity="center" 
      android:onClick="twoPointerAway" 
      android:layout_marginTop="20dp" 
      android:textSize="20dp" 
      android:padding="10dp" 
      /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Free Throw" 
      android:background="#ffbf80" 
      android:layout_gravity="center" 
      android:onClick="freeThrowAway" 
      android:layout_marginTop="20dp" 
      android:textSize="17dp" 
      android:padding="10dp" 
      /> 
    </LinearLayout> 

</LinearLayout> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="reset" 
    android:textAllCaps="true" 
    android:layout_centerHorizontal="true" 
    android:layout_alignParentBottom="true" 
    android:padding="15dp" 
    android:layout_marginBottom="40dp" 
    android:textSize="20dp" 
    android:background="#ffbf80" 
    android:onClick="reset" 

    /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Lineups" 
    android:textAllCaps="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentBottom="true" 
    android:padding="15dp" 
    android:layout_margin="10dp" 
    android:textSize="20dp" 
    android:background="#ff8000" 
    android:id="@+id/lineup_button" /> 

</RelativeLayout> 


///////Main2Activity.java (lineup view)////// 


    package com.example.ryan.basketballscorekeeper; 

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 Main2Activity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main2); 

    Button mainButton= (Button) findViewById(R.id.main_button); 

    mainButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(Main2Activity.this, MainActivity.class); 
      startActivity(intent); 
     } 

    }); 

    EditText editText1 = (EditText) findViewById(R.id.one); 
    one=editText1.getText().toString(); 
    EditText editText2 = (EditText) findViewById(R.id.two); 
    two=editText1.getText().toString(); 
    EditText editText3 = (EditText) findViewById(R.id.three); 
    three=editText1.getText().toString(); 
    EditText editText4 = (EditText) findViewById(R.id.four); 
    four=editText1.getText().toString(); 
    EditText editText5 = (EditText) findViewById(R.id.five); 
    five=editText1.getText().toString(); 

} 
String one,two,three,four,five; //this represents the five players 
} 

////////activity_main2.xml (lineup view xml file)/////// 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.ryan.basketballscorekeeper.Main2Activity" 
android:orientation="vertical" 
> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:src="@drawable/bballcourt" 
     android:scaleType="centerCrop" 
     /> 

    <TextView 
     android:id="@+id/lineups" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#ffffff" 
     android:text="Lineups" 
     android:textSize="30dp" 
     android:padding="30dp" 
     /> 

    <EditText 
     android:textColor="#ffffff" 
     android:id="@+id/one" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textNoSuggestions" 
     android:maxLines="1" 
     android:layout_below="@id/lineups" 
     /> 
    <EditText 
     android:textColor="#ffffff" 
     android:id="@+id/two" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textNoSuggestions" 
     android:maxLines="1" 
     android:layout_below="@id/one" 
     /> 
    <EditText 
     android:textColor="#ffffff" 
     android:id="@+id/three" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textNoSuggestions" 
     android:maxLines="1" 
     android:layout_below="@id/two" 
     /> 
    <EditText 
     android:textColor="#ffffff" 
     android:id="@+id/four" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textNoSuggestions" 
     android:maxLines="1" 
     android:layout_below="@id/three" 
     /> 
    <EditText 
     android:textColor="#ffffff" 
     android:id="@+id/five" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textNoSuggestions" 
     android:maxLines="1" 
     android:layout_below="@id/four" 
     /> 
    <Button 
     android:id="@+id/main_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Score" 
     android:textAllCaps="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentBottom="true" 
     android:padding="15dp" 
     android:layout_margin="10dp" 
     android:textSize="20dp" 
     android:background="#ff8000" 

     /> 
    </RelativeLayout> 

</LinearLayout> 
+0

Dies könnte helfen [Werte verloren in früheren Aktivität, wenn ich zurück button] (http://stackoverflow.com/questions/14044588/values-lost-in-previous-activity-when-i-hit-back-button -aber nicht-wann-dieses-finis) – Shubham

Antwort

0

Versuchen Sie, Crate neue Klasse wie unten

public class Score {

public static int homeScore = 0; // Heimmannschaft Punktzahl

public static int awayScore = 0; // Mannschaft punkten

}

und wo immer Sie verwenden homeScore repalce dieser Variablen mit

homeScore -> Score.homeScore

awayScore -> Score.awayScore

und Entfernen Sie die lokalen Score-Variablen in MainActivity

Aktualisieren Sie die Textansichten in onResume() -Methode

Verwandte Themen