I am making an exact word trasnlation app. I have pair of words inside my sqlite and my app only returns a result when an exact literal match (string by string) is queired. My problem is when I issue a query I get an index out of bounds exception in Android SQLite
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
then when I try and select all records it throws
W/System.err: android.database.CursorIndexOutOfBoundsException: Index 24 requested, with a size of 24
BELOW IS MY CODE:
package com.example.android.cebuano_tagalogtranslator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button btn_clear;
Button btn_translate_to_ceb;
Button btn_translate_to_fil;
EditText txt_input;
EditText txt_output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//HABAGAT CONTROLS BEGIN
btn_clear = (Button) findViewById(R.id.btn_clear);
btn_translate_to_ceb = (Button) findViewById(R.id.btn_trans_to_ceb);
btn_translate_to_fil = (Button) findViewById(R.id.btn_trans_to_fil);
txt_input = (EditText) findViewById(R.id.input);
txt_output = (EditText) findViewById(R.id.output);
//HABAGAT : CLEAR BOTH TEXT
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txt_input.setText("type here");
txt_output.setText("");
}
});
//HABAGAT : FILIPINO -> CEBUANO
btn_translate_to_ceb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
String textinput = txt_input.getText().toString();
textinput = "\""+ textinput +"\"";
String filToCebQuery = "SELECT ceb FROM filtoceb WHERE fil = "+ textinput;
SQLiteDatabase DB = openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);
//Cursor c = DB.rawQuery("SELECT * FROM filtoceb", null);
Cursor c = DB.rawQuery(filToCebQuery, null);
int cebIndex = c.getColumnIndex("ceb");
//int filIndex = c.getColumnIndex("fil");
c.moveToFirst();
while (c != null) {
//Log.i("Results - ceb", c.getString(cebIndex));
txt_output.setText(c.getString(cebIndex));
c.moveToNext();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
//HABAGAT : CREATE DB OPEN IF NOT CREATED YET
try {
SQLiteDatabase eventsDB = this.openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);
//eventsDB.execSQL("drop table user");
eventsDB.execSQL("CREATE TABLE IF NOT EXISTS filtoceb (fil VARCHAR, ceb VARCHAR)");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Kumusta ka?','Kumusta ka?')");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Maayo, salamat', 'Mabuti naman, salamat')");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay imong pangalan?', 'Ano pangalan mo?')");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay ngalan mo?', 'ano pangalan mo?')");
} catch (Exception e) {
e.printStackTrace();
}
}
}
What am I doing wrong?
int cebIndex = c.getColumnIndex("ceb");
if (cursor.moveToFirst()){
while(!cursor.isAfterLast()){
String data = cursor.getString(cebIndex);
// do what ever you want here
txt_output.setText(cursor.getString(cebIndex));
cursor.moveToNext();
}
}
cursor.close();