Search code examples
javaandroidandroid-activitytextview

After creating many TextViews and giving them IDs, I am trying to open an activity with their information. How do I do that?


So I can successfully create an android Text View using java and set it a value and an ID. But my problem is that after I create them and gave them values and id, I have no control over them. After clicking on them, I want the new page to have the data that was stored in that Text View.

Here is how I created and set value and ID to my Text Views:

                             textView = new TextView(SellActivity.this);

                            // put the data in the text view
                            textView.setText(data);


                            // give it an id
                            textView.setId(Integer.parseInt(getStrID));


                            //place it nicely under one another
                            textView.setPadding(0, 50, 0, 0);

                            // if clicked any of the textviews, open the offer page
                            textView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    //setGetStrID(getStrID);
                                    Intent intent = new Intent(SellActivity.this, OfferActivity.class);
                                    startActivity(intent);
                                }
                            });

                            // add the text view to our layout
                            linearLayout.addView(textView);

Solution

  • You just have to pass the data to the new intent,

    textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 Intent intent = new Intent(SellActivity.this, OfferActivity.class);
                 intent.putExtra("TextViewContent", textView.getText());
                 startActivity(intent);
            }
    });
    

    Then in onCreate function of OfferActivity.java,

    String s = getIntent().getStringExtra("TextViewContent");
    

    Now the string s will contain the value of your textView.