I m building a Dictionary App where i m using an existing database consisting of 100k words in it. Now the problem is when my app runs on my device it starts with splash activity and then another activity where i have an edit text view and a search button for searing a word. But when i press search button the app hangs for few seconds and it doesn't move to another activity where that word and meaning is shown. Here is my code:
This is my homeActivity.java that opens after the splash screen.
package index1.developer.acadview.com.dictionaryapp;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class homeActivity extends AppCompatActivity {
public static ArrayList<DictObjectModel> data;
EditText et;
Button b1;
DatabaseHelper db;
ArrayList<String> wordcombimelist;
ArrayList<String> meancombimelist;
LinkedHashMap<String, String> namelist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
db = new DatabaseHelper(this);
et=(EditText) findViewById(R.id.et1);
b1=(Button)findViewById(R.id.but1);
data = new ArrayList<DictObjectModel>();
fetchData();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String st=et.getText().toString();
Intent go= new Intent(homeActivity.this, wordDisplay.class);
Bundle args = new Bundle();
args.putSerializable("meaningdata", (Serializable) data);
go.putExtra("wd",st);
go.putExtra("BUNDLE",args);
startActivity(go);
}
});
}
public void fetchData() {
db = new DatabaseHelper(this);
try {
db.createDataBase();
db.openDataBase();
} catch (Exception e) {
e.printStackTrace();
}
namelist = new LinkedHashMap<>();
int ii;
SQLiteDatabase sd = db.getReadableDatabase();
Cursor cursor = sd.query("Dictionary1", null, null, null, null, null, null);
ii = cursor.getColumnIndex("word");
wordcombimelist = new ArrayList<String>();
meancombimelist = new ArrayList<String>();
while (cursor.moveToNext()) {
namelist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndex("definition")));
}
Iterator entries = namelist.entrySet().iterator();
while(entries.hasNext()) {
Map.Entry thisEntry = (Map.Entry) entries.next();
wordcombimelist.add(String.valueOf(thisEntry.getKey()));
meancombimelist.add("- " + String.valueOf(thisEntry.getValue()));
}
for (int i = 0; i < wordcombimelist.size(); i++) {
data.add(new DictObjectModel(wordcombimelist.get(i), meancombimelist.get(i)));
}
}
}
This is my another activity where i wish to display my words and meaning fetched from database to be displayed as a text switcher wordDisplay.java:
package index1.developer.acadview.com.dictionaryapp;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import java.util.ArrayList;
public class wordDisplay extends AppCompatActivity {
private TextSwitcher tx;
ArrayList<DictObjectModel> dataset;
ImageButton right;
ImageButton left;
int j = 1;
int currentindex;
long messageCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word_display);
dataset = new ArrayList<>();
Intent intent = getIntent();
Bundle bundle= intent.getBundleExtra("BUNDLE");
dataset= (ArrayList<DictObjectModel>)
bundle.getSerializable("meaningdata");
Bundle b = intent.getExtras();
messageCount = dataset.size();
tx = (TextSwitcher) findViewById(R.id.textSwitcher);
right = (ImageButton) findViewById(R.id.rightbut);
left = (ImageButton) findViewById(R.id.leftbut);
tx.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView textdis = new TextView(wordDisplay.this);
textdis.setGravity(Gravity.TOP | Gravity.CENTER_VERTICAL);
textdis.setTextSize(24);
textdis.setTextColor(Color.BLUE);
return textdis;
}
});
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
tx.setAnimation(in);
tx.setAnimation(out);
final String i = b.getString("wd");
currentindex = j;
tx.setText(dataset.get(currentindex).getWord());
/* while (currentindex < messageCount) {
if (i.equalsIgnoreCase(dataset.get(currentindex).getWord())) {
tx.setText((CharSequence) dataset.get(currentindex).getWord());
break;
}
currentindex++;
}
* */
}
}
/*left.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
currentindex--;
if (currentindex == -1) {
currentindex = 0;
Toast.makeText(wordDisplay.this, "Reached the first word",
Toast.LENGTH_SHORT).show();
}
tx.setText((CharSequence) dataset.get(currentindex).getWord());
tx.setText((CharSequence)
dataset.get(currentindex).getMeaning());
}
});
right.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
currentindex++;
if (currentindex == messageCount)
currentindex = 0;
tx.setText((CharSequence) dataset.get(currentindex).getWord());
tx.setText((CharSequence)
dataset.get(currentindex).getMeaning());
}
});
* */
And my databaseHelper.java:
package index1.developer.acadview.com.dictionaryapp;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Button;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
public class DatabaseHelper extends SQLiteOpenHelper{
//The Android's default system path of your application database.
private static String DB_PATH = "";
private static String DB_NAME = "dictionary.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to
the application assets and resources.
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
DB_PATH= myContext.getDatabasePath(DB_NAME).toString();
}
/**
* Creates a empty database on the system and rewrites it with your own
database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the
default system path
//of your application so we are gonna be able to overwrite that
database with our database.
this.getWritableDatabase();
this.close();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each
time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
// this.getReadableDatabase();
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH ;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created
empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH ;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH ;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// Add your public helper methods to access and get content from the
database.
// You could return cursors by doing "return myDataBase.query(....)" so
it'd be easy
// to you to create adapters for your views.
//add your public methods for insert, get, delete and update data in
database.
}
And last the object model class:
package index1.developer.acadview.com.dictionaryapp;
import java.io.Serializable;
public class DictObjectModel implements Serializable {
String word, meaning;
public DictObjectModel(String word, String meaning){
this.word=word;
this.meaning = meaning;
}
public String getWord()
{
return word;
}
public String getMeaning()
{
return meaning;
}
}
Please someone help me.