Hashmap get tag crashes - java

I have an array named checkBox that contains checkboxes items in my xml. I also have a HashMap that stores the value of each of the checkboxes in Double.
Now the problem is when I try to getTag() inside the loop that checks the selected checkboxes by user, it crashes without error shown. I do not understand what went wrong.
double priceSum= 0.0;
for (CheckBox cb:checkBoxes) {
cb.setEnabled(true);
if (cb.isChecked()) {
count++;
toppingsSelection.add(cb); //adding selected items into arrayList
}
}
for (CheckBox items: toppingsSelection) {
text = text + items.getText().toString()+ ", ";
priceSum += hashMap.get(items.getTag()); //THIS CAUSES CRASHING
}
toppings.setText(text); displaying texts of checkboxes checked
price.setText(Double.toString(priceSum));
This is my hashmap:
hashMap.put("chkCheese", 2.50);
hashMap.put("chkPep", 3.50);
hashMap.put("chkChick", 2.00);
hashMap.put("chkBeef", 4.00);
hashMap.put("chkBlackOlives", 2.00);
hashMap.put("chkPine", 1.00);
hashMap.put("chkMushroom", 1.00);

It was a rookie mistake, I have to set tags to my checkboxes in my XML file

Related

JTable getModel().setValueAt removing first element from array

I'm encountering an odd bug while updating the contents of a JTable. I've got a two dimensional array which is storing arrays of values to be displayed by the table (I'm only displaying one row of data at a time). When the user clicks a button, the next set of values (along with the next set of column headers) is displayed, and when the user clicks another button, it displays the previous set. The problem is that the first time I call getModel().setValueAt, on the first array in the two dimensional array, it is removed.
Here's the code that updates the JTable:
for(int i = 0; i < formattedData[displayedDataSet].length - 1; i++) {
TableColumn col = displayTable.getTableHeader().getColumnModel().getColumn(i);
col.setHeaderValue(WeatherData.WEATHER_MAP_KEYS[displayedDataSet * 5 + i]);
displayTable.getTableHeader().resizeAndRepaint();
displayTable.getModel().setValueAt(formattedData[displayedDataSet][i], 0, i);
}
for(String s: formattedData[0]) {
System.out.println(s);
}
I expect the output to be the first array in formattedData, but it is the second. However, commenting out the line with displayTable.getModel().setValueAt causes the correct result to be output. Why is this happening, and how can I update the JTable while preserving my array?

Value from button to be set in the next available JTextField?

In my app, I need for values from buttons (the value is taken from an SQLite database) to be displayed in one of 13 JTextFields, and they can be in any order. How do I make it possible for the value to be displayed in the next available JTextField, if say the first one is empty?
The only thing I could think of was
if (textField.getText().isEmpty())
{
String text = String.valueOf(num);
textField.setText(textField.getText() + text);
}
What should I do next? How should I go around the else statement? Should I even use it?
Thanks in advance!
If I understand your question correctly,this should be the idea
JTextField[] fields = new JTextField[13];
firstText.setName("First Text") ;
.......
field[0] = firstText;
field[1] = SecondText;
//then add remaining textfields
for(JTextField txtField : fields) {
if(txtField.getText().equals("") ) {
// do whatever you want
}else{
// do whatever you want
}
}

How to Iterate over entire android listview

I have a listview where user choose how much for each item he wants, so I need to iterate over each row to get the name (Ovo, Presunto, Queijo, etc), the quantity (the number between the "+" and "-" buttons) and the price. This is already working fine, but I have the following problem: If you look at the bottom of the listview you can know that there are more items, in this picture there are 9 items, but there are 15, and if the user scrolls I will can not iterate over the "hidden" items of listview.
Today I have this code, where I try iterate over the elements of listview:
for (int i = 0; i < listView.getCount(); i++) {
v = listView.getChildAt(i);
if(v != null){
value = (TextView) v.findViewById(R.id.edOptionValue);
nome = (TextView) v.findViewById(R.id.tvOptionName);
tvPrice = (TextView) v.findViewById(R.id.tvPrice);
String price = tvPrice.getText().toString();
//more code here
}
}
listView.getCount() gets the correct number of items (15 items), but when i==10 listView.getChildAt(i) is getting null.
Is there some way to iterate over all items of a listview, incluing the "hidden" elements ?
You should not iterate the list views, instead you should keep all that data (price/quantity) in a list and iterate over that if you really need to do that. That list can be used to populate the ListView as well.

JTable stop cell editing without user click

I'm trying to solve a strange problem with my program. This program creates a series of GUI's and JTables that give a user the ability to generate an XML file. One of these tables is for creating the "statements". I won't get into detail as far as that except to say that the data is stored in multiple 2D arrays which are in turn stored in a hash map.
Here is what happens. When a user enters the Statement screen a JTable is generated using the contents from the 2D array. This data populates the cell's which the user is able to modify. One of these cells (and the most important) is the amount. The amounts they set for the rows much match another amount from another class.
At the bottom of the table is a "finished" button. When the user clicks this button the logic will check to see if the money amounts match. If they do then the program will update the 2D array with any changed values and dispose of the JTable.
My problem is that once a user updates a cell and clicks "finished" the last updates made do not work. Essentially the user must first click somewhere else in the table and THEN hit finished. I would like this action to happen automatically so that when the user clicks "finished" cell editing is stopped. Here is the code for the finished button:
finishedButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
//Creates another table model for the finished button logic.
DefaultTableModel dm = (DefaultTableModel)StatementGUI.tbl.getModel();
//Gets the total number of table rows.
int rows = dm.getRowCount();
//Creates a variable to store the statement transaction total.
double statementTransactionTotal=0;
//For each row in the table.
for(int i = 0; i < dm.getRowCount(); i++){
//Gets the total of all transactions in the table.
String currentTotal = tbl.getValueAt(i, 3).toString();
Double currentTotalDouble = Double.parseDouble(currentTotal);
statementTransactionTotal=statementTransactionTotal+currentTotalDouble;
}
//Creates a decimal format and applies the statement total.
DecimalFormat df = new DecimalFormat("0.00");
String currentTotalDF = df.format(statementTransactionTotal);
//Stops editing on the table so that the data can be used.
if(null != tbl.getCellEditor()){
tbl.getCellEditor().stopCellEditing();
}
//If the statement total matches the transaction total..
if(currentTotalDF.matches(ClearedMainGUI.currentTransactionAmount)){
//For each row in the table..
for(int i = 0; i < dm.getRowCount(); i++){
//Will replace the hash/array value with the table value.
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][0]=tbl.getValueAt(i, 0).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][1]=tbl.getValueAt(i, 1).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][2]=tbl.getValueAt(i, 2).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][3]=tbl.getValueAt(i, 3).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][4]=tbl.getValueAt(i, 4).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][5]=tbl.getValueAt(i, 5).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][6]=tbl.getValueAt(i, 6).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][7]=tbl.getValueAt(i, 7).toString();
}
//For each row in the table..
for(int i = rows - 1; i >=0; i--){
//Removes the current row so the table will be empty next time.
dm.removeRow(i);
}
//Removes the frame and goes back to the previous GUI.
frame.dispose();
//If the statement total and transaction total do not match..
}else{
JOptionPane.showMessageDialog(null, "The statement total entered: $"+statementTransactionTotal+" " +
"does not match the transaction total of: $"+ClearedMainGUI.currentTransactionAmount);
}
}
});
I think my problem is with this line:
if(null != tbl.getCellEditor()){
tbl.getCellEditor().stopCellEditing();
}
This only seems to work once the user has clicked another area of the table after editing a cell.
I appreciate the help!
My problem is that once a user updates a cell and clicks "finished" the last updates made do not work.
Check out Table Stop Editing for two approaches:
You can either:
Add code to the ActionListener:
if (table.isEditing())
table.getCellEditor().stopCellEditing();
or set a property on the table:
JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Edit:
When the user clicks finished all the cell data will be saved to a 2D array.
Why? All the data is already stored in the TableModel.
In any case you need to stop editing BEFORE you attempt to copy data from the TableModel to the Array.

Must Dislay the types not in for loop

Hi friends am using a code in which I particularly mention a few items to be mandatory for selection now if any of the item is not selected then on click of the button it must toast a message with that missing item. Below is the code.
imageTypeMandatory = new String[] {"A","F","K","I","PROOF","LAND","BANK","INVOICE"};
for(int iCopy=0;iCopy<imageTypeMandatory.length;iCopy++){ if(imageTypeMandatory[iCopy].trim().equalsIgnoreCase(dataOne.getString(0).trim())){
mandatoryCount++;
}
Here if i dint select any of the above items like A and F then i t must pop up with the missing item. Say "A and F"
You can place all elements in a Set, and in each iteration - delete the element from the set.
When the number of trials is done - the set contains all the element that were not chosen.
Something along the lines of:
String[] imageTypeMandatory = new String[] {"A","F","K","I","PROOF","LAND","BANK","INVOICE"};
Set<String> set = new HashSet<>();
for (String s : imageTypeMandatory) set.add(s.toLowerCase());
for(int iCopy=0;iCopy<imageTypeMandatory.length;iCopy++) {
set.remove(dataOne.getString(0).trim());
}
System.out.println(set); //or any other way to use the result
If ur if-statement. Put all missing Strings in a
String missing=missings+imageTypeMandatory[iCopy]
and then Toast the missing
Toast toast = Toast.makeText( getApplicationContext(), missing, Toast.LENGTH_SHORT).show();

Categories