1

我的應用程序是一個新聞應用程序。由5個標籤或更多(這將是根據每個用戶的要求設置)。我如何通過動態URL到每個網頁視圖活動在多標籤,在每個選項卡1個Web視圖 - Android應用

當應用程序啓動我動態創建5個選項卡,並創建一個web視圖爲每個標籤的意圖,我只需要通過URL爲每個標籤的意圖,這裏是我的代碼。

這是主要的活動

package news.mobile; 

import android.app.Activity; 
import android.os.Bundle; 
import android.app.TabActivity; 
import android.widget.TabWidget; 
import android.widget.TabHost; 
import android.widget.TabHost.TabSpec; 
import android.content.Intent; 

public class NewsMobile extends TabActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     TabHost tabHost = getTabHost(); 
     // Here I create the tabs dynamically. 
     for(int i=0;i<5;i++){ 
     tabHost.addTab(
       tabHost.newTabSpec("tab"+i) 
       .setIndicator("Politics") 
        // I need to pass an argument to the WebviewActivity to open a 
        // specific URL assume it is "http://mysite.com/?category="+i 
       .setContent(new Intent(this, WebviewActivity.class))); 
     } 
     tabHost.setCurrentTab(0); 
    } 
} 

這是我的網頁視圖造物主活動

package news.mobile; 

import android.app.Activity; 
import android.os.Bundle; 
import android.webkit.WebView; 

public class WebviewActivity extends Activity { 
    WebView browse; 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     browse=new WebView(this); 
     setContentView(browse); 
       // I need the following line to read an argument and add it to the url 
     browse.loadUrl("http://mysite.com/?category="); 
    } 
} 

回答

1

您可以使用捆綁這樣

Bundle bundle = new Bundle(); 
String url = "http://www.google.com"; 
bundle.putString("urlString", url); 
Intent intent = new Intent(ThisActivity.this, NewActivity.class); 
intent.putExtras(bundle); 
startActivity(intent); 
+0

確定截獲此信息現在

所以開始的WebView活動當我試圖讀它在它給錯誤 browse.loadUrl的WebviewActivity(savedInstanceState.getString(「URL」))我在NewsMobile活動設置這個.. 現在; 你可以請你的解決方案上面的代碼? 我是一個初學者Java開發人員,我最初的PHP/Python開發 – Shehabix 2012-04-18 23:55:59

+0

@Shehabix根據StackOverflow的政策,這是一個單獨的問題,你問一個問題,在這裏發表評論對我來說,我會檢查這個問題,留下一個答案。 – 2012-04-19 11:26:07

+0

:: Pass =發送和接收,所以這不是另一個問題。 。儘管我發現它在別的地方。 – Shehabix 2012-04-20 18:16:06

1

在情況下,它可以幫助任何人,這裏是額外信息@Shehabix問了:如圖所示接受的答案

Bundle bundle = new Bundle(); 
String url = "http://www.google.com"; 
bundle.putString("urlString", url); 
Intent intent = new Intent(ThisActivity.this, NewActivity.class); 
intent.putExtras(bundle); 
startActivity(intent); 

這裏是如何在網頁視圖活動

public class MyWebView extends Activity { 

    private WebView webView; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.asset_web_view); 
     //get url stored in intent 
     String url = super.getIntent().getExtras().getString("urlString"); 
     loadUrlInWebView(url); 
    } 

    private void loadUrlInWebView(String url){ 
     webView = (WebView) findViewById(R.id.mywebview); 
     webView.setWebViewClient(new WebViewClient()); 
     webView.loadUrl(url); 
    } 
} 
相關問題