0

this is MainActivity

//class declare
public class MainActivity extends Activity {
        // a lot of code
}

//OnCreate Set
setContentView(R.layout.activity_main);

and Inside having a TableLayout with ID R.id.widget101, TableLayout having a dynamic created LinearLayout.

here is the second activity code,trying to get the MainActivity TableLayout and get all the inside LinearLayout child

//class declare
public class SecondActivity extends Activity {
        // a lot of code
}

//OnCreate Set
setContentView(R.layout.second);  


public void EnableButton(boolean bool){

                View view = LayoutInflater.from(getApplication()).inflate(R.layout.activity_main, null);
                ViewGroup layout = (ViewGroup) view.findViewById(R.id.widget101);
                if (layout != null){
                    Log.d(null,"EnableButton getchild="+layout.getChildCount()+"");

                    Log.d(null,"EnableButton Function");
                }

        }

Problem Occurs here, When the Second Aciticity calling the MainActivity to get the TableLayout Child are return 0

===============================================================

this is how i call the SecondActivity

public class MainActivity extends Activity
    implements OnClickListener {

        //overrides the OnClickListener interface method
    @Override
    public void onClick(View arg0) {

        if(arg0.getId() == R.id.btnWEmas){
            //define a new Intent for the second Activity
            Intent intent = new Intent(this,SecondActivity.class);
            intent.putExtra("From_LocationName",global.From_LocationName);
            intent.putExtra("To_LocationName",global.To_LocationName);
            TextView textview = (TextView) findViewById(R.id.lblPrice);
            intent.putExtra("Price",textview.getText());
            //start the second Activity
            this.startActivity(intent);
        } 
       // still have a lot of code
John Walker
  • 1,121
  • 4
  • 26
  • 68
  • are you destroying MainActivity with finish(); code, when you call second activity? – Sercan Ozdemir Dec 04 '13 at 06:19
  • @SercanOzdemir code updated – John Walker Dec 04 '13 at 06:21
  • why don't you use [Bundle-Extras](http://stackoverflow.com/questions/4233873/how-to-get-extra-data-from-intent-in-android) – Sercan Ozdemir Dec 04 '13 at 06:24
  • @SercanOzdemir Yes,i m using Bundle Extras – John Walker Dec 04 '13 at 06:28
  • Try to send your whole information as string or array and then in your second activity get them as shown in my link above. (getStringExtra). Even in a dirty way you can put your whole information in a String with a split charachter like (#) then in your second activity you can get whole string and split it to your information. – Sercan Ozdemir Dec 04 '13 at 06:30

2 Answers2

0

put this in your first activity

static ViewGroup layout ;  //define it globally
layout = (ViewGroup) view.findViewById(R.id.widget101);  //put it inside the oncreate of main activity

and use this object to second activity

public void EnableButton(boolean bool){

            View view = LayoutInflater.from(getApplication()).inflate(R.layout.activity_main, null);

            if (MainActivity.layout != null){
                Log.d(null,"EnableButton getchild="+MainActivity.layout.getChildCount()+"");

                Log.d(null,"EnableButton Function");
            }

    }
Smogger
  • 553
  • 5
  • 17
  • 4
    static view group is a bad idea may lead to memory leaks. – Raghunandan Dec 04 '13 at 06:33
  • @ShashankAgarwal : This is extremely bad practice. Don't ever advise that using static fields or methods in an Activity is a good thing. If the OP assumes it is acceptable and uses that approach as a normal design pattern for Activities, they will run in to problems at some point in the future. – Squonk Dec 04 '13 at 09:39
0

You should follow the following scenario :

create a Custom class file, which contains the Table layout and its children elements. Create a default Constructor with a context parameter. Please follow below link for more clearity.

https://stackoverflow.com/a/20367698/1944782

Here I have created a custom Toast, You can customize your Table Layout according that.

One more thing, please ignore the use of static variable as much as possible. It leads to memory leak and also crashes the app.

Community
  • 1
  • 1
Karan Maru
  • 991
  • 6
  • 17