简体   繁体   中英

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(Bundle 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!

在第二个活动中,只需检索额外数据。

  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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM