Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 47
Registered: ‎05-11-2012
My Device: Blackberry Bold 9930
My Carrier: Tmobile

Auto- complete field& database!

Hi,

I'm trying to use this code. Which is an auto-complete field that use the database.

 

import java.util.Vector;

import net.rim.device.api.collection.util.*;
import net.rim.device.api.database.Cursor;
import net.rim.device.api.database.Database;
import net.rim.device.api.database.DatabaseFactory;
import net.rim.device.api.database.Row;
import net.rim.device.api.database.Statement;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;

 


public class AutoCompleteFieldDemo extends UiApplication
{

public static void main(String[] args)
{

AutoCompleteFieldDemo app = new AutoCompleteFieldDemo();
app.enterEventDispatcher();
}



public AutoCompleteFieldDemo()
{
pushScreen(new AutoCompleteFieldDemoScreen());
}


public static String[] getDataFromDB()
{
Vector names = new Vector();

try
{


Database db = DatabaseFactory.openOrCreate("database1.db");
Statement statement1 = db.createStatement("SELECT name FROM Directory_Items");
statement1.prepare();
statement1.execute();
Cursor c = statement1.getCursor();
Row r;

while(c.next())
{
r = c.getRow();
names.addElement(r.getString(0));

}

statement1.close();
db.close();

}
catch( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}

String [] returnValues = new String[names.size()];
for (int i = 0; i < names.size(); i++) {
returnValues[i] = (String) names.elementAt(i);
}

return returnValues;

}


static final class AutoCompleteFieldDemoScreen extends MainScreen
{



AutoCompleteFieldDemoScreen()
{


BasicFilteredList filterLst = new BasicFilteredList();

filterLst.addDataSet(1,getDataFromDB() ,"Names",BasicFilteredList.COMPARISON_IGNORE_CASE)​;
AutoCompleteField autoFld = new AutoCompleteField(filterLst);
add(autoFld);


}

 

 

I found it in here

 

http://supportforums.blackberry.com/t5/Java-Development/Autocomplete-textfield/m-p/1730311#M199761

 

The problem that I'm facing is, I can't find the database. I have created the database in the SD card and I put the name in the createOropen("myDataBase.db). But When I debug it jump to the exception. Any idea about why I'm having this problem.

 

 Thank you so much!