1
我有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並列出結果在列表視圖。 在單擊列表視圖中的項目時,我想在同一個選項卡即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>
感謝名單!
我真的不知道。但我認爲在ListView的OnItemClick監聽器中使用帶'startActivity()'的'Intent'會在同一個選項卡中啓動活動,因爲當前選項卡是Tab-1本身。並且不要忘記在完成後「完成()」活動。對不起,這個「不確定」,但直到你得到答案,你爲什麼不嘗試呢?順便說一句,好問題。 +1 – Exorcist 2012-03-28 05:55:20
我寫了這段代碼: 'Intent testIntent = new Intent(view.getContext(),NextActivity.class); \t \t \t \t startActivity(testIntent); ();' 新的活動開始,但問題是,選項卡消失?? – captaindroid 2012-03-28 06:25:22
因爲這還沒有解決。只要將新的活動擴展到TabActivity,並暫時將這些選項卡再次包含在此新活動中。我會嘗試,因爲它有點有趣,並會讓你知道,如果我取得任何成功。 \ m/ – Exorcist 2012-03-28 12:03:45