2010-07-20 146 views
2

是否可以通過選項卡活動調用listactivity?基本上,我正在開發一個帶有3個選項卡的應用程序,爲此我使用Tabactivity。而且,在其中一個選項卡中,我想要一個listview,所以我從listactivity派生出來。從android中的tabactivity調用listactiviy

現在我想要在列表視圖中確定點擊事件。我錯過了什麼嗎?

public class Tabissue extends TabActivity 
{ 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    TabHost host = getTabHost(); 

    host.addTab(host.newTabSpec("Tab1").setIndicator("Tab1").setContent(new Intent(this,Tab1.class))); 
    host.addTab(host.newTabSpec("Tab2").setIndicator("Tab2").setContent(new Intent(this,Tab2.class))); 
    host.setCurrentTab(1); 
} 
} 

TAB1類

public class Tab2 extends ListActivity 
    { 
    ListView list; 
    @Override 

public void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tab2);  

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 
    HashMap<String, String> map = new HashMap<String, String>(); 
    map.put("Test1","####"); 
    map.put("Test2", "India"); 
    map.put("Time", "India time"); 
    mylist.add(map); 
    map = new HashMap<String, String>(); 
    map.put("Test1", "####"); 
    map.put("Test2", "US"); 
    map.put("Time","US time"); 
    mylist.add(map); 
    map = new HashMap<String, String>(); 
    map.put("Test1", "####"); 
    map.put("Test2", "UK"); 
    map.put("Time", "UK Time"); 
    mylist.add(map); 

    ListAdapter mSchedule = new SimpleAdapter( this, 
      mylist, 
      R.layout.row, 
      new String[] 
      { 
       "India", 
       "US", 
       "UK" 
      }, 
      new int[] 
      { 
       R.id.TRAIN_CELL, 
       R.id.FROM_CELL, 
       R.id.TO_CELL, 
      } 
     ); 
    list.setAdapter(mSchedule);  


} 

}

+0

能否請您闡述一下您所面對的確切的問題?現在我沒有看到任何問題。使用ListActivity作爲一個選項卡應該可以開箱即用。 – 2010-07-20 06:58:52

+0

@tg, 我附上了我的應用程序的代碼,它包含2個選項卡和一個列表視圖 – Girish 2010-07-20 09:42:04

+0

有什麼問題?你不能設置一個點擊監聽器,或者ListView完全不出現? – 2010-07-20 10:19:35

回答

2

在你ListActivity設置onItemClickListener:

getListView().setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> av, View v, int position, 
        long id) { 
       // Do your stuff here 
      } 
});