0

主程序.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?

4

1 回答 1

0

在 MAIN.JAVA 中使用 AsyncTask 在 AsyncTask 中你在方法 doInBackground 连接到数据库(使用源 MAINACTIVITY.JAVA)和方法 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;

    }

}
于 2013-03-04T14:40:24.433 回答