2011-11-25 151 views
0

我有三個2選項卡TabA和TabB。 TabA顯示值正確。現在在Tab2我有ListView顯示值。每當我單擊listitem值時,必須將值傳遞給下一個活動,並且每次單擊listItem時都必須基於該值創建新選項卡,則應該生成該選項卡。我的問題是在listitem intent = new Intent()的click事件中不受支持。我的代碼如下選項卡,列表視圖和意圖

public class FriendLists extends TabActivity { 
     //Declarations 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.friendtab); 

     connection=XMPPLogic.getInstance().getConnection(); 
     Resources res = getResources(); // Resource object to get Drawables 
     TabHost tabHost = getTabHost(); // The activity TabHost 
     TabHost.TabSpec spec; // Reusable TabSpec for each tab 
     Intent intent; // Reusable Intent for each tab 

      lv=(ListView) findViewById(R.id.footerlist); 

     adapter = new ArrayAdapter<String>(this,R.layout.listitems,R.id.list_content, my_own_listarray); 
     lv.setAdapter(adapter); 
     //end of listing friends 
     final AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 

      @Override 
      public void onItemClick(AdapterView<?> parent, View v, int position, long id){ 
     //problem is here when i try to put intent and pass String S it creates a problem      
       String S=group.get(position).toString(); 

//    spec = tabHost.newTabSpec(S).setIndicator(S, 
//      res.getDrawable(R.drawable.tab_friendlist)) 
//     .setContent(null); 
//    tabHost.addTab(spec); 

       // set the message to display 
       alertbox.setMessage(S).show(); 

      }}); 

     // Create an Intent to launch an Activity for the tab (to be reused) 
     intent = new Intent().setClass(this,Friends.class); 

     // Initialize a TabSpec for each tab and add it to the TabHost 
     spec = tabHost.newTabSpec("friendlist").setIndicator("Friend List", 
          res.getDrawable(R.drawable.tab_friendlist)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.setCurrentTab(0); 

     tabHost.addTab(tabHost.newTabSpec("onlinefriends").setIndicator("Online Friends", 
       res.getDrawable(R.drawable.tab_friendlist)) 
       .setContent(R.id.footerlist)); 
    } 

} 
+0

你是什麼意思的「不支持」?它不編譯或它會產生異常? – Hicham

+0

構造函數Intent(new AdapterView.OnItemClickListener(){},類)未定義...這是當我聲明intent = new Intent(this,destination.class)時顯示的消息.. – Shalawei

回答

0

從一個匿名內部類裏面,你不能指代被宣佈在它之外的非最終局部變量(intent)。

我假設你只是需要一個臨時變量來保存你想要傳遞給setContent的意圖。只要在裏面聲明onItemClick

Intent intent = new Intent(/* do your thing */); 
spec = tabHost.newTabSpec(S).setIndicator(S, 
      res.getDrawable(R.drawable.tab_friendlist)) 
     .setContent(intent); 
tabHost.addTab(spec); 
+0

感謝您的回覆,但我試過這個,但創建一個問題...構造函數Intent(new AdapterView.OnItemClickListener(){},類)未定義 – Shalawei

+0

檢查出[Intent']的構造函數(http://developer.android.com/reference /android/content/Intent.html#Intent())。您正在傳遞不受支持的參數。 – mhelvens

+0

謝謝我解決了這個問題.. – Shalawei

相關問題