开发者

Show specific xml layouts with only one java class

I had a very similar problem before where i wanted to show random xml layouts. I've done that - with lots of help by Ben Williams - with a class named DisplayRandomPage.class and I had a main.xml layout with four buttons.

That's the code of the Main.class:

    switch (view.getId()) {
case R.id.first_button:
    startActivity(new Intent(this, FirstPage.class));
    break;
case R.id.second_button:
    startActivity(new Intent(this, SecondPage.class));
    break;
case R.id.third_button:
    startActivity(new Intent(this, ThirdPage.class));
    break;
case R.id.random_button:
    Intent intent = new Intent(this, DisplayRandomPage.class);
    startActivity(intent);

and this is in the DisplayRandomPage.class:

    public class DisplayRandomPage extends Activity {

    private Integer [] mLinearLayoutIds = { 
    R.layout.one
    R.layout.two,
    R.layout.three
    };

    public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); 


Random random = new java.util.Random();
int rand = random.nextInt(3);

setContentView(mLin开发者_Python百科earLayoutIds[rand]);
        }
}

What i'd like to do is creating a DisplaySpecificPage.class. Above I've shown my main.class with the switch-case-clause. So when i click on the first button, it will start the FirstPage.class, clicking the second, it will start SecondPage.class, and so on. So for each xml-file i have to create a new java-class although the different java-classes do all the same. Only the xml-files are different. So i'd like to put something like this:

pseudo-code:

    case R.id.first_button:
startActivity(new Intent(this, DisplaySpecificPage.class)) with R.layout.first_page;
break;

how do i pass the ID from the layout (R.layout.first_page) on?


You should change your switch statement to

final Intent intent = new Intent(this, DisplaySpecificPage.class);
switch (view.getId()) {
    case R.id.first_button:
        intent.putExtra("mylayout", R.layout.one);
        break;
    case R.id.second_button:
        intent.putExtra("mylayout", R.layout.two);
        break;
    case R.id.third_button:
        intent.putExtra("mylayout", R.layout.three);
        break;
    case R.id.random_button:
        intent.putExtra("mylayout", randomNumber);
        startActivity(intent);
}
startActivity(intent);

This way you'd start the same activity no matter which button would be pressed, and inside the DisplaySpecificPage activity's onCreate method you should set the content to this passed layout:

final int layout = getIntent().getIntExtra("mylayout", R.layout.one);
setContentView(layout);

The code above passes an extra parameter to the intent when starting the DisplaySpecificPage activity, with the name: "mylayout".

Inside the DisplaySpecificPage class' onCreate method you just retrieve this extra parameter using the getIntExtra method of the passed intent (getIntent() will return it for you), and you set the content view of this DisplaySpecificPage activity to the passed layout by setContentView(layout).

You should make sure though, to always pass a valid layout identifier to that intent, otherwise you'll get exception when trying to inflate it (so randomNumber should be selected properly).

Update
With adding extras to the Intent you can parametrize your activities.

So using intent.putExtra("paramName", paramValue) you'll pass the paramValue value on the name of paramName to the activity you start by this intent.

You want to start the DisplaySpecificPage activity, but want it to have different layout based on which button you click.
So you create an intent:

final Intent intent = new Intent(this, DisplaySpecificPage.class);

Before starting the activity by calling startActivity(intent), you have to put this extra information inside the intent, so the DisplaySpecificPage activity to know which layout it should set as its content view:

So if you pressed the second button, you want the layout of your new activity to be the one defined by the two.xml inside your res/layout folder. It is referenced by as R.layout.two (which is a static int value).

intent.putExtra("mylayout", R.layout.two);

This line puts the layout's value as an extra into the intent object, and now you can start the activity, the layout reference will be passed to it.

The "mylayout" is a name you choose for your parameter. It can be anything (valid), it will be used inside the DisplaySpecificPage activity to retrieve the layout's reference. It is retrieved by this name:

final int layout = getIntent().getIntExtra("mylayout", R.layout.one);

The getIntExtra method gets the integer value from the intent which has the name "mylayout", and if it's not found, then it will return R.layout.one.

If you want to handle this case (when no parameter with name mylayout is set), you can write

final int layout = getIntent().getIntExtra("mylayout", -1);
if (layout < 0) 
{
    //TODO: no layout reference was passed
}


Here is my final code:

Main.class:

    public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        }
public void change(final View view) {
    Intent intent2 = null;
    final Intent intent = new Intent(this, DisplaySpecificPage.class);
    switch (view.getId()) {
        case R.id.first_button:
            intent.putExtra("mylayout", R.layout.layout1);
            break;
        case R.id.second_button:
            intent.putExtra("mylayout", R.layout.layout2);
            break;
        case R.id.third_button:
            intent.putExtra("mylayout", R.layout.layout3);
            break;
        case R.id.random_button:
           intent2 = new Intent(this, DisplayRandomPage.class);
           startActivity(intent2);
        }
    // if the Random button was not clicked, if-condition is true.
    if (intent2 == null)
            startActivity(intent);
    }
}

DisplaySpecificPage.class:

    public class DisplaySpecificPage extends Activity{
        public void onCreate(Bundle savedInstanceState) {
        final int layout = getIntent().getIntExtra("mylayout", R.layout.one);
        super.onCreate(savedInstanceState); 
        setContentView(layout);
        }
    }

DisplayRandomPage.class:

    public class DisplayRandomPage extends Activity {

private Integer [] mLinearLayoutIds = { 
        R.layout.layout1,R.layout.layout2,R.layout.layout3
        };

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    Random random = new java.util.Random();
    int rand = random.nextInt(mLinearLayoutIds.length);
    setContentView(mLinearLayoutIds[rand]);
    }
}

Big thanks to rekaszeru!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜