Android SQLite:如何将变量传输到其他活动?

时间:2013-03-04 14:35:34

标签: android database sqlite extending

MAIN.JAVA

public void onClick(View arg0) {
    switch (arg0.getId()) {
    case R.id.buttonupdate:
        String name = sqlName.getText().toString();
        String value = sqlValue.getText().toString();
        MainActivity entry = new MainActivity(Main.this);
        entry.open();
        entry.createEntry(name, value);
        entry.close();
        break;
    case R.id.buttonview:
        Intent intentone = new Intent();
        intentone.setClassName("com.example.sqltwo", "com.example.sqltwo.View");
        startActivity(intentone);
        break;
    case R.id.buttonsearch:
        String searchtext = sqlQuery_value.getText().toString();
        break;
    }   public void onClick(View arg0) {
    switch (arg0.getId()) {
    case R.id.buttonsearch:
        String name = sqlName.getText().toString();
        String value = sqlValue.getText().toString();
        MainActivity entry = new MainActivity(Main.this);
        entry.open();
        entry.createEntry(name, value);
        entry.close();
        break;
    case R.id.buttonview:
        Intent intentone = new Intent();
        intentone.setClassName("com.example.sqltwo", "com.example.sqltwo.View");
        startActivity(intentone);
        break;
    case R.id.buttonsearch:
        String searchtext = sqlQuery_value.getText().toString();
        break;
    }

MAINACTIVITY.JAVA

public long createEntry(String name, String value) {
    ContentValues cv = new ContentValues();
            cv.put(KEY_NAMES, name);
            cv.put(KEY_VALUES, value);
            return qdatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
    String[] columns = new String[]{KEY_NAMES, KEY_VALUES};
    Cursor c = qdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iValue = c.getColumnIndex(KEY_VALUES);
    int iName = c.getColumnIndex(KEY_NAMES);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result + c.getString(iName) + "\n" + c.getString(iValue) + "\n";
    }
    return result;  
}
public String search(String searchtext) {
    String searchresult = "";
    String arg = searchtext;

    String query = "select * from " + DATABASE_TABLE + " where title=\"" + arg +"\"";

    Cursor c = qdatabase.rawQuery(query, new String[] {arg});
    int iName = c.getColumnIndex(KEY_NAMES);

    if (c.moveToFirst()) {
        searchresult = searchresult + c.getString(iName);
    }

    return searchresult;

}

在R.id.buttonsearch案件中

它抓取TextEdit并将其存储到searchtext中。 我需要MainActivity中的searchtext运行搜索功能并返回与该查询匹配的所有行。如何将Main中的searchtext链接到MainActivity?

1 个答案:

答案 0 :(得分:0)

在MAIN.JAVA中使用AsyncTask中的AsyncTask你在metod doInBackground连接到数据库(使用源MAINACTIVITY.JAVA)和metod onPostExecute更改MAIN.JAVA

例如

private class loudAllTranslate extends
        AsyncTask<Void, Void, HashMap<String, Translate>> {

    Context context;
    String fullText;

    loudAllTranslate(Context cont, String fullText_) {
        context = cont;
        fullText = fullText_;
    }

    @Override
    protected void onPostExecute(HashMap<String, Translate> success) {
        super.onPostExecute(success);

        // do somting

    }

    @Override
    protected HashMap<String, Translate> doInBackground(Void... params) {

        LBD conection = new LBD(context);
        conection.open();
        conection.tblSettings();

        HashMap<String, Translate> listTranstate = Settings
                .updateAllTranslate(fullText);
        conection.close();
        return listTranstate;

    }

}