2012-03-28 21 views
1

Tab1、Tab2という2つのタブがあります。
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は、JSONを解析し、リストビュー内の結果を一覧ListActvity延びています。 リストビューで項目をクリックすると、同じタブ、つまり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に書く必要がありますどのようなコード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> 

ありがとう!!!

+0

、私は本当にわかりません。しかし、リストビューの 'OnItemClick'リスナーで' startActivity() 'と' Intent'を使うと、現在のタブと同じタブのアクティビティがTab-1自身で開始されると思います。完了したら、アクティビティを 'finish()'するのを忘れないでください。申し訳ありませんが、これは "確かではありません"が、回答が得られるまで、試してみてはいかがですか?ところで、良い質問。 +1 – Exorcist

+0

私はこのコードを書いた: 'Intent testIntent = new Intent(view.getContext()、NextActivity.class); \t \t \t \t startActivity(testIntent); \t \t \t \t finish(); ' 新しいアクティビティが始まりますが、タブの消滅が問題ですか? – captaindroid

+0

これはうまくいかないので、新しいアクティビティをTabActivityに拡張して、当面はこれらのタブをこの新しいアクティビティに再び含めるようにしてください。私はそれがちょっと面白いのでこれを試してみるつもりです、そして、もし私が成功すればあなたに知らせるでしょう。 \ m/ – Exorcist

答えて

関連する問題