我试图ListField
用黑莓中的复选框来实现自定义。我能够正确显示列表。我的要求是我需要使用黑莓中的复选框选择多个项目,但我无法更改选中并取消选中复选框。我尝试了navigationClick
, CheckBox.setChangeListener
,HorizontalFieldManager.setChangeListener
方法,但没有得到任何解决方案。谁能告诉我我做错了什么?
我的代码是:
public class InboxWithCheckboxes extends MainScreen{
private InboxWithCheckboxeslistfield inboxWithCheckboxeslistfield;
public MenuItem _DeleteMessage;
private String[]templongDBTimeStamp;
private Database db;
private static final int[] GRADIENT_COLORS =
{
Color.PALEVIOLETRED, Color.ANTIQUEWHITE, Color.DARKTURQUOISE, Color.DEEPSKYBLUE
};
protected void makeMenu(Menu menu, int instance)
{
menu.add(_DeleteMessage);
//Create the default menu.
super.makeMenu(menu, instance);
}
public InboxWithCheckboxes(){
inboxWithCheckboxeslistfield = new InboxWithCheckboxeslistfield();
Background bg = BackgroundFactory.createLinearGradientBackground(
GRADIENT_COLORS[0], GRADIENT_COLORS[1],
GRADIENT_COLORS[2], GRADIENT_COLORS[3]);
this.getMainManager().setBackground(
bg);
add(inboxWithCheckboxeslistfield);
add(new SeparatorField());
db = new Database();
_DeleteMessage = new MenuItem("DeleteMessage", 200, 10){
public void run(){
int index[] = inboxWithCheckboxeslistfield.getSelection();
System.out.println("the list size is:"+inboxWithCheckboxeslistfield.getSize());
for(int i=0;i<inboxWithCheckboxeslistfield.getSize();i++){
int index1 = inboxWithCheckboxeslistfield.getSelectedIndex();
System.out.println(" selected index is:"+index1);
inboxWithCheckboxeslistfield.delete(index1);
inboxWithCheckboxeslistfield.eraseAt(index1);
}
invalidate();
}
};
}
}
class InboxWithCheckboxeslistfield extends ListField implements ListFieldCallback {
public Vector rows;
private TableRowManager row ;
public TableRowManager rowManager;
private LabelField UserName,Message,Timestamp;
private HorizontalFieldManager hfmcheck;
private CheckboxField checkbox;
private Database db;
private Vector inboxdatavector;
private String[] msg,key,userinfo,timearray;
private long[] timeStamp;
/* Border For EditFields */
Border bdr = BorderFactory.createRoundedBorder(new XYEdges(4, 4, 4, 4),
Border.STYLE_SOLID);
/**
* Creates a new MyScreen object
*/
public InboxWithCheckboxeslistfield() {
// TODO Auto-generated constructor stub
super(0, ListField.MULTI_SELECT);
setRowHeight(120);
setCallback(this);
rows = new Vector();
db = new Database();
inboxdatavector = db.fetchDataInbox();
int count = db.getcountInbox();
msg = new String[count];
key = new String[count];
userinfo = new String[count];
timearray = new String[count];
timeStamp = new long[count];
for (int i = 0; i < count; i++) {
row = new TableRowManager() {
public void paint(Graphics g) {
g.setBackgroundColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
g.clear();
super.paint(g);
}
};
try{
byte[] a = (byte[]) inboxdatavector.elementAt(i);
ByteArrayInputStream bais = new ByteArrayInputStream(a);
DataInputStream dis = new DataInputStream(bais);
msg[i]= dis.readUTF();
key[i] = dis.readUTF();
userinfo[i] = dis.readUTF();
timeStamp[i] = dis.readLong();
timearray[i] = convertMillisecondsToDate(timeStamp[i]);
System.out.println("msg is "+msg[i]);
System.out.println("key is "+key[i]);
System.out.println("userinfo is "+userinfo[i]);
System.out.println("timeStamp is "+timeStamp[i]);
bais.close();
dis.close();
} catch (Exception e) {
e.printStackTrace();
}
UserName = new LabelField("" + String.valueOf(i),
DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH
| DrawStyle.LEFT);
UserName.setText(userinfo[i]);
row.add(UserName);
hfmcheck = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);
row.add(hfmcheck);
checkbox = new CheckboxField("",false,Field.USE_ALL_WIDTH){
public void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
};
hfmcheck.add(checkbox);
hfmcheck.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
System.out.println("entered into hfmfield");
checkbox.setChecked(true);
}
});
Message = new LabelField("" + String.valueOf(i),
DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH
| DrawStyle.RIGHT);
Message.setText(msg[i]);
row.add(Message);
Timestamp = new LabelField("" + String.valueOf(i),
DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH
| DrawStyle.RIGHT);
Timestamp.setText(timearray[i]);
row.add(Timestamp);
rows.addElement(row);
}
for (int i = 0; i < userinfo.length; i++) {
timearray[i] = convertMillisecondsToDate(timeStamp[i]);
System.out.println("the time is:"+timearray[i]);
}
/*checkbox.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
System.out.println("enter into checked changed.");
checkbox.setChecked(true);
}
});*/
setSize(rows.size());
}
public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {
// TODO Auto-generated method stub
InboxWithCheckboxeslistfield list = (InboxWithCheckboxeslistfield) listField;
rowManager = (TableRowManager) list.rows.elementAt(index);
((TableRowManager) rowManager).drawRow(graphics, 0, y, width, list.getRowHeight());
}
public void erase() {
rows.removeAllElements();
}
public void eraseAt(int index) {
rows.removeElementAt(index);
}
private class TableRowManager extends Manager {
protected TableRowManager(){
super(0);
}
public void drawRow(Graphics graphics, int i, int y, int width,
int rowHeight) {
// TODO Auto-generated method stub
// Arrange the cell fields within this row manager.
layout(width, rowHeight);
// Place this row manager within its enclosing list.
setPosition(i, y);
// Apply a translating/clipping transformation to the graphics
// context so that this row paints in the right area.
graphics.pushRegion(getExtent());
// Paint this manager's controlled fields.
subpaint(graphics);
graphics.setColor(0x00CACACA);
graphics.drawLine(0, 0, getPreferredWidth(), 0);
// Restore the graphics context.
graphics.popContext();
}
protected void sublayout(int width, int height) {
// TODO Auto-generated method stub
// set the size and position of each field.
int fontHeight = Font.getDefault().getHeight();
int preferredWidth = getPreferredWidth();
Field field = getField(0);
layoutChild(field, preferredWidth, 50);
setPositionChild(field, 10, 5);
field = getField(1);
layoutChild(field, preferredWidth, 150);
setPositionChild(field, 10,40);
// set the list name label field
field = getField(2);
layoutChild(field, 300, fontHeight + 1);
//setPositionChild(field, 1, 2);
setPositionChild(field, 80, fontHeight );
// set the due time name label field
field = getField(3);
layoutChild(field, 150, fontHeight + 1);
setPositionChild(field, 500, fontHeight );
setExtent(getPreferredWidth(), getPreferredHeight());
}
// The preferred width of a row is defined by the list renderer.
public int getPreferredWidth()
{
return Display.getWidth();
}
// The preferred height of a row is the "row height" as defined in the
// enclosing list.
public int getPreferredHeight()
{
return getRowHeight();
}
}
public Object get(ListField listField, int index) {
return null;
}
public int getPreferredWidth(ListField listField) {
return 0;
}
public int indexOfList(ListField listField, String prefix, int start) {
return 0;
}
public String convertMillisecondsToDate(long timeStamp) {
// Create a DateFormatter object for displaying date information.
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
java.util.Date date = new java.util.Date(timeStamp);
System.out.println("the date is:"+formatter.formatLocal(date.getTime()));
return formatter.formatLocal(date.getTime());
}
protected boolean navigationClick(int status, int time) {
/*_DeleteMessage = new MenuItem("DeleteMessage", 200, 10){
public void run(){
System.out.println("entered into delete item");
int index = getSelectedIndex();
System.out.println(" selected index is:"+index);
for(int i=0;i<rows.size();i++){
rows.removeElementAt(index);
invalidate();
}
}
};*/
return true;
}
}