R
rellik
Neues Mitglied
- 0
Hallo Leute
Ich habe eine Frage hab folgenden Code
Meine MainActivity
MainActivity Design
Nach jedem Klick auf den Button erstelle ich ein TextView mit Trainingsprogramm und eine zahl die ich hoch zähle nach 4 mal klicken habe ich 4 TextViews. Die Textview hab ich mit einem onClickListener versehen und möchte gerne wenn ich auf den Klicke das der Text z.B Trainingsprogramm 3 im neuen Activity im TextView angezeigt wird. Wie kann ich dies schaffen??
Ich habe eine Frage hab folgenden Code
Meine MainActivity
Code:
package com.example.km4l.myapplication;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static int zahl = 0;
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initListeners();
}
private void initListeners (){
Button btnTraining = (Button) findViewById(R.id.btnTraining);
final LinearLayout linearlayout = (LinearLayout) findViewById(R.id.myLinearLayout);
btnTraining.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//eigenen code
System.out.println("Test");
String text = "Neue Trainingseinheit";
TextView tv = new TextView(context);
linearlayout.addView(tv);
tv.setText(text +zahl);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gotoTraining(view);
}
});
zahl++;
}
});
}
public void gotoTraining (View view){
Intent intent = new Intent(this, TrainingsActivity.class);
intent.putExtra("Zahl", zahl);
startActivity(intent);
}
}
MainActivity Design
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnTraining"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Trainingseinheit hinzufuegen"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/myLinearLayout"
android:layout_width="368dp"
android:layout_height="431dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnTraining">
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Nach jedem Klick auf den Button erstelle ich ein TextView mit Trainingsprogramm und eine zahl die ich hoch zähle nach 4 mal klicken habe ich 4 TextViews. Die Textview hab ich mit einem onClickListener versehen und möchte gerne wenn ich auf den Klicke das der Text z.B Trainingsprogramm 3 im neuen Activity im TextView angezeigt wird. Wie kann ich dies schaffen??