Saving a JComboBox Selection to an ArrayList - java

I cannot retrieve the selection in the JComboBox that was selected by the user. the J ComboBox inclued a list of car registration numbers, which then the user has to select one and it will be added to the booking ArrayList. Unfortunately it is not working properly. and the booking is not being saved because of this. Please Help Me! Maybe I need to change the get Method for the combo box.
ArrayList BookingList = CarRentalSystem.BookingList;
ArrayList CarList = CarRentalSystem.CarList;
UsingFiles BookingFile = CarRentalSystem.BookingFile;
String [] regNums;
public NewBooking() {
regNums = new String[CarList.size()+1];
for (int i = 0; i< CarList.size();i++){
regNums[i] = ""+((Car)CarList.get(i)).getCarNum();
}
initComponents();
....
Booking book = new Booking();
String regNum = cmbCar.getActionCommand();
book.getRegNum();

Not easy to understand what u want can you add more code?
try this to convert your list into a String Array that u pass in as a argument to your combobox.
public String[] regNums() {
String tArray[] = null;
for (int i=0; i<carList.size(); i++){
//Convert into a String[] and put the arrayList values in it.
tArray = carList.toArray(new String[i]);
}
}

Related

How to append the id to the url after assigning the id to the String

Scenario: I need to append the id to the url.
What I have done :
I have taken the last id from the table and stored it in a list:
Then I get the text of the id and is Stored in a String.
List<WebElement> id = driver.findElements(By.xpath("(//table[contains(#class,'mat-table')]//tr/td[1])[last()]"));
int rowsize = id.size();
for(int i=0;i<rowsize;i++)
{
String text = id.get(i).getText();
System.out.println("Get the id:"+text);
Then I use that text and append it to the URL
String confirmationURL = "https://test-websites.net/#/email?type=confirm";
String newurl = confirmationURL+"&id=text"; = **This part iam giving the text as id ... which is
wrong and I need to enter the id which I got from the list ....**
driver.get(newurl);
So Basically the url should be like: https://test-websites.net /#/email?type=confirm&id=47474
Can someone pls give inputs on what should be done?
You can create a new list of URLS, and can use add method to append text.
List<WebElement> id = driver.findElements(By.xpath("(//table[contains(#class,'mat-table')]//tr/td[1])[last()]"));
String confirmationURL = "https://test-websites.net/#/email?type=confirm";
List<String> newurls = new ArrayList<String>();
int rowsize = id.size();
for(int i = 0; i < rowsize; i++) {
String text = id.get(i).getText();
System.out.println("Get the id:"+text);
newurls.add(confirmationURL + "&id=" + text);
}
after successfully execution of this code, you'd have a newurls list with URLs ending with id's from //table[contains(#class,'mat-table')]//tr/td[1])[last()] xpath.

Insert Multiple Rows realm android

Hello ive been trying to insert multiple rows to my realm database using values from arraylists , whenever i try to insert through a for loop it only adds the last one, if you need something else (code, xml) pls let me know
here is my code:
realm.executeTransactionAsync(new Realm.Transaction() { //ASYNCHRONOUS TRANSACCION TO EXECUTE THE QUERY ON A DIFFERENT THREAD
#Override
public void execute(Realm bgRealm) {
// increment index
Invoices inv = bgRealm.createObject(Invoices.class, RealmController.autoincrement(bgRealm, Invoices.class)); //METHOD THAT GIVES US THE AUTONINCREMENTE FUNCTION
//inv.id = nextId; //THE 2ND PARAMETER IN CREATE OBJECTE DEFINES THE PK
//...
//realm.insertOrUpdate(user); // using insert API
inv.number = n;
inv.serial = s;
inv.client = c;
inv.subtotal = sub;
inv.tax = tax;
inv.total = tot;
Invoice_lines invl = bgRealm.createObject(Invoice_lines.class, RealmController.autoincrement(bgRealm, Invoice_lines.class));//ID FROM ANOHTER TABLE (ROW)
for(int i=0; i<price.size(); i++) {
invl.description = description.get(i);
invl.price = price.get(i);
invl.quantity = quantity.get(i);
invl.invoice = inv;
bgRealm.insert(invl);
}
}
}
I'm not sure. You create only one realm object in this line:
Invoice_lines invl = bgRealm.createObject(Invoice_lines.class, RealmController.autoincrement(bgRealm, Invoice_lines.class));//ID FROM ANOHTER TABLE (ROW)
And in cycle you change invl fields, but don't insert new objects.
Try to create objects inside cycle.
Because what you wanted to do is
Invoice_lines invl = new Invoice_lines(); // unmanaged object
for(int i = 0; i < price.size(); i++) {
inv1.setId(RealmController.autoincrement(bgRealm, Invoice_lines.class));//ID FROM ANOHTER TABLE (ROW)
invl.description = description.get(i);
invl.price = price.get(i);
invl.quantity = quantity.get(i);
invl.invoice = inv;
bgRealm.insert(invl);
}

Use string values from an array as string variable to parse json - codenameone

I have a no. of checkboxes (20), what i did is if a user select any checkbox, its name is stored in an array (eg abc array below in code). The name of the string variable that stores the respective json is of the same name as of the checkbox. For eg if Checkbox "a" is clicked, string value "a" is stored in array and there is a string variable named "a" that stores the related json values. What I need is that if i pass the string value stored in array as InputStream is = new ByteArrayInputStream(abc.get(i).getBytes()), it should be used to parse the inputStream for json. But it gives NullPointerException since the string value "a" is not equal to string variable a. How can i solve this problem? I ran out of ideas here. Is there other ways to achieve what i want to do here?
code: String values of the selected checkboxes are stored in an array
String a = "[{\n"
+ "\t\t\t\"title\": \"title1\",\n"
+ "\t\t\t\"describ\": \"describ1\"\n"
+ "}]";
String b = "[{\n"
+ "\"title\": \"title2\",\n"
+ "\"describ\": \"describ2\"\n"
+ "}]";
String c = "[{\n"
+ "\t\t\t\"title\": \"title3\",\n"
+ "\t\t\t\"describ\": \"describ3\"\n"
+ "}]";
//and all jsons required are there
ArrayList<String> abc;
#Override
protected void beforeTestForApp(Form f) {
f.setTitle("abc");
abc = new ArrayList<>();
//I have stored "a" & "b" in the abc array here for simplicity, but it is dynamic,
//ie. if the user select checkbox c, c will be stored in abc array and so on
abc.add("a");
abc.add("b");
Button bb = new Button("go");
bb.addActionListener((e) -> {
showForm("TestForAppResult", null);
});
f.add(bb);
}
Form for json parser and displaying the values:
#Override
protected void beforeTestForAppResult(Form f) {
f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
InputStream is;
for (int i = 0; i < abc.size(); i++) {
Label heading = new Label(abc.get(i));
f.add(heading);
//this gives error since abc.get(i) gives string value, not string variable
is = new ByteArrayInputStream(abc.get(i).getBytes());
showDetails(is, f);
}
//if i do this instead of for loop jst above, i can get the result but since what value'll be stored in an array is not known,it is not possible
//is = new ByteArrayInputStream(a.getBytes());
//showDetails(is, f);
//is = new ByteArrayInputStream(b.getBytes());
//showDetails(is, f);
}
private void showDetails(InputStream is, Form f) {
JSONParser p = new JSONParser();
Hashtable<String, Object> test;
try {
test = p.parse(new InputStreamReader(is));
Vector aVector = (Vector) test.get("root");
for (int j = 0; j < aVector.size(); j++) {
Hashtable hm = (Hashtable) aVector.get(j);
String title = (String) hm.get("title");
String describ = (String) hm.get("describ");
Label z = new Label(title);
Label zz = new Label(describ);
f.add(z);
f.add(zz);
}
} catch (IOException ex) {
}
}
tbh i didnt get your problem concretely but i still try to give you some shots so you can try out.
If i understand correctly you have 20 objects which contains values underlying?
So then you have a JSONArray, just iterate trough it and grab that JSONObject.
now just use parseJSON instead of parse as it is deprecated...
here is a short snippet of my code
JSONArray jsonTasks = new JSONArray(responseString);
for (int index = 0; index < jsonTasks.length(); index++) {
JSONObject jsonObject = (JSONObject) jsonTasks.get(index);
if (jsonObject != null) {
Map jsonMap = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(jsonObject.toString().getBytes(UTF8)), UTF8));
System.out.println(jsonMap.get("date"));

How to convert rows of sqlite data to a ArrayList<double[]>()?

I get the data type of data from sqlite database is string. How to put these data into ArrayList()? Thank you!
try below code:-
ArrayList<String> array = new ArrayList<String>();
Levels l = new Levels(getBaseContext()); // level ur table name
l.open();
Cursor c_l = l.selectAll();
for (int i = 0; i < c_l.getCount()+1; i++)
{
array.add(c_l.getString(0)); // getstring 0 means your column
}
c_l.close();
l.close();

JList won't add data to it

If i want to add an ArrayList to a JList, it is not shown in the list, but the array contains the items. How could i fix this?
public void updateLeftList(){
// Enter the search text
interFace.Search.setText(this.search);
// Get the left list
JList leftList = interFace.LeftList;
leftList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// Define the arraylists
allPdf = new ArrayList();
curPdf = new ArrayList();
addPdf = new ArrayList();
// Get Files from manuals folder
File files = new File(configFile.getProperty("dir") + "/" + this.taal);
File[] listFiles = files.listFiles();
for(int i = 0; i < listFiles.length; i++){
if(listFiles[i].getName().endsWith(".pdf") && listFiles[i].isFile()){
allPdf.add(listFiles[i].getName().toString());
}
}
leftList.setListData(allPdf.toArray());
}
use ListModel to add the data:
1. create new instance of DefaultListModel
2. add your file names to the list model
3. set it as your JList list model
for(Object item : arrayList.toArray()){
((DefaultListModel) list1.getModel()).addElement(item);
}

Categories

Resources