メイン.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 の検索テキストが必要です。Main の検索テキストを MainActivity にリンクするにはどうすればよいですか?