11.07.2013, 01:12. Просмотров 532. Ответов 0
Не могу реализовать простейший поиск в приложении. В приложении таб из 3-х вкладов, на 1 и на 2 лист вью с несколькими элементами.
так я описал XML ,на которой необходимо разместить поиск поиск:
XML |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top">
<EditText
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/text"
android:layout_weight="100.0" />
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add" />
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_gravity="left"
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/no_records"/>
</LinearLayout> |
|
тут я пытался хоть чтонить придумать....но безуспешно
Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
| public class search extends Activity{
private EditText text;
private Button add;
private RecordsDbHelper mDbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
mDbHelper = new RecordsDbHelper(this);
mDbHelper.open();
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
add = (Button) findViewById(R.id.add);
text = (EditText) findViewById(R.id.text);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String data = text.getText().toString();
if (!data.equals("")) {
saveTask(data);
text.setText("");
}
}
});
}
private void saveTask(String data) {
mDbHelper.createRecord(data);
}
private void showResults(String query) {
Cursor cursor = mDbHelper.fetchRecordsByQuery(query);
startManagingCursor(cursor);
String[] from = new String[] { RecordsDbHelper.KEY_DATA };
int[] to = new int[] { R.id.text };
SimpleCursorAdapter records = new SimpleCursorAdapter(this,
R.layout.record, cursor, from, to);
}
} |
|
RecordsDbHelper
Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
| public class RecordsDbHelper {
public static final String KEY_DATA = "data";
public static final String KEY_ROWID = "_id";
private static final String TAG = "RecordsDbHelper";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE = "CREATE TABLE records(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "data TEXT NOT NULL);";
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "records";
private static final int DATABASE_VERSION = 1;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS tasks");
onCreate(db);
}
}
public RecordsDbHelper(Context ctx) {
this.mCtx = ctx;
}
public RecordsDbHelper open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public long createRecord(String data) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DATA, data);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public Cursor fetchRecordsByQuery(String query) {
return mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_DATA }, KEY_DATA + " LIKE" + "'%" + query + "%'", null,
null, null, null, null);
}
} |
|
Помогите дописать мой код или предложите что-либо более простое, если Вас не затруднит.