[英]Passing an int value from class to another and storing it in an array in Java
[英]Passing a Int value to another class
我在listview中有四個組,每個組都有四個要加載到webView中的URL。 當用戶選擇要轉到的網址時,我會像這樣設置一個值;
if (position == 0) webb = webb +2;
{
Intent intent = new Intent(this, official.class);
startActivity(intent);
}
然后,我打算移動到webView類,在該類中,我給每個url一個這樣的值;
if (webb == 2) mWebView.loadUrl("http://yahoo.com");
if (webb == 3) mWebView.loadUrl("http://www.bbc.co.uk");
但是,如果我在Official.class中聲明該值,則屏幕保持空白。
我如何根據用戶從列表視圖中進行的選擇獲取此值以傳遞到另一個類。 抱歉,這很難理解。
如果您的問題是如何在活動之間共享數據(例如您的webb值),那么您可能需要查看Intent.putExtra(...)方法。 更多信息:
據我了解您的問題,請嘗試使用其他功能將信息與意圖一起傳遞。 更多信息可以在這里找到
package com.ff.org.l2forums;
import com.ff.org.official;
import com.ff.org.league2.RssActivityc;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Forums extends ListActivity {
public int webb = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "Official Forum", "Claret & Banter", "Bantams Fan Blog", "Official Website", "League Two Football Forum", "Boy From Brazil Blog", "Supporters Trust"};
// Create an ArrayAdapter, that will actually make the Strings above
// appear in the ListView
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, names));
}
// Get the item that was clicked
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
if (position == 0) webb = webb +2; {
Intent intent = new Intent(this, official.class);
startActivity(intent);}
if (position == 9) {
String url = "http://bcafc.livewwware.co.uk/index.php";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
if (position == 1) {
String url = "http://www.claretandbanter.com/forum.php";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
public class official extends Activity {
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browser1);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
if (webb == 2) mWebView.loadUrl("http://bcafc.livewwware.co.uk/viewforum.php?f=7&sid=009c462b00069f307ef6dcd09e747f7c");
if (webb == 3) mWebView.loadUrl("http://www.bbc.co.uk");
mWebView.setWebViewClient(new HelloWebViewClient());
}
}
class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.