我还没有看到很多关于TableView的解释例子。当然,我出来了它的基本实现(通过使用模板,模型等)。但是现在我想实现部分,按公共字段对行进行分组。例如日期。这就是我提出这个问题的原因,我如何在邮件应用程序中实现它们,或者可能有链接可以帮助(已经搜索过它)或一些代码示例?
编辑:
我想解释一下我到目前为止所做的事情。
目的是按照您想要的任何条件对字段进行排序,然后循环它们并使用变量来注册我们正在排序的字段的当前值,在这种情况下它是一个String。这种方式,当该变量的最后一个值发生变化时,意味着该类别确实也发生了变化(因为它是一个排序列表)。根据tableModel,我们可以添加一行,其中包含我们要为该类别设置的标题。
这样在getDataFields方法中,我们将测试我们检索的数据是否只有一个值,然后根据我们想要在表中显示的部分返回Fields[]
数组。现在这就是我尝试过的,但是当绘制类别行时,tableView只是停止循环其他字段(显然抛出异常但是因为Blackberry API和模拟器错误管理非常特别,所以我没有收到任何消息看看发生了什么)。
这是我目前正在使用的代码,希望任何人都可以检查我做错了什么,并建议我可以改进代码。
public class AutorizacionesScreen extends MainScreen implements HttpDelegate {
private final int ROW_HEIGHT = 30;
TableView tableView;
TableModel tableModel;
ActivityIndicatorView activityView;
SimpleSortingVector autorizaciones;
VerticalFieldManager manager;
Font listFont;
PollAutorizacionesThread pollThread;
public AutorizacionesScreen() {
super(NO_VERTICAL_SCROLL);
setTitle(new LabelField("Workflow Details", Field.FIELD_HCENTER
| Field.NON_FOCUSABLE));
manager = new VerticalFieldManager();
tableModel = new TableModel();
tableView = new TableView(tableModel);
WorkflowDataTemplate template = new WorkflowDataTemplate(tableView);
template.createRegion(new XYRect(0, 0, 1, 1));
template.createRegion(new XYRect(1, 0, 1, 1));
template.createRegion(new XYRect(2, 0, 1, 1));
template.createRegion(new XYRect(3, 0, 1, 1));
template.setColumnProperties(0, new TemplateColumnProperties(10,
TemplateColumnProperties.PERCENTAGE_WIDTH));
template.setColumnProperties(1, new TemplateColumnProperties(15,
TemplateColumnProperties.PERCENTAGE_WIDTH));
template.setColumnProperties(2, new TemplateColumnProperties(25,
TemplateColumnProperties.PERCENTAGE_WIDTH));
template.setColumnProperties(3, new TemplateColumnProperties(50,
TemplateColumnProperties.PERCENTAGE_WIDTH));
template.setRowProperties(0, new TemplateRowProperties(ROW_HEIGHT));
template.useFixedHeight(true);
tableView.setDataTemplate(template);
TableController controller = new TableController(tableModel, tableView);
controller.setFocusPolicy(TableController.ROW_FOCUS);
tableView.setController(controller);
FontFamily fontFamily;
try {
fontFamily = FontFamily.forName("BBClarity");
listFont = fontFamily.getFont(FontFamily.CBTF_FONT, 15);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
activityView = new ActivityIndicatorView(FIELD_HCENTER,
new HorizontalFieldManager());
Bitmap bitmap = Bitmap.getBitmapResource("waiting.png");
activityView.createActivityImageField(bitmap, 12, 0);
ActivityImageField animation = activityView.getAnimation();
animation.setPadding(0, 5, 0, 0);
activityView.setLabel("Por favor espere...");
manager.add(activityView);
manager.add(tableView);
add(manager);
checkServerSession();
// if(!ApplicationPreferences.getInstance().isLoggedIn()){
// UiApplication.getUiApplication().invokeLater(new Runnable() {
//
// public void run() {
// UiApplication.getUiApplication().popScreen(AutorizacionesScreen.this);
//
// }
// });
// return;
// }
pollThread=new PollAutorizacionesThread(30000);
pollThread.start();
}
private void checkServerSession() {
if (!ApplicationPreferences.getInstance().isLoggedIn()) {
UiApplication.getUiApplication().invokeAndWait(new Runnable() {
public void run() {
ApplicationPreferences.getInstance().login();
}
});
}
}
public void reloadData() {
if (!ApplicationPreferences.getInstance().isLoggedIn()) {
String url = Properties.getInstance().getProperties()
.get("resource.base").toString()
+ Properties.getInstance().getProperties()
.get("ec.com.smx.workflow.autorizaciones.activas")
.toString();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
if(manager.getField(0) instanceof TableView){
manager.insert(activityView, 0);
}
}
});
HttpHelper helper = new HttpHelper(url, null, this);
helper.setOperation(HttpHelper.GET);
helper.start();
}
}
private class PollAutorizacionesThread extends Thread{
private long pollTime;
private boolean _stop=false;
public PollAutorizacionesThread(long sleepTime) {
pollTime=sleepTime;
}
public void run(){
while(!_stop){
try {
reloadData();
Thread.sleep(pollTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void stop(){
_stop=true;
}
}
private class WorkflowDataTemplate extends DataTemplate {
public WorkflowDataTemplate(TableView view) {
super(view, 1, 4);
}
public Field[] getDataFields(int modelRowIndex) {
Object[] data = (Object[]) ((TableModel) getView().getModel())
.getRow(modelRowIndex);
Field[] fields=null;
if(data.length==1){
fields=new Field[4];
fields[0]=new LabelField("",DrawStyle.ELLIPSIS
| Field.FIELD_VCENTER);
fields[1] = new LabelField("",DrawStyle.ELLIPSIS
| Field.FIELD_VCENTER);
fields[2]=new LabelField(data[0],DrawStyle.ELLIPSIS
| Field.FIELD_VCENTER);
fields[3]=new LabelField("",DrawStyle.ELLIPSIS
| Field.FIELD_VCENTER);
return fields;
}
fields = new Field[4];
fields[0] = new BitmapField(
Bitmap.getBitmapResource("envelope.png"),
Field.FIELD_VCENTER);
LabelField newField = new LabelField(data[1],
LabelField.FIELD_VCENTER | LabelField.FIELD_RIGHT) {
public void paint(Graphics g) {
g.drawText(getText(), 0, (ROW_HEIGHT - getFont()
.getHeight()) / 2);
}
protected void layout(int width, int height) {
super.layout(
Math.min(width,
this.getFont().getAdvance(this.getText())),
40); // height of the bitmap);
setExtent(
Math.min(width,
this.getFont().getAdvance(this.getText())),
40); // height of the bitmap);
}
};
newField.setFont(listFont);
fields[1] = newField;
fields[2] = new LabelField(data[2], DrawStyle.ELLIPSIS
| Field.FIELD_VCENTER);
fields[3] = new LabelField(data[3], DrawStyle.ELLIPSIS
| Field.FIELD_VCENTER);
return fields;
}
}
private class AutorizacionComparator implements Comparator{
public int compare(Object o1, Object o2) {
Calendar c1=Calendar.getInstance();
c1.setTime(((Autorizacion)o1).getFechaCreacionAutorizacion());
Calendar c2=Calendar.getInstance();
c2.setTime(((Autorizacion)o2).getFechaCreacionAutorizacion());
long deltaSeconds = (c2.getTime().getTime()-c1.getTime().getTime())/1000;
if(deltaSeconds==0) return 0;
if(deltaSeconds>0) return -1;
return 1;
}
}
public void didReceiveData(byte[] data) {
AutorizacionesParser autorizacionesParser = new AutorizacionesParser();
try {
autorizacionesParser.initialize(data);
Vector autorizacionesVector=autorizacionesParser.readObjects();
autorizaciones=new SimpleSortingVector();
autorizaciones.setSortComparator(new AutorizacionComparator());
for (int i = 0; i < autorizacionesVector.size(); i++) {
autorizaciones.addElement(autorizacionesVector.elementAt(i));
}
autorizaciones.setSort(true);
autorizaciones.reSort();
for (int i = 0; i < autorizacionesVector.size(); i++) {
System.out.println(((Autorizacion)autorizaciones.elementAt(i)).getFechaCreacionAutorizacion());
}
} catch (ParserException e) {
e.printStackTrace();
}
int count=tableModel.getNumberOfRows();
for (int i = 0; i < count; i++) {
tableModel.removeRowAt(0);
}
Calendar dateCalendar=Calendar.getInstance();
String lastDay="";
for (int i = 0; i < autorizaciones.size(); i++) {
Autorizacion item = (Autorizacion) autorizaciones
.elementAt(i);
dateCalendar.setTime(item.getFechaCreacionAutorizacion());
String day=String.valueOf(dateCalendar.get(Calendar.DAY_OF_MONTH))+"//"+
String.valueOf(dateCalendar.get(Calendar.MONTH)+1)+"//"+
String.valueOf(dateCalendar.get(Calendar.YEAR));
if(!lastDay.equals(day)){
lastDay=day;
tableModel.addRow(new Object[]{day});
}
tableModel.addRow(new Object[] { item.getEstado().toString(),
(new Integer(item.getNumeroAutorizacion()).toString()),
item.getFechaCreacionAutorizacion().toString(),
item.getObservacionAutorizacion()});
}
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try{
if(manager.getField(0) instanceof ActivityIndicatorView){
manager.delete(activityView);
tableView.invalidate();
}
}catch (NullPointerException e) {
// TODO: WORKAROUND BECAUSE RIM APIS ARE BUGGY
}
}
});
}
public void didReceiveUnauthorizedResponse() {
// TODO: HANDLE ERRORS
}
public void didReceiveResponse(int statusCode) {
// TODO: IN CASE OF POST REQUEST THIS COULD BE USED
}
public boolean onClose() {
if(pollThread!=null)
pollThread.stop();
return super.onClose();
}
}
答案 0 :(得分:1)
我建议你做以下事情:
添加更多带错误记录的try-catch内容。在这种特殊情况下,尝试捕获(例外e)只是为了确保您不会遗漏任何内容,并且记录每个捕获。
我建议在try-catch中完全放入以下方法:
AutorizacionesScreen.checkServerSession()
AutorizacionesScreen.reloadData()
AutorizacionComparator.compare()
AutorizacionesScreen.didReceiveData()
与我们分享例外情况。
你提出的代码是不可操作的,因为那里有很多自定义类(ex Autorizacion,AutorizacionesParser,ParserException,HttpHelper,Properties,ApplicationPreferences,HttpDelegate),还需要一些服务器端,是吗?
您应该尝试帮助其他人帮助您:将您的问题与项目分开。
为此,创建一个新的Eclipse工作区并在那里创建新的BlackBerry项目,将此代码放在那里并删除所有不相关的类。如果您需要其他类,请将其放入相同的* .java文件中。
如果可能的话,删除服务器调用,如果没有,用模型数据替换它们(只是在你发布的同一个类文件中创建一些数据对象)。
完成这项工作后,您很可能会得到我们的帮助!