开发者

Transferring Variables Between Classes In Java

I have created and XML parser that acquires the data in the description tag. I am attempting to pass it to a web view because it has HTML formatting. I am new to Java and I don't really know how to do that.

This is what I have in the first class:

 Intent intent = new Intent(this,Webscreen.class);
 Bundle bundle = new Bundle();
 bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription());
 intent.putExtras(bundle);
 startActivity(intent);

This is what I have so far in the second class:

public static final String URL = "";
private static final String TAG = "WebscreenClass";

/** Called when the activity is first created. */
@Override
public void onCreate(Bund开发者_JAVA百科le savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webscreen);

    if(URL == null && URL == ""){
        WebView webview = (WebView) findViewById(R.layout.webscreen);
        String htmlString = keyDescription;
        webview.loadData(htmlString, "text/html", "utf-8");
    }else{
        String turl = getIntent().getStringExtra(URL);
        Log.i(TAG, " URL = "+turl);

        WebView webview = new WebView(this);
        WebSettings websettings = webview.getSettings();
        websettings.setJavaScriptEnabled(true);
        setContentView(webview);


        // Simplest usage: An exception won't be thrown if there is a page-load error
        webview.loadUrl(turl);
    }
}

I am using the if else statement because I use this class to load web pages as well. Thank you for your help!


In your second activity just retrieve the Extra Data.

  Bundle extras = getIntent().getExtras();


first: Your if-Statment is wrong. It would be "false" at every run. You have to use "||" instead of "&&". The problem is that your URL String could only be "null" or empty.

second: You can get your "keyDescription" with following statment in your second activity:

String keyDescription = savedInstanceState.getString("keyDescription");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜