Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.45 KB | None | 0 0
  1. package com.gmd.referenceapplication;
  2.  
  3. import android.app.ListActivity;
  4. import android.app.SearchManager;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.database.Cursor;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.support.v7.widget.SearchView;
  11. import android.support.v7.widget.Toolbar;
  12. import android.util.Log;
  13. import android.view.Menu;
  14. import android.view.MenuInflater;
  15. import android.view.MenuItem;
  16. import android.view.View;
  17. import android.widget.AdapterView;
  18. import android.widget.ListView;
  19. import android.widget.SimpleCursorAdapter;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22.  
  23. public class SearchableActivity extends AppCompatActivity {
  24.  
  25. DatabaseTable db= new DatabaseTable(this);
  26.  
  27.  
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_searchable);
  32.  
  33. //creates toolbar
  34. Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
  35. setSupportActionBar(myToolbar);
  36.  
  37.  
  38.  
  39. // get the intent sent when user searches from search widget, verify the action and extract what is typed in
  40. Intent intent = getIntent();
  41. handleIntent(intent);
  42.  
  43. }
  44.  
  45. //creates menu
  46.  
  47. public boolean onCreateOptionsMenu(Menu menu) {
  48. MenuInflater inflater = getMenuInflater();
  49. inflater.inflate(R.menu.overflow, menu);
  50.  
  51.  
  52. SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
  53. searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  54. @Override
  55. public boolean onQueryTextSubmit(String query) {
  56. //search button is pressed
  57.  
  58. Cursor c=db.getWordMatches(query,null);
  59. doSearch(c);
  60.  
  61. Log.e("searchable activity","text submitted");
  62.  
  63. return true;
  64. }
  65.  
  66. @Override
  67. public boolean onQueryTextChange(String newText) {
  68. // User changed the text
  69. Cursor c=db.getWordMatches(newText,null);
  70. doSearch(c);
  71.  
  72. Log.e("searchable activity ", "haha losing will to live");
  73. return true;
  74. }
  75. });
  76.  
  77. SearchManager searchManager =
  78. (SearchManager) getSystemService(Context.SEARCH_SERVICE);
  79. searchView =
  80. (SearchView) menu.findItem(R.id.search).getActionView();
  81. searchView.setSearchableInfo(
  82. searchManager.getSearchableInfo(getComponentName()));
  83.  
  84. return true;
  85.  
  86. }
  87.  
  88.  
  89.  
  90. public void onNewIntent(Intent intent) {
  91. setIntent(intent);
  92. handleIntent(intent);
  93. }
  94.  
  95. private void handleIntent(Intent intent) {
  96.  
  97. if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
  98. String query = intent.getStringExtra(SearchManager.QUERY);
  99. Cursor c = db.getWordMatches(query, null);
  100. doSearch(c);
  101. Log.e("Search Operation", "Database searched");
  102.  
  103.  
  104. //still need to process Cursor and display results
  105. }
  106. }
  107.  
  108. public void onListItemClick(ListView l,
  109. View v, int position, long id) {
  110. // call detail activity for clicked entry
  111. }
  112.  
  113.  
  114.  
  115. private void doSearch(Cursor query) {
  116. // get a Cursor, prepare the ListAdapter
  117. // and set it
  118. Cursor c = query;
  119. startManagingCursor(c);
  120.  
  121.  
  122. ListView sl =(ListView)findViewById(android.R.id.list);
  123.  
  124. String[] from = new String[] {"QUANTITY", "_id"};
  125. int[] to = new int[] {android.R.id.text1};
  126. SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, from, to);
  127. sl.setAdapter(cursorAdapter);
  128. Log.e("doSearch method:", "has been called");
  129.  
  130. sl.setOnItemClickListener(
  131. new AdapterView.OnItemClickListener() {
  132. public void onItemClick(AdapterView<?> parent, View view,
  133. int position, long id) {
  134. // When clicked, show a toast with the TextView text
  135. Log.e("doSearch method:", "Answer: " + ((TextView) view).getText());
  136. }
  137. });
  138. }
  139.  
  140.  
  141.  
  142. //manages activity bar
  143.  
  144. @Override
  145. public boolean onOptionsItemSelected(MenuItem item) {
  146. switch (item.getItemId()) {
  147. case R.id.constants:
  148. startActivity(new Intent(this, FundamentalPhysicalConstants.class));
  149. return true;
  150.  
  151. case R.id.joes_rules:
  152. //go to rules
  153. //startActivity(new Intent(MainActivity.this, ExampleListView.class));
  154. return true;
  155.  
  156. case R.id.home:
  157. //Go back to the home screen
  158. startActivity(new Intent(this, MainActivity.class));
  159. return true;
  160.  
  161. case R.id.search:
  162. //open search
  163. //startActivity(new Intent(this, SearchableActivity.class));
  164. return true;
  165.  
  166. case R.id.links:
  167. //go to referencelinks
  168. startActivity(new Intent(this, ReferenceLinks.class));
  169. return true;
  170.  
  171. case R.id.base_units:
  172. //go to baseunits
  173. startActivity(new Intent(this, SIBaseUnits.class));
  174. return true;
  175.  
  176.  
  177. default:
  178. // If we got here, the user's action was not recognized.
  179. // Invoke the superclass to handle it.
  180. return super.onOptionsItemSelected(item);
  181.  
  182. }
  183. }
  184.  
  185.  
  186.  
  187. }
  188.  
  189. <?xml version="1.0" encoding="utf-8"?>
  190. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  191. xmlns:tools="http://schemas.android.com/tools"
  192. android:layout_width="match_parent"
  193. android:layout_height="match_parent"
  194. xmlns:app="http://schemas.android.com/apk/res-auto"
  195. android:paddingBottom="@dimen/activity_vertical_margin"
  196. android:paddingLeft="@dimen/activity_horizontal_margin"
  197. android:paddingRight="@dimen/activity_horizontal_margin"
  198. android:paddingTop="@dimen/activity_vertical_margin"
  199. tools:context="com.gmd.referenceapplication.SearchableActivity">
  200.  
  201. <android.support.v7.widget.Toolbar
  202. android:id="@+id/my_toolbar"
  203. android:layout_width="match_parent"
  204. android:layout_height="?attr/actionBarSize"
  205. android:background="?attr/colorPrimary"
  206. android:elevation="4dp"
  207. android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
  208. app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
  209. />
  210.  
  211.  
  212. <ListView
  213. android:layout_width="wrap_content"
  214. android:layout_height="wrap_content"
  215. android:id="@id/android:list" />
  216.  
  217. </RelativeLayout>
  218.  
  219. package com.gmd.referenceapplication;
  220.  
  221. import android.content.ContentValues;
  222. import android.content.Context;
  223. import android.content.res.Resources;
  224. import android.database.Cursor;
  225. import android.database.sqlite.SQLiteDatabase;
  226. import android.database.sqlite.SQLiteOpenHelper;
  227. import android.database.sqlite.SQLiteQueryBuilder;
  228. import android.text.TextUtils;
  229. import android.util.Log;
  230.  
  231. import com.gmd.referenceapplication.R;
  232.  
  233. import java.io.BufferedReader;
  234. import java.io.IOException;
  235. import java.io.InputStream;
  236. import java.io.InputStreamReader;
  237.  
  238. /**
  239. * Created by gmd on 6/8/2016.
  240. */
  241. public class DatabaseTable {
  242.  
  243.  
  244. public static final String TAG = "ConstantDatabase";
  245.  
  246. //the columns included in the table
  247. public static final String COL_QUANTITY = "QUANTIY";
  248. public static final String COL_VALUE = "VALUE";
  249. public static final String COL_UNCERTAINTY = "UNCERTAINTY";
  250. public static final String COL_UNIT = "UNIT";
  251. public static final String _id = "_id";
  252.  
  253. private static final String DATABASE_NAME = "CONSTANTS";
  254. private static final String FTS_VIRTUAL_TABLE = "FTS";
  255. private static final int DATABASE_VERSION = 1;
  256.  
  257.  
  258. private final DatabaseOpenHelper mDatabaseOpenHelper;
  259.  
  260.  
  261.  
  262. public DatabaseTable(Context context){
  263. mDatabaseOpenHelper = new DatabaseOpenHelper(context);
  264. }
  265.  
  266. private static class DatabaseOpenHelper extends SQLiteOpenHelper {
  267.  
  268. private final Context mHelperContext;
  269. private SQLiteDatabase mDatabase;
  270.  
  271. private static final String FTS_TABLE_CREATE =
  272. "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE +
  273. " USING fts3 (" +_id+ ", "+
  274. COL_QUANTITY + ", " +
  275. COL_VALUE + "," +
  276. COL_UNCERTAINTY + "," +
  277. COL_UNIT + ")";
  278.  
  279. public DatabaseOpenHelper(Context context) {
  280. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  281. Log.e("Database Operation", "DatabaseOpenHelper constructor called");
  282. mHelperContext = context;
  283. }
  284.  
  285. @Override
  286. public void onCreate(SQLiteDatabase db) {
  287. mDatabase = db;
  288. mDatabase.execSQL(FTS_TABLE_CREATE);
  289. Log.e("Database Operation", "Constants Table Created ...");
  290. loadConstants();
  291. }
  292.  
  293. @Override
  294. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  295. Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
  296. + newVersion + ", which will destroy all old data");
  297. db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
  298. onCreate(db);
  299. }
  300.  
  301. public SQLiteDatabase getmDatabase(){
  302. return mDatabase;
  303. }
  304.  
  305.  
  306. // populating the virtual table with a string reading code
  307.  
  308.  
  309. private void loadConstants() {
  310. new Thread(new Runnable() {
  311. public void run() {
  312. try {
  313. loadConstantss();
  314. } catch (IOException e) {
  315. throw new RuntimeException(e);
  316. }
  317. }
  318. }).start();
  319.  
  320. Log.e("Loading", "Constants Table Populated ...");
  321. }
  322.  
  323. private void loadConstantss() throws IOException {
  324. final Resources resources = mHelperContext.getResources();
  325. InputStream inputStream = resources.openRawResource(R.raw.txt);
  326. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  327.  
  328. try {
  329. String line;
  330. while ((line = reader.readLine()) != null) {
  331. String[] strings = TextUtils.split(line, ",");
  332. if (strings.length < 4) continue;
  333. long id = addConstant(strings[0].trim(), strings[1].trim(), strings[2].trim(), strings[3].trim());
  334. if (id < 0) {
  335. Log.e(TAG, "unable to add word: " + strings[0].trim());
  336. }
  337. }
  338. } finally {
  339. reader.close();
  340. }
  341. }
  342.  
  343. public long addConstant(String quantity, String value, String uncertainty, String unit) {
  344. ContentValues initialValues = new ContentValues();
  345. initialValues.put(COL_QUANTITY, quantity);
  346. initialValues.put(COL_VALUE, value);
  347. initialValues.put(COL_UNCERTAINTY, uncertainty);
  348. initialValues.put(COL_UNIT, unit);
  349.  
  350.  
  351. return mDatabase.insert(FTS_VIRTUAL_TABLE, null, initialValues);
  352. }
  353.  
  354.  
  355. //database openhelper ends
  356.  
  357. }
  358.  
  359.  
  360.  
  361.  
  362. public Cursor getWordMatches(String query, String[] columns) {
  363. String selection = COL_QUANTITY + " MATCH ?";
  364. String[] selectionArgs = new String[] {query+"*"};
  365.  
  366. return query(selection, selectionArgs, columns);
  367. }
  368.  
  369. public Cursor query(String selection, String[] selectionArgs, String[] columns) {
  370. SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
  371. builder.setTables(FTS_VIRTUAL_TABLE);
  372.  
  373. Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(),
  374. columns, selection, selectionArgs, null, null, null);
  375.  
  376. if (cursor == null) {
  377. return null;
  378. } else if (!cursor.moveToFirst()) {
  379. cursor.close();
  380. return null;
  381. }
  382. return cursor;
  383. }
  384.  
  385. public Cursor getAllTitles()
  386. {
  387. return mDatabaseOpenHelper.getmDatabase().query(FTS_VIRTUAL_TABLE, new String[] {
  388. COL_QUANTITY,
  389. COL_UNCERTAINTY,
  390. COL_UNIT,
  391. COL_VALUE},
  392. null,
  393. null,
  394. null,
  395. null,
  396. null);
  397. }
  398.  
  399.  
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement