· 8 years ago · Nov 30, 2017, 07:38 AM
1package com.example.android.cebuano_tagalogtranslator;
2
3import android.support.v7.app.AppCompatActivity;
4import android.os.Bundle;
5
6import android.database.Cursor;
7import android.database.sqlite.SQLiteDatabase;
8import android.util.Log;
9import android.view.View;
10import android.widget.Button;
11import android.widget.EditText;
12
13public class MainActivity extends AppCompatActivity {
14
15 Button btn_clear;
16 Button btn_translate_to_ceb;
17 Button btn_translate_to_fil;
18 EditText txt_input;
19 EditText txt_output;
20 @Override
21 protected void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.activity_main);
24
25
26
27 //HABAGAT CONTROLS BEGIN
28 btn_clear = (Button) findViewById(R.id.btn_clear);
29 btn_translate_to_ceb = (Button) findViewById(R.id.btn_trans_to_ceb);
30 btn_translate_to_fil = (Button) findViewById(R.id.btn_trans_to_fil);
31
32 txt_input = (EditText) findViewById(R.id.input);
33 txt_output = (EditText) findViewById(R.id.output);
34
35 //HABAGAT : CLEAR BOTH TEXT
36 btn_clear.setOnClickListener(new View.OnClickListener() {
37 @Override
38 public void onClick(View view) {
39 txt_input.setText("type here");
40 txt_output.setText("");
41 }
42 });
43
44 //HABAGAT : FILIPINO -> CEBUANO
45 btn_translate_to_ceb.setOnClickListener(new View.OnClickListener() {
46 @Override
47 public void onClick(View view) {
48
49
50 try {
51 String textinput = txt_input.getText().toString();
52 textinput = """+ textinput +""";
53 String filToCebQuery = "SELECT ceb FROM filtoceb WHERE fil = "+ textinput;
54 SQLiteDatabase DB = openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);
55 //Cursor c = DB.rawQuery("SELECT * FROM filtoceb", null);
56 Cursor c = DB.rawQuery(filToCebQuery, null);
57
58 int cebIndex = c.getColumnIndex("ceb");
59 //int filIndex = c.getColumnIndex("fil");
60
61 c.moveToFirst();
62 while (c != null) {
63
64 //Log.i("Results - ceb", c.getString(cebIndex));
65 txt_output.setText(c.getString(cebIndex));
66 c.moveToNext();
67 }
68
69 } catch (Exception e) {
70 e.printStackTrace();
71 }
72
73 }
74 });
75
76 //HABAGAT : CREATE DB OPEN IF NOT CREATED YET
77 try {
78 SQLiteDatabase eventsDB = this.openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);
79 //eventsDB.execSQL("drop table user");
80 eventsDB.execSQL("CREATE TABLE IF NOT EXISTS filtoceb (fil VARCHAR, ceb VARCHAR)");
81
82 eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Kumusta ka?','Kumusta ka?')");
83 eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Maayo, salamat', 'Mabuti naman, salamat')");
84 eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay imong pangalan?', 'Ano pangalan mo?')");
85 eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay ngalan mo?', 'ano pangalan mo?')");
86
87 } catch (Exception e) {
88 e.printStackTrace();
89 }
90 }
91}