how to select all items at a time when cliking on select all menuitem for a listfield checkbox in blackberry - blackberry

I want to select all items in a listfield at a time when cliking on the select all menu item in the application.i tried with my code like this..
selectall = new MenuItem("Selectall", 200, 10){
public void run(){
int elementLength = _elements.length;
for(int count = 0; count < elementLength; ++count)
{
_listData.addElement(new ChecklistData(_elements[count], true));
listField.insert(count);
}
}
};
but iam getting the result with old list also.below the old list this new list is coming with checked.how to solve that.please give your ideas.and where iam doing the mistake.thanks in advance..

Your use of listField.insert means that you're adding new ChecklistData objects to the list. That's why you're getting a completely new list underneath your previous one. Instead of adding to _listData, go through it and set the ChecklistData to be checked.
It looks like that is a custom class, so I don't know what it will take for you to do that. If you used CheckboxFields in _listData, you could do it like this:
for (Enumeration e = _listdata.elements() ; e.hasMoreElements() ;) {
CheckboxField c = (CheckboxField)e.nextElement();
c.setChecked(true);
}

Thanks for your all suggestions. i solved my problem now..the solution is..
selectall = new MenuItem("Selectall", 200, 10){
public void run(){
int elementLength = _elements.length;
for(int count = 0; count < elementLength; ++count)
{
_listData.setElementAt(new ChecklistData(_elements[count], true), count);
}
}
};

Related

Blackberry verticalfieldmanager partial screen scrolling with label fields

I am trying to create a set of FAQ questions and answers using a bunch of LabelFields in a VFM. Issue is that when I try to scroll, it jumps to the bottom of the list and doesn't show the mid-section questions.
public class HelpTab implements ITabAreaLayout, ScrollChangeListener {
public String[] GetQandAs() {
String[] QandAs = new String[22];
QandAs[0] = "Q. ....";
QandAs[1] = "A. ....";
....
....
QandAs[20] = "Q. ...";
QandAs[21] = "A. ....";
return QandAs;
}
VerticalFieldManager _vfm;
public VerticalFieldManager GetLayout() {
_vfm = new VerticalFieldManager(Field.FIELD_LEFT
| Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
_vfm.add(UIElements.GetTitleArea(" ? FAQ"));
String[] QandAs = GetQandAs();
for (int i = 0; i < QandAs.length; i++) {
LabelField lblQandA = null;
if ((i % 2) == 0) {
lblQandA = UIElements.GetQuestionLabel(QandAs[i]);
} else {
lblQandA = UIElements.GetAnswerLabel(QandAs[i]);
}
_vfm.add(lblQandA);
}
_vfm.add(new NullField(NullField.FOCUSABLE)); // for scrolling
return _vfm;
}
public void scrollChanged(Manager manager, int newHorizontalScroll,
int newVerticalScroll) {
if (_vfm != null){
_vfm.setVerticalScroll(newVerticalScroll);
}
}
public class HomeScreen extends MainScreen
{
public HomeScreen() {
super(Manager.FIELD_HCENTER | Screen.NO_VERTICAL_SCROLL);
_vfmMain = new VerticalFieldManager();
// add header image
_vfmMain.add(UIElements
.GetBitmapField(UIElements.IMG_HEADER, false));
// add tab strip
_vfmMain.add(MakeTabStrip());
add(_vfmMain);
_vfmTabArea = new HelpTab().GetLayout();
add(_vfmTabArea);
}
}
I was not able to find much help on setVerticalScroll usage, maybe that is the reason for this issue.
Please advise.
Thanks
In your code, you added the focusable null field at the end position of the loop. so if you scroll, it will goto the last element. If you add the focusable field to- After first question, then after second question, ..... so it will scroll one by one.
Try This code -
for (int i = 0; i < QandAs.length; i++) {
LabelField lblQandA = null;
if ((i % 2) == 0) {
lblQandA = UIElements.GetQuestionLabel(QandAs[i]);
} else {
lblQandA = UIElements.GetAnswerLabel(QandAs[i]);
}
_vfm.add(lblQandA);
_vfm.add(new NullField(NullField.FOCUSABLE)); //after each element, add a focusable null field.
}
As Signare has pointed out, the issue here is probably related to your LabelFields not being focusable, which are they are not by default. One answer is to add NullFields as has been suggested. However I suspect you actually want these to be focusable, so the user can click on the one they would like more information on. So make your LabelFields Focusable, by setting the style, for example
LabelField lab = new LabelField("Label", LabelField.FOCUSABLE);
Alternatively, and to my mind preferably, use RichTextField instead of LabelField. This will give you scrolling line by line, LabelField focuses on the whole text.

Stackoverflow exception in blackberry CheckBoxField

I am implementing a simple app, where in the registration page user can select news categories. Requirements are below
All the categories are the CheckBoxField's. User have to select at least one category.
Select all CheckBox will allow to select all/deselect all categories CheckBox.
If user manually selects all checkbox fields then "Select All" checkbox must be selected.
Approaches: I have created the categories checkbox in a loop.
for(int i=0;i<interests.length;i++){
allFields[i] = new ColorCheckBoxField(interests[i], false, checkBoxStyle | USE_ALL_WIDTH);
allFields[i].setCookie(i+"");
allFields[i].setFont(bodyFont);
allFields[i].setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ColorCheckBoxField tempChoice = (ColorCheckBoxField)field;
int index =Integer.parseInt(tempChoice.getCookie().toString().trim());
//set the selection
if(tempChoice.getChecked()){
parent.selectInterest(index);
}
boolean flag = true;
int[] intrests = parent.getSelectedInterest();
for (int i = 0; i < intrests.length; i++) {
if(intrests[i]==0){
flag = false;
}
}
if(flag==true){
selectAll.setChecked(flag); // select all is Checkbox object
}else{
selectAll.setChecked(false);
}
}
});
vfm.add(allFields[i]);
}
My selectAll checkbox logic is
selectAll = new ColorCheckBoxField("Select All", false, checkBoxStyle | USE_ALL_WIDTH);
selectAll.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ColorCheckBoxField temp = (ColorCheckBoxField) field;
//if (context == FieldChangeListener.PROGRAMMATIC ) {
checkAll(temp.getChecked()); // it loops through all checkbox and set them checked
//}
}
});
innerHfm.add(selectAll);
I understand the problem, its due to infinite loop. I have used "FieldChangeListener.PROGRAMMATIC" but that wont help because i want the field listener to work for both pragmatically and manually. I don't have any option left to fix. Any hack will help me?
That's correct that you have to use FieldChangeListener.PROGRAMMATIC. But you have to use it with interest checkboxes instead of using it for selectAll checkbox.
Please add one defensive check to FieldChangeListener for interest checkboxes:
if ( nonProgrammaticChange(context) ) {
ColorCheckBoxField tempChoice = (ColorCheckBoxField)field;
int index = Integer.parseInt(tempChoice.getCookie().toString().trim());
...
}
Where nonProgrammaticChange is:
private boolean nonProgrammaticChange (int context) {
return (context & FieldChangeListener.PROGRAMMATIC) != FieldChangeListener.PROGRAMMATIC;
}
I see bug in your code - you don't clear interest in parent if checkbox is unchecked.
Minor improvements as for me - use Vector where you'll store indexes of selected checkboxes. This will allow to replace this code:
boolean flag = true;
int[] intrests = parent.getSelectedInterest();
for ( int i = 0; i < intrests.length; i++ ) {
if( intrests[i] == 0 ) {
flag = false;
}
}
To this code:
selectedInterestIndexes.size() == interests.length
And probably this will give you less iteration in other places.
As well I would work more on removal of duplicates and code readability.

Distinguish events from different button fields

I have a for loop that creates ButtonFields with identical text values. I want to get a distinct event from each of those buttons, which tells me which index of the for loop created the button. I don't want to create an anonymous class for each ButtonField.
If they are going one by one (that I'm assuming from your post) you could remember index of the first one in use next code in your fieldChanged method:
if (field instanceof ButtonField) {
int buttonIndex = field.getManager().getFieldIndex(field) - zeroButtonInex;
}
Don't forget to assign FieldChangeListener to each of these buttons.
Or sure you could make your new class from ButtonField (could by anonymous) where you could save index and have getter for it.
You have add the buttons to an array. I will give you an idea to try this:
private ButtonField buttonsObj[];
In your code before the for loop you know the number of buttons, so you can initialize the array length.
int size = 10;
buttonsObj = new ButtonFields[size];
for(int i = 0; i < size; i++)
{
buttonsObj[i] = new ButtonFields["btn"];
buttonsObj[i].setChangeListener(this);
add(buttonsObj[i]);
}
public void fieldChanged(Field field, int context) {
for(int i=0;i<size;i++) {
if(field == buttonsObj[i]) {
// you can trigger your event
}
}
}

c# drop down list selected count

yesterday i asked one question and got the answer from our friend here, and i ran successfully, also it have one problem with it. "Yester day My question is, when we selecting the drop down list then it should be shown by a label as "1" at very first time, again it'll be increas by selection", this is what the answer i got..,
static int count = 0;
private void bind()
{
ArrayList ar = new ArrayList();
ar.Add("first");
ar.Add("Second");
ar.Add("Third");
ar.Add("Four");
ar.Add("Five");
ar.Add("Six");
ar.Add("Seven");
CCddl.DataSource = ar;
CCddl.DataBind();
}
protected void CCddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (count == 0) count = 1;
Label12.Text = count++.ToString();
}
this code worked, but once the running window getting closed, then it lose the continuation, i mean again application getting run it'll shown again "1". But exactly what i want is, the number continuation should be end when the system day has change.
Try using the Application Settings feature. I added two user settings CountDate and Count in the Project->Property's->Settings and changed your SelectedIndexChangedEvent to
protected void CCddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (count == 0) count = 1;
Label12.Text = count++.ToString();
Properties.Settings.Default.CountDate = DateTime.Now.Date;
Properties.Settings.Default.Count = count;
Properties.Settings.Default.Save();
}
And right before you call your Bind Method during your form initialization put something like this.
if(Properties.Settings.Default.CountDate.Date != DateTime.Now.Date)
{
Properties.Settings.Default.Count = 0;
Properties.Settings.Default.CountDate = DateTime.Now.Date;
Properties.Settings.Default.Save();
}
else
count = Properties.Settings.Default.Count;
bind();
Added the Property Settings Image
You should somehow store the value in a database or something. With the date value. Then when the datechanges you just reset the value.

Not getting the text from Edit Field

In my Application, i am adding a check box, a label field and a Edit Field in a Grid Field manager. Then this grid Field manager, i am adding multiple times in Vertical Field manager. So it is looking like List of items. Now when i checked five check box, i am trying to get the text of the correspondent edit field.
This is the code for Grid Field Manager:
int c[] = {screenWidth/6, (screenWidth)/3, (screenWidth)/2};
gm = new GridFieldManager(c, Manager.VERTICAL_SCROLL);
Logger.out("Grocery", "Here it is coming"+i);
cbfChecked = new CustomCheckBoxField();
cbfChecked.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
if(checked[i] == false)
{
checked[i] = true;
}
else if(checked[i] == true)
{
checked[i] = false;
Logger.out("Grocery", "It is UnChecked" +checked[i]);
}
}
});
gm.add(cbfChecked);
Logger.out("Grocery", "Adding first Label Field");
LabelFieldCustom lfFrom = new LabelFieldCustom((String) m_vtrItems.elementAt(i),Color.BROWN,FONT_FAMILY_0_SF_AS_16,Field.FIELD_LEFT);
gm.add(lfFrom);
Logger.out("Grocery", "Adding second Label Field");
efcAmount = new EditFieldCustom(Bitmap.getBitmapResource("dob_text_box.png"), 25);
efcAmount.setMargin(new XYEdges(30, 0, 0, 0));
gm.add(efcAmount);
return gm;
Here i am adding the grid field manager multiple times:
for (int i = 0;i < m_vtrItems.size();i++)
{
vfm.add(getRow(i));
vfm.add(new SeparatorField(SeparatorField.NON_FOCUSABLE));
}
Please help me.
I solve the problem. Now i am taking the Edit Field array.

Resources