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