Java 使用视图而不是单独的活动应用选项卡

Java 使用视图而不是单独的活动应用选项卡,java,android,android-listview,Java,Android,Android Listview,我在试图找出如何将我当前的选项卡设置转换为使用视图而不是单独活动的选项卡设置时遇到困难。。。我在调用搜索功能时遇到问题,我认为这是由于我创建选项卡的方式造成的 我的主要启动程序活动是创建选项卡的公共类菜单扩展了TabActivity intent = new Intent().setClass(this, TabGroup1.class); // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHo

我在试图找出如何将我当前的选项卡设置转换为使用视图而不是单独活动的选项卡设置时遇到困难。。。我在调用搜索功能时遇到问题,我认为这是由于我创建选项卡的方式造成的

我的主要启动程序活动是创建选项卡的
公共类菜单扩展了TabActivity

 intent = new Intent().setClass(this, TabGroup1.class);
 // Initialize a TabSpec for each tab and add it to the TabHost
 spec = tabHost.newTabSpec("codes").setIndicator("All Codes",res.getDrawable(R.drawable.ic_tab_codes))
 .setContent(intent);
 tabHost.addTab(spec);
`TabGroup1'对每个选项卡执行以下操作

public class TabGroup1 extends TabGroupActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startChildActivity("Category", new Intent(this,Category.class));
}
}
然后调用
ListActivity
,当单击一个项目时,它会显示其中的内容。另一个意图被创建,然后开始一个新的活动,允许我在用户进入列表时在每个级别上设置选项卡

这是通过以下代码完成的

    public void onListItemClick(ListView parent, View view, int position, long id) {
    Intent intent = new Intent(getParent(), SubCategory.class);
    Cursor cursor = (Cursor) adapter.getItem(position);
    intent.putExtra("CATEGORY", cursor.getString(cursor.getColumnIndex("_id")));
    /*startActivity(intent);*/
    TabGroupActivity parentActivity = (TabGroupActivity)getParent();
    parentActivity.startChildActivity("SubCategory", intent);
}
TabGroupActivity
是我从中找到的一个类,它允许在同一选项卡布局下有多个活动

我正在努力使用视图转换我必须的内容,并使用setContent更改视图

我已经找到了一个例子,但它并没有提供足够的细节让我继续下去。 也发现了一个

有人能告诉我我需要更改什么,以及如何使用我的listactivities设置内容吗


提前谢谢你几件事。。。setContent定义内容,它不会导致选项卡切换。如果要强制更改某个选项卡,请使用
TabHost.setCurrentTab(tabid),否则默认为第一个选项卡,然后是用户选择的任何选项卡

我自己的一个项目中的一个例子(这个项目实际上有四个选项卡,但我删掉了一些选项卡,以便切中要害)。有几种方法可以做到这一点,但我发现最简单的方法是创建一个方法来填充每个选项卡,这样我就可以根据需要刷新一个选项卡,为我需要的任何选项卡调用适当的方法(下面的所有java都包含在TabActivity中)

setupdetailmain.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="5dp" >
            <TextView
                    android:id="@+id/setupheader"
                    android:layout_width="fill_parent"
                    android:layout_height="20dp"
                    android:textSize="15dp" />
            <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="30dp"
                    android:gravity="bottom" />
            <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:padding="5dp" >
            <!-- General Info Tab -->
                    <LinearLayout
                            android:id="@+id/note_tab"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >
                    </LinearLayout>
            <!-- Tool Tab -->
                    <LinearLayout
                            android:id="@+id/offset_tab"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >
                            <ListView
                                    android:id="@+id/list2"
                                    android:layout_width="match_parent"
                                    android:layout_height="0dp"
                                    android:layout_weight="1"
                                    android:drawSelectorOnTop="false" />
                    </LinearLayout>
            </FrameLayout>
    </LinearLayout>
</TabHost>
选项卡填充方法

// ***************************************************************
// Fill the Notes tab
// ***************************************************************
private void notestab() {
    notes = (LinearLayout) findViewById(R.id.note_tab);
    notestxt = new TextView(this);
    notestxt.setText(SETUPINFO_NOTES);
    notestxt.setTextSize(15);
    notes.addView(notestxt);
}

// ***************************************************************
// Fill the Offset tab
// ***************************************************************
private void offsettab() {
    wpccount = 0;
    for (int i = 0; i < 20; i++) {
        if (wpcdesc[i] != null) {
            wpcdisplayhold[wpccount] = wpcid[i] + " - " + wpcdesc[i];
            wpcidhold[wpccount] = wpcid[i];
            wpcdeschold[wpccount] = wpcdesc[i];
            wpccount++;
        }
    }
    wpcdisplay = new String[wpccount];
    for (int i = 0; i < wpccount; i++) {
        wpcdisplay[i] = wpcdisplayhold[i];
    }
    mWPCView = (ListView) findViewById(R.id.list2);
    mWPCView.setAdapter(new ColorizingListAdapter(SetupDisplay.this,
            wpcdisplay, "Offset"));
    registerForContextMenu(mWPCView);
}
//***************************************************************
//填写“备注”选项卡
// ***************************************************************
私有void notestab(){
注释=(线性布局)findViewById(R.id.note_选项卡);
notestxt=新的文本视图(此);
notestxt.setText(SETUPINFO_NOTES);
notestxt.setTextSize(15);
notes.addView(notestxt);
}
// ***************************************************************
//填充“偏移”选项卡
// ***************************************************************
私人无效抵销表(){
wpccount=0;
对于(int i=0;i<20;i++){
如果(wpcdesc[i]!=null){
wpcdisplayhold[wpccount]=wpcid[i]+“-”+wpcdesc[i];
wpcidhold[wpccount]=wpcid[i];
wpcdeschold[wpccount]=wpcdesc[i];
wpccount++;
}
}
wpcdisplay=新字符串[wpccount];
对于(int i=0;i

这使用了一个自定义适配器,但希望它能让您了解如何在选项卡视图中设置列表,而无需执行新的活动。

我很困惑。。。您每次更改选项卡时都会创建新的选项卡当前???Hi@Barak我用一些进一步的代码编辑了我上面的问题,说明了每个活动如何调用下一个活动,以便选项卡位于顶部…它可以工作,但我想从中转换,因为我读到它不是最好的方法,并且ActivityGroup已被弃用。这是我用来创建选项卡的。
// ***************************************************************
// Fill the Notes tab
// ***************************************************************
private void notestab() {
    notes = (LinearLayout) findViewById(R.id.note_tab);
    notestxt = new TextView(this);
    notestxt.setText(SETUPINFO_NOTES);
    notestxt.setTextSize(15);
    notes.addView(notestxt);
}

// ***************************************************************
// Fill the Offset tab
// ***************************************************************
private void offsettab() {
    wpccount = 0;
    for (int i = 0; i < 20; i++) {
        if (wpcdesc[i] != null) {
            wpcdisplayhold[wpccount] = wpcid[i] + " - " + wpcdesc[i];
            wpcidhold[wpccount] = wpcid[i];
            wpcdeschold[wpccount] = wpcdesc[i];
            wpccount++;
        }
    }
    wpcdisplay = new String[wpccount];
    for (int i = 0; i < wpccount; i++) {
        wpcdisplay[i] = wpcdisplayhold[i];
    }
    mWPCView = (ListView) findViewById(R.id.list2);
    mWPCView.setAdapter(new ColorizingListAdapter(SetupDisplay.this,
            wpcdisplay, "Offset"));
    registerForContextMenu(mWPCView);
}