Android:获取bundle字符串以创建sqlite数据库表

时间:2012-03-08 16:56:56

标签: android sqlite

感谢您抽出时间阅读我的问题。

我计划编写一个应用程序,允许用户选择表格列来执行全文搜索。因此,我将用户输入到bundle string中并尝试将其传递给SQLiteOpenHelper。但是,SQLOpenHelper无法获取字符串。我的代码段如下:

ActivityA.java
public class ActivityA extends Activity {
...
//get the selected dropdown list value
public void addListenerOnButton(final Spinner sp1, final Spinner sp2, final MultiSpinner msp1, final MultiSpinner msp2) {

btnSubmit = (Button) findViewById(R.id.btnSubmit);

btnSubmit.setOnClickListener(new OnClickListener() {

//@Override
public void onClick(View v) {

    strpkey = String.valueOf(sp1.getSelectedItem());
    strskey = String.valueOf(sp2.getSelectedItem());
    stronscreen = String.valueOf(msp1.getSelectedItem());
    strwhenclick = String.valueOf(msp2.getSelectedItem());

    Intent intent = new Intent();
        intent.setClass(ActivityA.this, ActivityB.class);
    Bundle bundle = new Bundle();
    bundle.putString("Pkey", strpkey);
    bundle.putString("Skey", strskey);
    bundle.putString("Onscreen", stronscreen);
    bundle.putString("Whenclick", strwhenclick);
    intent.putExtras(bundle);
      startActivity(intent);                                
}

注意:我能够在ActivityB中获取捆绑字符串,因此我不认为问题出在ActivityA中。

ActivityB.java

public class ActivityB extends Activity {
public String strpkey, strskey;
public String stronscreen;
private DbAdapter dbHelper;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.countsheet);

    strpkey = getfilepath("Pkey");
    strskey = getfilepath("Skey");
    stronscreen = getfilepath("Onscreen");

    Toast.makeText(this, strpkey + " " + strskey + " " + stronscreen, Toast.LENGTH_LONG).show();

    dbHelper = new DbAdapter(this);
    dbHelper.open();

public String getfilepath(String strbundletext) {
    Bundle bundle = this.getIntent().getExtras();
    String csvpath = bundle.getString(strbundletext);
    return csvpath;
}
...
}

DBAdapter.java

import com.ksgrp.stocktake.ActivityB;

public class DbAdapter {

public static class DatabaseHelper extends SQLiteOpenHelper {

    public ListDesign csobj2 = new ListDesign();
    public String pkey = csobj2.strpkey;
    private String skey = csobj2.strskey;
    public String onscreen = csobj2.stronscreen;        

    private String getstr(String pkey, String skey) {
        ActivityB csobj = new ActivityB();
        String pkey = csobj.strpkey;
        String skey = csobj.strskey;
        String onscreen = csobj.stronscreen;

        String sqlstr = "CREATE VIRTUAL TABLE " + vdb_tbl + " USING fts3(" + sqlstr + ")";
        return sqlstr;      
    }


    //@Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String strsql = getstr(this.pkey, this.skey);
        db.execSQL("DROP TABLE IF EXISTS " + vdb_tbl);
        db.execSQL(strsql);
        Log.w("TAG", strsql);
    }
}

当我单击ActivityA中的按钮时,我得到一个java空指针异常:

03-08 14:04:31.569: E/AndroidRuntime(29573): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ksgrp.stocktake/com.ksgrp.stocktake.ActivityB}: android.database.sqlite.SQLiteException: vtable constructor failed: stocklist2: CREATE VIRTUAL TABLE stocklist2 USING fts3(null, null)      

我相信我无法将捆绑字符串中的值输入到DBAdapter类中。任何人都可以帮我一把吗?我可以提供完整的代码(但它很长,所以我刚刚提取了上面的相关位)。

1 个答案:

答案 0 :(得分:0)

我猜你应该尝试在` and not onButtonClick`中检索微调器的选定值。

所以,而不是strpkey = String.valueOf(sp1.getSelectedItem());中的onButtonClick

sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView View selectedItemView, int position, long id) {

           strpkey =sp1.getItemAtPosition(position).toString();
           }

并直接在strpkey中使用此onButtonClick

与其他3个微调器类似。

希望它有所帮助...