我正在一个应用程序中工作,我必须在列表视图中显示联系人。我已经在Vertical Field Manager和Label Fields的帮助下创建了列表视图。
我有一个电子邮件字段和电话号码字段,我必须在其上调用点击监听器。
我已经在我的类上实现了监听器,但是对于click事件失败了。不要显示任何点击事件。
目前,我正在使用以下代码:
public class CustomListView extends VerticalFieldManager implements FieldChangeListener{
private VerticalFieldManager mVFM=null;
private Font _allFonts=null;
private Font _headingFonts=null;
/**
* @param resultVec
*/
public CustomListView() {
super(VerticalFieldManager.VERTICAL_SCROLL);
setMargin(0, 0, 10, 0);
int resolutionWidth = Display.getWidth(); // PLANNING FOR THREE FIXED RESOLUTIONS 320, 360 AND 480
//HANDLE WITH THE CODE DIFFERENT DEVICES
if(resolutionWidth>=480){
_allFonts = ApplicationFont.explainationFont_18;
_headingFonts = ApplicationFont.labelFont_18;
}else if(resolutionWidth >=360){
_allFonts = ApplicationFont.explainationFont_18;
_headingFonts = ApplicationFont.labelFont_18;
}else{//RESOLUTION WIDTH <=320
_allFonts = ApplicationFont.explainationFont_15;
_headingFonts = ApplicationFont.labelFont_15;
}
int maxLength = Constants.Country_List.length;
int minLength = 0;
if(maxLength >= 0){
for (int i = minLength; i < maxLength; i++) {
VerticalFieldManager vfm = getVerticalFieldManager();
vfm.setMargin(0, 10, 0, 10);
LabelField countryLabel = new LabelField();
countryLabel.setText(Constants.Country_List[i]);
//countryLabel.setFont(_allFonts);
countryLabel.setMargin(10, 0, 10, 0);
HorizontalFieldManager countryHFM = new HorizontalFieldManager(Manager.USE_ALL_WIDTH);
countryHFM.add(countryLabel);
countryHFM.setBackground(BackgroundFactory
.createSolidBackground(Constants.LightGreyBgColorCode));
LabelField addressLabel = new LabelField();
addressLabel.setText(Constants.Address_List[i]);
//addressLabel.setFont(_allFonts);
LabelField stateLabel = new LabelField();
stateLabel.setText(Constants.State_List[i]);
// stateLabel.setFont(_allFonts);
LabelField cityLabel = new LabelField(){
protected void paint(Graphics graphics) {
graphics.setColor(Constants.DarkGreyTextColorCode);
super.paint(graphics);
}
};
cityLabel.setText(Constants.City_List[i]);
//cityLabel.setFont(_allFonts);
LabelField phoneLabel = new LabelField(){
protected void paint(Graphics graphics) {
graphics.setColor(Constants.DarkGreyTextColorCode);
super.paint(graphics);
}
};
phoneLabel.setText("Phone(s):");
//phoneLabel.setFont(_allFonts);
LabelField numLabel = new LabelField(){
protected void paint(Graphics graphics) {
graphics.setColor(Constants.blueTextColor);
super.paint(graphics);
}
};
numLabel.setText(Constants.Phone_List[i]);
//numLabel.setFont(_allFonts);
HorizontalFieldManager phoneNumberHFM = new HorizontalFieldManager();
phoneNumberHFM.add(phoneLabel);
phoneNumberHFM.add(numLabel);
LabelField emailLabel = new LabelField(){
protected void paint(Graphics graphics) {
graphics.setColor(Constants.DarkGreyTextColorCode);
super.paint(graphics);
}
};
emailLabel.setText("Email:");
//emailLabel.setFont(_allFonts);
LabelField emailIdLabel = new LabelField(){
protected void paint(Graphics graphics) {
graphics.setColor(Constants.blueTextColor);
super.paint(graphics);
}
};
emailIdLabel.setText(Constants.Email_List[i]);
//emailIdLabel.setFont(_allFonts);
HorizontalFieldManager emailIdHFM = new HorizontalFieldManager();
emailIdHFM.add(emailLabel);
emailIdHFM.add(emailIdLabel);
mVFM = new VerticalFieldManager(USE_ALL_WIDTH /*|FOCUSABLE*/);
mVFM.setMargin(20, 0, 20, 0);
mVFM.add(countryHFM);
mVFM.add(addressLabel);
mVFM.add(stateLabel);
mVFM.add(cityLabel);
mVFM.add(phoneNumberHFM);
mVFM.add(emailIdHFM);
mVFM.add(new NullField(FOCUSABLE));
vfm.add(mVFM);
/*SeparatorField separater = new SeparatorField(Constants.LightGreyBgColorCode);
separater.setMargin(0, 10, 0, 10);*/
vfm.add(new SeparatorField(Constants.LightGreyBgColorCode));
add(vfm);
}
}
}
private VerticalFieldManager getVerticalFieldManager() {
VerticalFieldManager verticalFieldManager = new VerticalFieldManager(
VerticalFieldManager.FOCUSABLE
| VerticalFieldManager.FIELD_HCENTER
| VerticalFieldManager.VERTICAL_SCROLL
| VerticalFieldManager.USE_ALL_WIDTH) {
protected boolean touchEvent(TouchEvent message) {
if (message.getEvent() == TouchEvent.CLICK) {
navigationClick(0, 0);
}
return super.touchEvent(message);
}
public boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
protected void onFocus(int direction) {
super.onFocus(direction);
setBackground(BackgroundFactory
.createSolidBackground(/*0x186DEF*/Constants.WhiteBgColorCode));
invalidate();
}
protected void onUnfocus() {
super.onUnfocus();
setBackground(BackgroundFactory
.createSolidBackground(Constants.WhiteBgColorCode));
invalidate();
}
};
return verticalFieldManager;
}
public void fieldChanged(Field field, int context) {
}
}
请建议,如何为列表视图构建一个点击监听器。
答案 0 :(得分:1)
我认为你还没有完全理解FieldChangeListener需要什么。
这是一个使用FieldChangeListener的简单实现(请注意,这只适用于OS 5.0及更高版本,因为之前ZoomScreen不可用。)
public MyScreen() {
// Set the displayed title of the screen
setTitle("Test ZoomScreen");
ButtonField zoomButton = new ButtonField("Zoom Screen Test");
FieldChangeListener listener=new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
EncodedImage ei = EncodedImage.getEncodedImageResource("Koala.jpg");
UiApplication.getUiApplication().pushScreen(new ZoomScreen(ei));
}
};
zoomButton.setChangeListener(listener);
add(zoomButton);
}
重要的步骤是:
我认为如果您查看此实现,您会发现您错过了一些步骤,特别是您似乎已经完成了(1),但没有(2)或(3)。而不是我纠正你的代码,我建议你使用我希望上面给你的洞察力,并纠正你自己的代码。祝你好运。