2012-03-28 6 views
1

Ich habe 2 Registerkarten mit dem Namen: Tab1, Tab2
Klasse MainActivity erweitert TabActivity.
MainActivity.javaWie starte ich eine neue Aktivität in der gleichen Registerkarte?

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 erstreckt ListActvity die json und listet das Ergebnis in Listenansicht parsen. Wenn ich in listview auf ein Element klicke, möchte ich eine neue Aktivität auf derselben Registerkarte, d. H. In Tab-1, starten.
Wie kann ich das tun ??
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 
     } 

    }); 

} 

}

Hier oben Code:

// 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 
     } 

    }); 

Welcher Code sollte ich in diesem ItemClickListener schreiben neue Aktivität in diesem Register dh Tab- zu starten 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 !!!

+0

Ich bin wirklich nicht sicher. Aber ich denke, dass die Verwendung von 'Intent' mit' startActivity() 'im' OnItemClick'-Listener Ihrer Listenansicht die Aktivität in derselben Registerkarte startet, wie die aktuelle Registerkarte Tab-1 selbst ist. Vergiss nicht, die Aktivität zu beenden, sobald du fertig bist. Sorry für dieses "nicht sicher sein", aber bis du eine Antwort bekommst, warum versuchst du es nicht? Übrigens, gute Frage. +1 – Exorcist

+0

Ich schrieb diesen Code: 'Intent testIntent = neue Absicht (view.getContext(), NextActivity.class); \t \t \t \t startActivity (testIntent); \t \t \t \t finish(); ' Neue Aktivität beginnt, aber das Problem ist, dass Registerkarte verschwindet? – captaindroid

+0

Da dies nicht funktioniert. Erweitern Sie die neue Aktivität einfach auf TabActivity und nehmen Sie diese Registerkarten vorerst wieder in diese neue Aktivität auf. Ich werde es versuchen, weil es irgendwie interessant ist und ich werde Sie wissen lassen, ob ich Erfolg habe. \ m/ – Exorcist

Antwort

Verwandte Themen