如何在同一个标​​签中开始新活动?

时间:2012-03-28 05:47:15

标签: android listview tabactivity

我有2个标签,名称为:Tab1,Tab2
class 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);

main.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扩展了ListActvity ,它解析json并在listview中列出结果。 在listview中单击项目时,我想在同一个选项卡中启动新的活动,即在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中编写什么代码来启动此选项卡中的新活动,即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>

Thanx !!!

1 个答案:

答案 0 :(得分:2)

让您的活动用作标签,片段的内容。片段可能有自己的后台。

见链接:

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