· 8 years ago · Nov 30, 2017, 08:18 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 if (c!=null)
69 {
70 try {
71 c.moveToFirst();
72 while(!c.isAfterLast()){
73 String result = c.getString(c.getColumnIndex("ceb"));
74 txt_output.setText(result);
75 c.moveToNext();
76 }
77 }
78 finally
79 {
80 c.close();
81 }
82 }
83
84
85 } catch (Exception e) {
86 e.printStackTrace();
87 }
88
89 }
90 });
91
92 //HABAGAT : CREATE DB OPEN IF NOT CREATED YET
93 try {
94 SQLiteDatabase eventsDB = this.openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);
95 //eventsDB.execSQL("drop table user");
96 eventsDB.execSQL("CREATE TABLE IF NOT EXISTS filtoceb (fil VARCHAR, ceb VARCHAR)");
97
98 eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Kumusta ka?','Kumusta ka?')");
99 eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Maayo, salamat', 'Mabuti naman, salamat')");
100 eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay imong pangalan?', 'Ano pangalan mo?')");
101 eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay ngalan mo?', 'ano pangalan mo?')");
102 eventsDB.close();
103
104 } catch (Exception e) {
105 e.printStackTrace();
106 }
107
108 }
109
110}