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
Regular Contributor
techie
Posts: 77
Registered: ‎05-21-2012
My Device: 9900
Accepted Solution

Autocomplete textfield

Can someone guide how to use autocomplete textfield to filter from database(SQLite) table values.

 

The link http://docs.blackberry.com/en/developers/deliverables/18125/Autocomplete_text_field_1200231_11.jsp

shows with predefined arrays.

 

I want it to filter from db table.How to go about it.

 

Developer
rabiray
Posts: 141
Registered: ‎07-13-2011
My Device: BB Bold

Re: Autocomplete textfield

Create a new filter list

filteredList = new BasicFilteredList();

Add data from database

String [] dataFromDB = get Data from DB


filteredList.addDataSet(0, dataFromDB ,"ANY STRING",BasicFilteredList.COMPARISON_IGNORE_CASE);

autoCmpltFldOperations = new CustomAutoCompleteField(filteredList, AutoCompleteField.FIELD_LEFT);

 

Then add the field to screen.

 

Hope this help.

 

 

Regular Contributor
techie
Posts: 77
Registered: ‎05-21-2012
My Device: 9900

Re: Autocomplete textfield

Hello rabiray,

 

Thanks for responding.
But your answer seems little vague.

 

"Create a new filter list
filteredList = new BasicFilteredList();
Add data from database
String [] dataFromDB = get Data from DB// What to add here?Select statement.But how?
filteredList.addDataSet(0, dataFromDB ,"ANY STRING",BasicFilteredList.COMPARISON_IGNORE_CASE);
Here in second arg "dataFromDB" is what a variable or a keyword.If its to substitute some query then how to go about including it.
autoCmpltFldOperations = new CustomAutoCompleteField(filteredList, AutoCompleteField.FIELD_LEFT);

Then add the field to screen."

 Please guide.I am using this type of component for the first time.Appreciate your guidance.

 

Developer
rabiray
Posts: 141
Registered: ‎07-13-2011
My Device: BB Bold

Re: Autocomplete textfield

from your reply u looks to be a new developer....

 

get Data from DB ->  YOu need to write your own methods  about how u r going to get data from database.

 

Write a method  

 

public String[] getDataFromDB(){

 

//IMPLEMENT YOUR OWN CODE TO CONNECT TO DB AND GET THE DATA U WANTED

 

}

 

dataFromDB   = getDataFromDB();

 

 

I hope u got my [point this time.

 

 

Regular Contributor
techie
Posts: 77
Registered: ‎05-21-2012
My Device: 9900

Re: Autocomplete textfield

I tried this but its giving me compile errors like 

"Identifier expected","Illegal start of expression"

Please see the below code:

 

 //Adding filters to name
         
         BasicFilteredList filterList = new BasicFilteredList();
         filterList.setChangeListener(new FieldChangeListener()
         { 
         private String[] getDataFromDB()
         {
            try
            {    

                //Open or create the database
                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();
   
                   return(r.getString(0));
                                        
              }
                              
             statement1.close(); 
             db.close();
                        
              }
              catch( Exception e ) 
              {         
                        System.out.println( e.getMessage() );
                        e.printStackTrace();
              }
         
         }
        
        
         
         String[] dataFromDB   = getDataFromDB();
         filterList.addDataSet(1,dataFromDB ,"ANY STRING",BasicFilteredList.COMPARISON_IGNORE_CASE);//Giving errors at this point for params first,second,third
       
  
         AutoCompleteField autoCompleteField = new AutoCompleteField(filterList);
         });
         add(autoCompleteField);

 As u can notice,i have added field change listener.Without that its giving "Illegal start of expression" error.Please guide.Thanks again for your guidance.

Developer
rabiray
Posts: 141
Registered: ‎07-13-2011
My Device: BB Bold

Re: Autocomplete textfield

[ Edited ]

:No:

Developer
rabiray
Posts: 141
Registered: ‎07-13-2011
My Device: BB Bold

Re: Autocomplete textfield

Try to run this code

 

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);


}

Regular Contributor
techie
Posts: 77
Registered: ‎05-21-2012
My Device: 9900

Re: Autocomplete textfield

Thanks a million rabiray!!!!

:Clap:

 

 

Regular Contributor
techie
Posts: 77
Registered: ‎05-21-2012
My Device: 9900

Re: Autocomplete textfield

Rabiray is it possible to resize the autocomplete field.I dont want it to span the entire screen.
I have tried using this but to no luck

AutoCompleteField autoFld = new AutoCompleteField(filterLst)
{
protected void layout(int maxWidth, int maxHeight)
{
super.layout(Math.min(maxWidth, 300), Math.min(maxHeight, 20));
}

};
add(autoFld);
Any idea
Developer
rabiray
Posts: 141
Registered: ‎07-13-2011
My Device: BB Bold

Re: Autocomplete textfield

Try this...please press Like if this solves ur issue

 

 

AutoCompleteField autoFld = new AutoCompleteField(filterLst){

public int getPreferredWidth() {
return Display.getWidth()/2;
}

public void sublayout(int maxWidth, int maxheight)
{

super.sublayout(getPreferredWidth(), getPreferredHeight());
setExtent(getPreferredWidth(), getPreferredHeight());

}
};