2010-05-15 40 views
0

我一直在想如何在點擊ListActivity中的一行時彈出一個webview。點擊時我甚至不能顯示吐司。需要幫助Android ListActivity OnListItemClick與Webviews的錯誤

我還是一位嘗試學習的網絡開發人員。以前感謝。

package com.mf.ar;

import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;

import android.app.Activity; import android.app.AlertDialog; import android.app.ListActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ArrayAdapter; import android.widget.Toast;

public class ListViewer extends ListActivity { private String mJSON; private String [] listRows; private String [] listRowsURI;

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.listview); 

    Bundle the = getIntent().getExtras(); 
    this.mJSON = the.getString("JSON"); 
    Log.i("ListViewIntentJSON", this.mJSON); 

    try { 
JSONObject UrbJSON = new JSONObject(mJSON); 

JSONObject UrbQuery = UrbJSON.getJSONObject("query"); 
int rows = UrbQuery.getInt("row"); 
Log.i("UrbRows", String.format("%d",rows)); 

JSONArray resultArray = UrbJSON.getJSONArray("result"); 

this.listRows = new String[rows]; 
for(int i=0; i<rows; i++) { 
this.listRows[i] = resultArray.getJSONObject(i). 
    getString("business_name").toString(); 
} 
this.listRowsURI = new String[rows]; 
for(int i=0; i<rows; i++) { 
this.listRowsURI[i] = resultArray.getJSONObject(i). 
    getString("business_uri_mobile").toString(); 
} 
    } catch(JSONException e) { 
     e.printStackTrace(); 
    } 

    this.setListAdapter(new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, ListViewer.this.listRows)); 
} 

@Override 
protected void onListItemClick(android.widget.ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 

    String theURI = ListViewer.this.listRowsURI[position].toString(); 
    /*String theURI = ListViewer.this.listRowsURI[position].toString(); 
    //String theURI = "http://www.mediafusion.web.id"; 

    WebView webview = new WebView(this); 
    //setContentView(webview); 
    //addContentView(webview, null); 
    webview.getSettings().setJavaScriptEnabled(true); 
    getWindow().requestFeature(Window.FEATURE_PROGRESS); 
    final Activity activity = this; 
webview.setWebChromeClient(new WebChromeClient() { 
    public void onProgressChanged(WebView view, int progress) { 
    // Activities and WebViews measure progress with different scales. 
    // The progress meter will automatically disappear when we reach 100% 
    activity.setProgress(progress * 1000); 
    } 
}); 
webview.setWebViewClient(new WebViewClient() { 
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
    Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
    } 
}); 
    webview.loadUrl(theURI);*/ 

    Toast.makeText(ListViewer.this.getApplicationContext(), theURI, Toast.LENGTH_SHORT); 

} 

}

回答

0

刪除的getApplicationContext()所有出現在你的代碼,事情會更好地爲您。 ListViewer是一個Context - 只需在Toast中使用它即可。

關於WebView,我認爲你會更好地使自己的活動,並從onListItemClick()開始該活動。

+0

我已經刪除了getApplicationContext(),而不是使用ListViewer.this,但仍然是吐司不會顯示:( 無論如何,我遵循你的建議,使webview自己的活動。 – Tista 2010-05-15 16:57:14

+0

我已經完成了你所說的,它像一個魅力一樣工作。感謝您的指點! – Tista 2010-05-15 17:15:15