我使用了静态列表并检查了所选的值,这是正确的。现在将相同的代码应用于从SQLite db动态填充的列表中,显示所单击的值的垃圾值。这是代码
class mal2eng extends MainScreen
implements ListFieldCallback {
private ListField _listfield;
private Item _selected = null;
private Vector _listVector = new Vector();
mal2eng() {
Database d;
try
{
URI myURI=URI.create("file:///SDCard/Databases/MyTestDatabase.db");
d=DatabaseFactory.open(myURI);
Statement st=d.createStatement("SELECT malLetter FROM MalayalamLetter");
st.prepare();
net.rim.device.api.database.Cursor c=st.getCursor();
Row r;
while(c.next())
{
r=c.getRow();
String w=r.getString(0);
_listVector.addElement(new Item(w));
}
st.close();
d.close();
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
this.setTitle("List");
_listfield = new ListField(_listVector.size());
_listfield.setCallback(this);
FontManager.getInstance().load("DC124.TTF", "MyFont", FontManager.APPLICATION_FONT) ;
{
try {
FontFamily typeface = FontFamily.forName("MyFont");
Font myFont = typeface.getFont(Font.PLAIN, 25);
_listfield.setFont(myFont);
this.add(_listfield);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();}
}
}
protected boolean onSavePrompt() {
return true;
}
protected void makeMenu( Menu menu, int instance ) {
}
public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
Item ToDraw = (Item) this.get(list, index);
int drawColor = Color.BLACK;
g.setColor(drawColor);
g.drawText(ToDraw.word, 0, y, 0, w);
}
public Object get(ListField list, int index) {
return _listVector.elementAt(index);
}
public int getPreferredWidth(ListField list) {
return Display.getWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
return start;
}
public boolean navigationClick(int status, int time) {
Field focus = _listfield.getLeafFieldWithFocus();
if (focus instanceof ListField) {
ListField listField = (ListField)focus;
String w=listField.getCallback().get(listField,listField.getSelectedIndex()).toString();
UiApplication ui = UiApplication.getUiApplication();
ui.pushScreen(new ListFieldScreen(w));
}
return true;
}
}
答案 0 :(得分:0)
当您从数据库获取数据时,您可以执行的一件小事使用像Vector或类似的数据结构。将数据放入您从数据库获取的向量中。覆盖 navigationClick method
如下所示...
navigationClickMethod(int, int ){
//Call getSelectedindex here
// And use this to call Element at the selected index of the dataStructure you have
}
这是从列表中获取所选值的最简单方法...