gpt4 book ai didi

java - 如何在列表字段中单击时添加复选框

转载 作者:行者123 更新时间:2023-12-01 05:20:34 24 4
gpt4 key购买 nike

我是黑莓开发的新手。我想在单击任何列表项时添加复选框或刻度线。当我尝试运行时,出现 104 错误。

public final class ListDemoScreen extends MainScreen {
private Vector _listElements;

private int LEFT_OFFSET = 10;
private int TOP_OFFSET = 10;
ListField list;
private ListField _checkList;
private MenuItem _toggleItem;

public ListDemoScreen() {
super(Manager.NO_VERTICAL_SCROLL);
// Set the displayed title of the screen
setTitle("List Demo 1");
add(new LabelField("Fruits List", LabelField.FIELD_HCENTER));
add(new SeparatorField());

_listElements = new Vector();
add(new SeparatorField());
list = new ListField();
ListCallback _callback = new ListCallback(this);

list.setCallback(_callback);
list.setSize(4);
int index = list.getSelectedIndex();

add(list);

createField();

}

protected void createField() {
String itemOne = "Apple";
String itemTwo = "Blackberry";
String itemthree = "Grapes";
String itemfour = "Banana";
_listElements.addElement(itemOne);

_listElements.addElement(itemTwo);
_listElements.addElement(itemthree);
_listElements.addElement(itemfour);
reloadList();

}

private void reloadList() {
list.setSize(_listElements.size());
}


public boolean invokeAction(int action) {
switch (action) {
case ACTION_INVOKE: // Trackball click.
int index = list.getSelectedIndex();
ChecklistData data = (ChecklistData) _listElements.elementAt(index);
data.toggleChecked();
_listElements.setElementAt(data, index);
list.invalidate(index);
return true; // We've consumed the event.
}
return super.invokeAction(action);

}


class ListCallback implements ListFieldCallback {
ListDemoScreen listDemoScreen;

public ListCallback(ListDemoScreen listDemoScreen)
{
this.listDemoScreen=listDemoScreen;

}

public void drawListRow(ListField list, Graphics g, int index, int y,
int w) {

String text = (String) _listElements.elementAt(index);
g.drawText(text, 60, y + 5, 0, w);
text = (String) _listElements.elementAt(index);
Bitmap bitm = Bitmap.getBitmapResource("bullet_arrow.png");
int xpos = LEFT_OFFSET;
int ypos = TOP_OFFSET + y;
w = bitm.getWidth();
int h = bitm.getHeight();

g.drawBitmap(xpos, ypos, w, h, bitm, 0, 0);

xpos = w + 20;

ChecklistData currentRow = (ChecklistData)this.get(list, index);

StringBuffer rowString = new StringBuffer();

// If it is checked draw the String prefixed with a checked box,
// prefix an unchecked box if it is not.
if (currentRow.isChecked()) {
rowString.append(Characters.BALLOT_BOX_WITH_CHECK);
} else {
rowString.append(Characters.BALLOT_BOX);
}

// Append a couple spaces and the row's text.
rowString.append(Characters.SPACE);
rowString.append(Characters.SPACE);
rowString.append(currentRow.getStringVal());

// Draw the text.
g.drawText(rowString.toString(), 0, y, 0, w);

}

public Object get(ListField list, int index) {
return _listElements.elementAt(index);
}

public int indexOfList(ListField list, String prefix, int string) {
return _listElements.indexOf(prefix, string);
}

public int getPreferredWidth(ListField list) {
return Display.getWidth();
}

}

private class ChecklistData {
private String _stringVal;
private boolean _checked;

/*
* ChecklistData() { _stringVal = ""; _checked = false; }
*/

ChecklistData(String stringVal, boolean checked) {
_stringVal = stringVal;
_checked = checked;
}

// Get/set methods.
private String getStringVal() {
return _stringVal;
}

private boolean isChecked() {
return _checked;
}

/*
* private void setStringVal(String stringVal) { _stringVal = stringVal;
* }
*
* private void setChecked(boolean checked) { _checked = checked; }
*/

// Toggle the checked status.
private void toggleChecked() {
_checked = !_checked;
}
}

/* (non-Javadoc)
* @see net.rim.device.api.ui.container.MainScreen#makeMenu(net.rim.device.api.ui.component.Menu, int)
*/
protected void makeMenu(Menu menu, int instance) {

// TODO Auto-generated method stub
Field focus = UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus();
if(focus == _checkList)
{
//The _checkList ListField instance has focus.
//Add the _toggleItem MenuItem.
menu.add(_toggleItem);
}


super.makeMenu(menu, instance);
}
}

最佳答案

如果您仔细调试代码,您可以轻松找到 ClassCastException。您在 String 方法的 _listElements Vector 中添加了 createField() 对象,并尝试将 String 对象转换为 ChecklistData 方法中的 invokeAction(int action) 对象:

ChecklistData data = (ChecklistData) _listElements.elementAt(index);

您应该在 ChecklistData vector 中插入 _listElements 对象,之后您显然无法在 String 方法中将这些元素转换为 drawListRow() :

String text = (String) _listElements.elementAt(index);
<小时/>

我已经修复了您的代码,请参阅下面(不知道您想对 bullet_arrow.png ...所以,我在修改了一下之后注释掉了该部分):

import java.util.Vector;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;

public final class ListDemoScreen extends MainScreen {

private Vector _listElements;

ListField list;
private ListField _checkList;
private MenuItem _toggleItem;

public ListDemoScreen() {
super(Manager.NO_VERTICAL_SCROLL);
// Set the displayed title of the screen
setTitle("List Demo 1");
add(new LabelField("Fruits List", LabelField.FIELD_HCENTER));
add(new SeparatorField());

_listElements = new Vector();
add(new SeparatorField());
list = new ListField();
ListCallback _callback = new ListCallback(this);

list.setCallback(_callback);
list.setSize(4);
int index = list.getSelectedIndex();

add(list);

createField();

}

protected void createField() {
// String itemOne = "Apple";
// String itemTwo = "Blackberry";
// String itemthree = "Grapes";
// String itemfour = "Banana";
ChecklistData itemOneCheckList = new ChecklistData("Apple", false);
ChecklistData itemTwoCheckList = new ChecklistData("Blackberry", false);
ChecklistData itemThreeCheckList = new ChecklistData("Grapes", false);
ChecklistData itemFourCheckList = new ChecklistData("Banana", false);

_listElements.addElement(itemOneCheckList);
_listElements.addElement(itemTwoCheckList);
_listElements.addElement(itemThreeCheckList);
_listElements.addElement(itemFourCheckList);
reloadList();

}

private void reloadList() {
list.setSize(_listElements.size());
}

public boolean invokeAction(int action) {
switch (action) {
case ACTION_INVOKE: // Trackball click.
int index = list.getSelectedIndex();
ChecklistData data = (ChecklistData) _listElements.elementAt(index);
data.toggleChecked();
_listElements.setElementAt(data, index);
list.invalidate(index);
return true; // We've consumed the event.
}
return super.invokeAction(action);

}

class ListCallback implements ListFieldCallback {
ListDemoScreen listDemoScreen;

public ListCallback(ListDemoScreen listDemoScreen) {
this.listDemoScreen = listDemoScreen;

}

public void drawListRow(ListField list, Graphics g, int index, int y,
int w) {

ChecklistData checkListData = (ChecklistData) _listElements.elementAt(index);
String text = checkListData.getStringVal();
// g.drawText(text, 60, y + 5, 0, w);
// Bitmap bitm = null;
// if(checkListData.isChecked()) {
// bitm = Bitmap.getBitmapResource("bullet_arrow1.png");
// } else {
// bitm = Bitmap.getBitmapResource("bullet_arrow2.png");
// }

// w = bitm.getWidth();
// int h = bitm.getHeight();
//
// int xpos = 2;
// int heightDifference = ( list.getRowHeight(index) - h );
// int ypos = y + ( heightDifference > -1 ? heightDifference : 0 ) / 2;
//
// g.drawBitmap( xpos, ypos, w, h, bitm, 0, 0 );

// xpos = w + 20;

ChecklistData currentRow = (ChecklistData) this.get(list, index);

StringBuffer rowString = new StringBuffer();

// If it is checked draw the String prefixed with a checked box,
// prefix an unchecked box if it is not.
if (currentRow.isChecked()) {
rowString.append(Characters.BALLOT_BOX_WITH_CHECK);
} else {
rowString.append(Characters.BALLOT_BOX);
}

// Append a couple spaces and the row's text.
rowString.append(Characters.SPACE);
rowString.append(Characters.SPACE);
rowString.append(currentRow.getStringVal());

// Draw the text.
g.drawText(rowString.toString(), 0, y, 0, -1);

}

public Object get(ListField list, int index) {
return _listElements.elementAt(index);
}

public int indexOfList(ListField list, String prefix, int string) {
return _listElements.indexOf(prefix, string);
}

public int getPreferredWidth(ListField list) {
return Display.getWidth();
}

}

private class ChecklistData {
private String _stringVal;
private boolean _checked;

/*
* ChecklistData() { _stringVal = ""; _checked = false; }
*/

ChecklistData(String stringVal, boolean checked) {
_stringVal = stringVal;
_checked = checked;
}

// Get/set methods.
private String getStringVal() {
return _stringVal;
}

private boolean isChecked() {
return _checked;
}


// Toggle the checked status.
private void toggleChecked() {
_checked = !_checked;
}

}

/*
* (non-Javadoc)
*
* @see
* net.rim.device.api.ui.container.MainScreen#makeMenu(net.rim.device.api
* .ui.component.Menu, int)
*/
protected void makeMenu(Menu menu, int instance) {

// TODO Auto-generated method stub
Field focus = UiApplication.getUiApplication().getActiveScreen()
.getLeafFieldWithFocus();
if (focus == _checkList) {
// The _checkList ListField instance has focus.
// Add the _toggleItem MenuItem.
menu.add(_toggleItem);
}

super.makeMenu(menu, instance);
}

}

希望对您有所帮助。

关于java - 如何在列表字段中单击时添加复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10481271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com