开发者

How can i display a listview by clicking on a button?

I've some buttons on main.xml layout. When i click on a button it should display listview. 开发者_开发技巧How can i implement this? Where can i find a best example for this?


You will want to tie your button click to your View. In this case a list. The below code should be something similar to what you have in your class.

Button Button1 = (Button) findViewById(R.id.MyView1);
    MyStuff.setOnClickListener(new Button.OnClickListener() {

    public void onClick(View z) {
//You will also have to understand intents
        Intent myIntent = new Intent(z.getContext(), MyStuff.class);
        startActivityForResult(myIntent, 0);     
    }
});

Assuming your XML etc are set up properly this should get you on the right track.

Show some of your code and people will be able to exactly pinpoint your problem.

You can check out this SO post which kinda covers the same thing and may get you started. The accepted answer should tie up any loose ends.

Button Click to List View


Can you give some more details? Do you want to have the listview popup as a dialog box or a new activity? If it's a new activity you do what harper89 suggested above.

If it is a popup dialog, instead of creating a new intent in the onClick(View view) you would have to inflate the listview layout from another layout xml file, create the adapter, then set the listview to that adapter.


The ListViewis a bit tricky, since you need to add an Adapter to it, that takes the items that should be displayed.

A good starter for ListViews is the developer resource for this topic.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜