Android 如何为每个按下的按钮显示不同的列表视图

Android 如何为每个按下的按钮显示不同的列表视图,android,listview,button,android-listview,Android,Listview,Button,Android Listview,我有一行要填充listview的按钮。看起来是这样的: 按钮1 |按钮2 |按钮3 |按钮4 清单项目1 清单项目2 清单项目3 如果我按下按钮1,我将显示一个视图,如果我选择按钮2,我将显示另一个视图。我让listview在没有选择按钮的情况下工作。但是,我想选择一个按钮,并根据所选按钮填充一个列表视图。这只是我的第二个应用程序,所以我可能走错了方向 这是我的布局文件: 我想选择“章节”按钮并填充列表按钮,或者选择“音乐会”并填充列表视图 我不知道在哪里或如何设置按下的按钮。以下是我

我有一行要填充listview的按钮。看起来是这样的:


按钮1 |按钮2 |按钮3 |按钮4 清单项目1 清单项目2 清单项目3 如果我按下按钮1,我将显示一个视图,如果我选择按钮2,我将显示另一个视图。我让listview在没有选择按钮的情况下工作。但是,我想选择一个按钮,并根据所选按钮填充一个列表视图。这只是我的第二个应用程序,所以我可能走错了方向

这是我的布局文件:


我想选择“章节”按钮并填充列表按钮,或者选择“音乐会”并填充列表视图

我不知道在哪里或如何设置按下的按钮。以下是我尝试失败的地方:

     public class ChapterListFragment extends ListFragment {
private static final boolean VERBOSE = true;
private ArrayList<Chapters> mChapters;
private static final String TAG = "ChapterListFragment";

private Button mChaptersButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    if (VERBOSE) Log.v(TAG, "+++ onCreate +++");
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_improvisation);
    mChaptersButton = (Button)v.findViewById(R.id.chapters);
    mChaptersButton.setPressed(true);

    getActivity().setTitle(R.string.chapters_title);
    mChapters = ChapterList.get(getActivity()).getChapters();

    ChapterAdapter adapter = new ChapterAdapter(mChapters);
    setListAdapter(adapter);        

}
公共类ChapterListFragment扩展ListFragment{
private static final boolean VERBOSE=true;
私人ArrayList mChapters;
私有静态最终字符串TAG=“ChapterListFragment”;
专用按钮mChaptersButton;
@凌驾
创建时的公共void(Bundle savedInstanceState){
if(VERBOSE)Log.v(标记“+++onCreate++”);
super.onCreate(savedInstanceState);
setContentView(右布局、活动和即兴创作);
mChaptersButton=(按钮)v.findviewbyd(R.id.chapters);
mChaptersButton.setPressed(true);
getActivity().setTitle(R.string.chapters\u title);
mChapters=ChapterList.get(getActivity()).getChapters();
ChapterAdapter=新ChapterAdapter(mChapters);
setListAdapter(适配器);
}

这不起作用,因为我无法在ListFragment中使用SetContentView。此外,按钮没有焦点。如果您有任何建议,将不胜感激。

创建通用listview适配器,并在每次单击按钮时使用不同的数据集填充listview。请确保调用您的
适配器。clear()
这样listview中的旧数据将消失。我希望您能理解。对于
setContentView,ListFragment和Fragment父对象的非`方法。您应该有一个具有布局的活动,并在其中设置ListFragment。请检查此链接

您可以使用android选项卡布局来显示不同的列表/pages@itsrajesh4uguys使用android选项卡布局有什么好处?您可以根据需要在页面中绑定不同的列表视图。@itsrajesh4uguys谢谢,您能指出吗告诉我有什么好的例子吗?当然,我会写一个示例,并提供给你他也可以使用片段,点击按钮,使用fragmentManager替换片段。这样做的好处是,如果他在其他任何地方需要相同的列表,他可以直接使用它。
     public class ChapterListFragment extends ListFragment {
private static final boolean VERBOSE = true;
private ArrayList<Chapters> mChapters;
private static final String TAG = "ChapterListFragment";

private Button mChaptersButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    if (VERBOSE) Log.v(TAG, "+++ onCreate +++");
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_improvisation);
    mChaptersButton = (Button)v.findViewById(R.id.chapters);
    mChaptersButton.setPressed(true);

    getActivity().setTitle(R.string.chapters_title);
    mChapters = ChapterList.get(getActivity()).getChapters();

    ChapterAdapter adapter = new ChapterAdapter(mChapters);
    setListAdapter(adapter);        

}