android - 如何在同一选项卡中开始新 Activity ?

标签 android listview tabactivity

我有 2 个标签,分别命名为:Tab1、Tab2
MainActivity 类扩展了 TabActivity。
MainActivity.java

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mTabHost = getTabHost();
    Intent intentTab;
    intentTab = new Intent().setClass(this, Tab1Activity.class);
    mTabHost.addTab(mTabHost.newTabSpec("tab_1").setIndicator("Tab1")
            .setContent(intentTab));

    intentTab = new Intent().setClass(this, Tab2Activity.class);
    mTabHost.addTab(mTabHost.newTabSpec("tab_2").setIndicator("Tab2")
            .setContent(intentTab));



    mTabHost.setCurrentTab(0);

ma​​in.xml

<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" >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp" >
    </FrameLayout>

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="-4dp"
        android:layout_weight="0" />
</LinearLayout>

Tab1Activity extends ListActvity 解析 json 并在 ListView 中列出结果。 在 ListView 中单击项目时,我想在同一选项卡中开始新的 Activity ,即在 Tab-1 中。
我该怎么做??
Tab1Activity.java

    public class Tab1Activity extends ListActivity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab1);

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String,     String>>();

    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting Array of Root
        root = json.getJSONArray(TAG_ROOT);

        // looping through All root
        for (int i = 0; i < root.length(); i++) {
            JSONObject c = root.getJSONObject(i);

            // Storing each json item in variable

            String name = c.getString(TAG_NAME);
            String town = c.getString(TAG_TOWN);
            String totalRating = c.getString(TAG_TOTALRATING);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value

            map.put(TAG_NAME, "Name: " + name);
            map.put(TAG_TOWN, "Town: " + town);
            map.put(TAG_RATING, "Rating: " + totalRating);

            // adding HashList to ArrayList
            myList.add(map);

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    /**
     * Updating parsed JSON data into ListView
     * */
    ListAdapter adapter = new SimpleAdapter(this, myList,
            R.layout.list_item, new String[] { TAG_NAME, TAG_TOWN,
                    TAG_RATING }, new int[] { R.id.name, R.id.address,
                    R.id.rating });

    setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();

    // Launching new screen on Selecting Single ListItem
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            //what code should i write to start new activity in this tab i.e Tab1
        }

    });

}

在上面的代码中:

    // Launching new screen on Selecting Single ListItem
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            //what code should i write to start new activity in this tab i.e Tab1
        }

    });

我应该在此 ItemClickListener 中编写什么代码以在此选项卡中启动新 Activity ,即 Tab-1 ???

tab1.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/background"
  android:orientation="vertical" >


  <ListView
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:choiceMode="singleChoice"
    android:drawSelectorOnTop="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:scrollbars="vertical" />

 </LinearLayout>

list_item.xml

  <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >



    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:paddingTop="6dip"
        android:textColor="#43bd00"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/town"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:textColor="#acacac" >
    </TextView>

    <TextView
        android:id="@+id/rating"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:textColor="#acacac" >
    </TextView>
</LinearLayout>

谢谢!!!

最佳答案

让您的 Activity 用作选项卡、 fragment 的内容。 Fragment 可能有自己的返回堆栈。

查看链接:

http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/

关于android - 如何在同一选项卡中开始新 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9901785/

相关文章:

Android appwidget 在运行时不启动服务

android - 如何显示pdf内容?

android - Proguard 返回错误代码 1,错误找不到引用的类 android.support.v4.app

Android 热敏打印机不接受对齐字节

Python GUI 在整个过程完成之前不会更新

android - ListView onItemClickListener 不起作用 Android

c# - 单个 ListView 中的多个列表

java - 关闭/重新启动 TabHost 中的 Activity

android - 如何从父 Activity/选项卡 Activity 调用 Activity 组内子 Activity 的功能?

android 在 Activity 内更改 TabWidget 外部的 textview