1

I would like to create a new textview to hold the information sent each time the button is clicked. I have the data I want passing to another screen in a textview but each time i try to put new data it overrides this data because it uses the same textView (textview4). I would like to know if there is a way of creating a new text view to hold my data each time the button is clicked. I hope I was clear enough, thanks.

This code is from a class called CreateWorkout.Java

    public void createNewWorkout (View view){
    TextView Exercise1TextView = (TextView) findViewById(R.id.Exercise1TextView);
    EditText  weightEntered = (EditText)findViewById(R.id.WeightLiftedEditText);
    EditText  reps = (EditText)findViewById(R.id.RepsEditText1);
    EditText  sets = (EditText)findViewById(R.id.setsEditText1);

    Intent getWorkoutIntent = new Intent(this, SelectWorkout.class);

    getWorkoutIntent.putExtra("Workout", Exercise1TextView.getText().toString()
            + " " + weightEntered.getText().toString() + "kg"
            + " " + reps.getText().toString() + " reps"
            + " " + sets.getText().toString() + " sets");

    startActivity(getWorkoutIntent);
}

This is where the intent is called. This is from SelectWorkout.Java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.select_workout);

    TextView textView4 = (TextView) findViewById(R.id.textView4);
    textView4.setText(getIntent().getExtras().getString("Workout"));
}

i want to take the data entered here to the next screen. So the exercise name(Leg Press), weight(50), set(3), reps(10) screenshot:

second screen

Ryan159
  • 109
  • 8
  • You might want to have a counter to use in creating a new TextView each time so you don't overwrite the original. Also remember everything is dynamic when you do it this way; so when the activity is finished, you won't have it – Eenvincible Apr 12 '16 at 21:50
  • could you explain more? I'm still trying to learn android – Ryan159 Apr 12 '16 at 21:55
  • Your question is not so clear. Can you put the file names corresponding to the code and how are they connected to each other? Please explain exact flow and if possible, attach screenshot. – Shadab Ansari Apr 12 '16 at 21:58
  • i added some screenshots to help you understand, if you have any advice or ideas please let me know. Thanks – Ryan159 Apr 12 '16 at 22:15

1 Answers1

0

creating a new text view to hold my data each time

Not an exact answer, but this should guide you in the right direction. You could do something like the following:

  • Get your bundle
  • Iterate bundle
  • Create Textviews during iteration and add them to "your_main_layout" which is the id of the main layout inside select_workout

The bundle is the extra data you're sending over from the Intent. The Iterate is going through each item you sent over to this new intent, the create part refers to creating a TextView for each item you sent over. I've supplied links to what I found below.

  • Listing all extras of an Intent

    //Get a bundle:
    for (String key : bundle.keySet()) {
    // This is each value (text) you sent over from the last intent 
        Object value = bundle.get(key);
    //output data to log, so you can see what prints out
        Log.d(TAG, String.format("%s %s (%s)", key, value.toString(), value.getClass().getName()));
    
    //adding a textview to a layout called your_main_layout (which can be a linear layout or something) 
    //with value.toString() as the text
    your_main_layout.addView(createATextView(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, RelativeLayout.ALIGN_PARENT_RIGHT,
                value.toString(), 20, 10, 20));
    }
    
  • How can I add a TextView to a LinearLayout dynamically in Android?

    //method to create view:
    public TextView createATextView(int layout_widh, int layout_height, int align,
    String text, int fontSize, int margin, int padding) {
    
    TextView textView_item_name = new TextView(this);
    
    // LayoutParams layoutParams = new LayoutParams(
    // LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    // layoutParams.gravity = Gravity.LEFT;
    RelativeLayout.LayoutParams _params = new RelativeLayout.LayoutParams(
            layout_widh, layout_height);
    
    _params.setMargins(margin, margin, margin, margin);
    _params.addRule(align);
    textView_item_name.setLayoutParams(_params);
    
    textView_item_name.setText(text);
    textView_item_name.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);
    textView_item_name.setTextColor(Color.parseColor("#000000"));
    // textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
    textView_item_name.setPadding(padding, padding, padding, padding);
    
    return textView_item_name;
    
    }
    
Community
  • 1
  • 1
Petro
  • 3,484
  • 3
  • 32
  • 59
  • could you please explain in more detail about this part: Getting a bundle, iterate, create. I don't really understand it, thanks – Ryan159 Apr 13 '16 at 09:46
  • 1
    The `bundle` is the extra data you're sending over from the `Intent`. The `Iterate` is going through each item you sent over to this new intent, the `create` part refers to creating a `textview` for each item you sent over. – Petro Apr 13 '16 at 17:37