2012-02-08 60 views
2

我正在使用片段和兼容性支持构建选项卡式应用程序。Android - 使用按钮控制片段内的列表视图

在其中一个选项卡中,我想要一个listview。在这个相同的选项卡中,我必须按钮来更改列表视图项(可以说每个按钮都有一个分配的字符串数组)。 我不知道如何做到这一点。我已经构建了按钮的布局和clicklisteners(这些工作是因为我用吐司消息测试过)。

你能给我一个提示吗?我会在哪里放置listview代码?在OnClick上? OnCreateView?

对不起,我没有太多的android经验。

这是我的片段代码。

public class TabFragment6 extends ListFragment { 
/** (non-Javadoc) 
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) 
*/ 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    if (container == null) { 
     // We have different layouts, and in one of them this 
     // fragment's containing frame doesn't exist. The fragment 
     // may still be created from its saved state, but there is 
     // no reason to try to create its view hierarchy because it 
     // won't be displayed. Note this is not needed -- we could 
     // just run the code below, where we would create and return 
     // the view hierarchy; it would just never be used. 
     return null; 
    } 



    LinearLayout theLayout = (LinearLayout)inflater.inflate(R.layout.tab_frag6_layout, container, false); 

    // Register for the Button.OnClick event GUITARRA 
    Button bShopGuit = (Button)theLayout.findViewById(R.id.buttonshopguit); 
    bShopGuit.setOnClickListener(new View.OnClickListener() {   //abre setonclicklistener(
     @Override 
     public void onClick(View v) { 
      Toast.makeText(TabFragment6.this.getActivity(), "GUITAR button clicked", Toast.LENGTH_LONG).show(); 

     } 
    });                 //fecha) 

    // Register for the Button.OnClick event BAIXO 
      Button bShopBass = (Button)theLayout.findViewById(R.id.buttonshopbass); 
      bShopBass.setOnClickListener(new View.OnClickListener() {   //abre setonclicklistener(
       @Override 
       public void onClick(View v) { 
        Toast.makeText(TabFragment6.this.getActivity(), "BASS button clicked", Toast.LENGTH_LONG).show(); 
       } 
      });                 //fecha) 

    return theLayout; 
} 

}

回答