2014-03-26 22 views
1

我試圖建立一個根據主要活動從SQLite數據庫中的WebView

傳遞的意圖數據我已經實現了代碼,但應用程式經常當從數據庫中提取網址的應用程序中使用URL /部隊關閉。

這裏是我的DBHelper類

package com.snplabs.learncpp; 

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DBHelper extends SQLiteOpenHelper { 

final protected static String DATABASE_NAME="cppreference"; 
public DBHelper(Context context) { 
super(context, DATABASE_NAME, null,2); 
} 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
if (oldVersion >= newVersion) return; 
db.execSQL("DROP DATABASE IF EXISTS " + DATABASE_NAME +";"); 
onCreate(db); 
} 
@Override 
public void onCreate(SQLiteDatabase db) { 

//here is the database definition 
db.execSQL("CREATE TABLE cppref " + 
"(_id INTEGER PRIMARY KEY, link TEXT);"); 
//insert pre-configured records 
db.execSQL("INSERT INTO cppref (_id, link) VALUES(1,'URL_HERE');"); 
db.execSQL("INSERT INTO cppref (_id, link) VALUES(2,'URL_HERE');"); 
db.execSQL("INSERT INTO cppref (_id, link) VALUES(3,'URL_HERE');"); 
db.execSQL("INSERT INTO cppref (_id, link) VALUES(4,'URL_HERE');"); 
db.execSQL("INSERT INTO cppref (_id, link) VALUES(5,'URL_HERE');"); 
    } 


} 

這裏是一個使用DBHelper,以便獲得url類:

package com.snplabs.learncpp; 

import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.TextView; 

public class ViewItem extends Activity{ 
private DBHelper dbhelper=new DBHelper(this); 
private SQLiteDatabase db; 
private String pgurl; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.item_view); 

    db = dbhelper.getReadableDatabase(); 

    TextView txtProduct = (TextView) findViewById(R.id.product_label); 

    Intent i = getIntent(); 
    // getting attached intent data 
    String product = i.getStringExtra("product"); 
    int data=Integer.parseInt(product.substring(4)); 
    // displaying selected product name 

    txtProduct.setText(product); 

    Cursor quer=db.rawQuery("SELECT link FROM cppref "+"WHERE _id='"+data+"';", null); 
      //-fetch record 
      if(quer.getCount()!=0){ 
      quer.moveToFirst();//go to first row 
      pgurl=quer.getString(1).toString(); 
      } 
      else{ 
      //display some notice here saying no data found 
      txtProduct.setText("Error"); 
      } 

    WebView myWebView = (WebView) findViewById(R.id.webview); 
    myWebView.setWebViewClient(new WebViewClient()); 
    myWebView.loadUrl(pgurl); 
} 

}

+0

然後發佈您的logcat。 –

回答

1

首先糾正該移動此

DBHelper dbhelper=new DBHelper(this); 

裏面onCreate(......)setContentView(.....)

+0

非常感謝!解決了問題! – snplabs

+0

@ user3462740喲歡迎! –