开发者

Android views, intents and layout problems

开发者 https://www.devze.com 2023-04-09 14:05 出处:网络
This is my first question, but I\'ve been reading a lot of posts in here trying to get this right. But I just can\'t figure it out! So I\'m hoping for assistance!

This is my first question, but I've been reading a lot of posts in here trying to get this right. But I just can't figure it out! So I'm hoping for assistance!

I'm working on a Android application from where I enter a searchphrase in a autocomplete field. From here I would like the application to change to another layout xml file which contains a listview of the results. When you click a item, it will go to yet another layout xml file.

I got all the http posts and functions ready and working, but the thing that gives me a headache is the switching l开发者_开发知识库ayout part. I been trying this so far:

setContentView(R.layout.newlayout);

Which works the way it should, it does change the layout but of course with problems. If I press the backbutton on my phone, it just closes the app instead of going back to the previous layout. If I create a back button and set it's onclick to do a

setContentView(R.layout.previouslayout); 

it goes back to the other layout, but it does not preserve the input data in the searchform, furthermore it does not reload the function for fetching the tags for the autocomplete. BIG problem!

I'm guessing, that I have to use some kind of intent, or activity manager but I don't know how that works, and how would I send data along with the start of a new activity for my list view and product view?


To show another layout you should use call startActivity from within your activity. To put data along the call use putExtra:

    int value = 1;  
    Intent i = new Intent(this, Test.class);  
    i.putExtra("var", value);
    startActivity(i);  

You should also create a class for each layout and use setContentView to choose the correct layout:

    public class Test extends Activity{
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            int var2 = (int)getIntent().getExtras().get("var");
            setContentView(R.layout.newlayout);
            ...
        }
    }


Despite using intent or another activity, you can also use ViewFlipper.

Example http://www.androidpeople.com/android-viewflipper-example

0

精彩评论

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