Android - get content of certain array-field to a String - java

I am trying to get the content of a single array item.
In my code, I want the value of String suitId to have the content of suitIdArray[getSuitId];, but it doesn't get the content.
Could you please help me to see what is wrong. Here is my code...
Object item1 = spinner1.getSelectedItem();
int getDragId = spinner1.getSelectedItemPosition();
String suitId;
if(!item1.equals("Choose size")) {
suitId = suitIdArray[getSuitId];
}
else {
pSuit = null;
}

Is is not working because you have a variable getdragid but you're using getsuitid in the array? ie, should it be the following...
Object item1 = spinner1.getSelectedItem();
int getdragtid = spinner1.getSelectedItemPosition();
if(!item1.equals("Choose size"))
{
suitid = suitidarray[getdragtid]; // I changed this line
}
else if(item1.equals("Choose size"))
{
psuit = null;
}
Otherwise, I'm not really sure what you're trying to do?

Related

Get text from multiples radiobuttons

I want to get the text from multiples checked radiobuttons, and retrieve it in one edittext... but dont know how is the best way to do it. What is better? saving results in string or a list?
Anyway the result should appear in one single editext with the space beetwen each text. For example... Dog, Cat, Mouse.
if (radioButton.isChecked()){
String obs2 = radioButton.getText().toString();
texto.setText(obs2);
}
if (radioButton3.isChecked()){
String obs2 = radioButton3.getText().toString();
texto.setText(obs2);
}
if (radioButton4.isChecked()){
String obs2 = radioButton4.getText().toString();
texto.setText(obs2);
}
if (radioButton5.isChecked()){
String obs2 = radioButton5.getText().toString();
texto.setText(obs2);
}
if (radioButton6.isChecked()){
String obs2 = radioButton6.getText().toString();
texto.setText(obs2);
}
if (radioButton7.isChecked()){
String obs2 = radioButton7.getText().toString();
texto.setText(obs2);
}
if (radioButton8.isChecked()){
String obs2 = radioButton8.getText().toString();
texto.setText(obs2);
}
editText1 = here should get the text of each radio button checked
new try:
if (radioButton.isChecked()){
obs2 = checkNullAndAppend(obs2, (String) radioButton.getText());
} else if (radioButton3.isChecked()){
obs2 = checkNullAndAppend(obs2, (String) radioButton3.getText());
} else if (radioButton4.isChecked()){
obs2 = checkNullAndAppend(obs2, (String) radioButton4.getText());
} else {
texto.setText(obs2);
}
If you want event-driven code then using a list may be easier to manage depending on how many buttons you have and how you do it. But in the case of your exact example, a string is more than enough. As suggested in comments you can simply append the text texto.setText(texto.getText() + "," + obs2);.
If you want to avoid issues with null values or reduce duplicate code, then there are many different ways you can do this, but one is to create a simple method:
public static String checkNullAndAppend(String existing, String toAppend){
//Check null
if (existing == null || existing.equals(""))
return toAppend;
//Otherwise add a comma and space
else
return existing + ", " + toAppend;
}
Then you can simply call the method inside your if statements:
if (radioButton.isChecked()){
texto = checkNullAndAppend(texto, radioButton3.getText());
}
if (radioButton3.isChecked()){
texto = checkNullAndAppend(texto, radioButton3.getText());
}
...
editText1 = texto;
...

In the table i need to store the values of the Id which is selected but not able to do that Any Leads

Scenario:
I select Two Ids and gets added in the Table which can been seen in the below screen shot.
Over Here i need to Store the value in a Method and then call that method in other Place.
The Problem is iam not able to get the Values .
Code:
public boolean checkselectedDemandId()
{
String cDemand = "";
cDemand = "//table//tr//td[contains(#class,'mat-column-demandId')]";
List<WebElement> rows = driver.findElements(By.xpath(cDemand));`
for(WebElement row:rows)`
{if(cDemand.length()>0)
{
selectedDemandId = cDemand;
return true;
}`else
{
return false;
}
public String getselectedCreatedDemandId()
{
return selectedDemandId;
}
HTML of the Table:
4384SELTESTTECHMTESSINGAPOREHONGKONG2030-04-092040-06-152055-12-30delete 4383SELTESTTECHMTESSINGAPOREHONGKONG2030-04-092040-06-152055-12-30delete
If you are sure about number of rows, use the following code
List<WebElement> rows = driver.findElements(By.xpath("//table//tr//td[contains(#class,'mat-column-demandId')]"));
String rowOne = rows.get(0).getText();
String rowTwo = rows.get(1).getText();

(Swift) How to remove characters more than once using IndexOf

I'm making an iOS app that parses JSON data from a google spreadsheet. One of the issues with Google JSON data is that it includes unnecessary data that has to be removed. I'm new to iOS programming.
/*O_o*/google.visualization.Query.setResponse({"version":"0.6","reqId":"0","status":"ok","sig":"1400846503","table":{JSON DATA I NEED}});
I have done this in JAVA on Android using this code
int start = result.indexOf("{", result.indexOf("{") + 1);
int end = result.lastIndexOf("}");
String jsonResponse = result.substring(start, end);
My swift code
var something = "My google JSON Data"
let Start = String(something).characters.indexOf("{")!;
let substring1: String = something.substringFromIndex(Start);
something = substring1;
let End = String(something).characters.indexOf(")")!.distanceTo(something.endIndex);
let index3 = something.endIndex.advancedBy(-End);
let substring4: String = something.substringToIndex(index3)
What I'm asking is how do I get the index of the 2nd "{"
You should use NSJsonSerializer, but if you want to do it your way:
extension String {
func indexOf(target: String) -> Int {
if let range = self.rangeOfString(target) {
return self.startIndex.distanceTo(range.startIndex)
} else {
return -1
}
}
func indexOf(target: String, startIndex: Int) -> Int {
let startRange = self.startIndex.advancedBy(startIndex)
if let range = self.rangeOfString(target, options: .LiteralSearch, range: startRange..<self.endIndex) {
return self.startIndex.distanceTo(range.startIndex)
} else {
return -1
}
}
}
let end = myString.indexOf("{", startIndex: myString.indexOf("{") + 1)

Why clearing the arraylist is clearing the other arraylist even after the completely the assigning the value?

I have created a two list. First list is of some class type(Syncgroupset) and second list is arraylist of type string. This syncgroupset class contain getter and setter for the arraylist of type list.
I will be parsing the xml and copying the parsed values to the list type of class(syncgroupset).
Please find the xml as below.
<syncgroupsettings>
<syncgroupset id = "sgMS" labelid = "lblMS" enabled = "YES" default = "OFF">
<syncgroup syncgroupname = "VISITS"/>
<syncgroup syncgroupname = "CUSTOMERS"/>
</syncgroupset>
<syncgroupset id = "sgAS" labelid = "lblAS" enabled = "YES" default = "ON">
<syncgroup syncgroupname = "SALESDOCS"/>
<syncgroup syncgroupname = "ANALYTICS"/>
</syncgroupset>
</syncgroupsettings>
Please find the code as below
public List<Syncgroupset> parse(XmlPullParser parser) {
String id = "";
String synlabelid="";
String enabled = "";
String default1 = "";
String syngroupname = "";
List<Syncgroupset> mysynset = new ArrayList<Syncgroupset>();
try {
int eventType = parser.getEventType();
List<String> mysyname = new ArrayList<String>();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if(tagname.equalsIgnoreCase("labels"))
{
skip(parser);
}else
if (tagname.equalsIgnoreCase("syncgroupsettings")) {
// do nothing
}else
if (tagname.equalsIgnoreCase("syncgroupset")) {
syncgroupset = new Syncgroupset();
id = parser.getAttributeValue(ns,"id");
synlabelid = parser.getAttributeValue(ns,"labelid");
enabled = parser.getAttributeValue(ns,"enabled");
default1 = parser.getAttributeValue(ns,"default");
syncgroupset.setLabelid(synlabelid);
syncgroupset.setEnabled(enabled);
syncgroupset.setDefault(default1);
syncgroupset.setId(id);
mysynset.add(syncgroupset);
}else
if(tagname.equalsIgnoreCase("syncgroup"))
{
syngroupname = parser.getAttributeValue(ns,"syncgroupname");
mysyname.add(syngroupname);
}
break;
case XmlPullParser.TEXT:
break;
case XmlPullParser.END_TAG:
if (tagname.equals("syncgroupset")) {
mysynset.get(mysynset.size() - 1).setSyncgroup(mysyname);
mysyname.clear();
}
break;
default:
break;
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return mysynset;
}
When i encounter the syncgroup tag, i will be copying the values to other list of type string. I'm not adding syncgroup value to list of type class here only.
When i encounter the end tag of syncgroupsetting, then only i'm adding list of type string to the list of type class by replacing.
Then i'm clearing the list of type string. When i do like this, it is even clearing the list which is inside the list of class. Could you please let me know where i'm doing wrong.
It seems that Syncgroupset keeps a shallow copy of the list you pass to it with setSyncgroup(list). That's why list.clear() affects the Syncgroupset too.
Change this line:
mysynset.get(mysynset.size() - 1).setSyncgroup(mysyname);
To this:
mysynset.get(mysynset.size() - 1).setSyncgroup(new ArrayList<String>(mysyname));
That way, it will be not mysyname passed to it,
but a fresh new list with all elements of mysyname copied,
and then mysyname.clear() can in no way affect the list passed with that setSyncgroup call.

Null Pointer Exception from JCalander Combobox

My Java Application produces Null Pointer Exception from JCalander Combobox. I tried to catch the error. But that didnt work. Can someone assist me to fix this. Please.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.util.Calendar.setTime(Calendar.java:1106)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:955)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:948)
at java.text.DateFormat.format(DateFormat.java:336)
at org.freixas.jcalendar.JCalendarCombo.paramString(JCalendarCombo.java:780)
at java.awt.Component.toString(Component.java:8095)
tbmodel = (DefaultTableModel)tblItmQty.getModel();
System.out.println(calRecvDate.getDate());
try{
if(calRecvDate.getDate()==null){ // Error
JOptionPane.showMessageDialog(null, "Please Select Shippment Received Date");
calRecvDate.requestFocus();
}else if(txtShipSs.getText().isEmpty()){
////////////////////////////////////////////////////////////////
if (inputValidate() == true) {
try {
String shipId = txtShipId.getText();
String invID = txtInvoice.getText();
String shipSs = txtShipSs.getText();
String address = txtNtfAddress.getText();
String sipper = txtAShipper.getText();
String vessal = txtVessal.getText();
Date rcvDate = calRecvDate.getDate(); // Jcalander
String consignee = txtConsigne.getText();
ArrayList<ShippmentItems> shipItems = new ArrayList<ShippmentItems>();
tbmodel = (DefaultTableModel) tblItmQty.getModel();
for (int i = 0; i < tbmodel.getRowCount(); i++) {
String itmcode = (String) tbmodel.getValueAt(i, 0);
String itmName = (String) tbmodel.getValueAt(i, 1);
int qty = (int) tbmodel.getValueAt(i, 2);
ShippmentItems shpItems = new ShippmentItems(shipId, itmcode, itmName, qty);
shipItems.add(shpItems);
}
Since this throws the NPE:
calRecvDate.getDate()==null
The calRecvDate variable is null, and you will either need to check if it's null before using it, or make sure that it isn't null by tracing back in your code to where you think you've initialized it and fix the problem (since it isn't initialized).
To check if it's null, you could do:
if (calRecvDate != null) {
// use the calRecvDate variable here
} else {
// initialize the calRecvDate variable here
// or perhaps better, display a JOptionPane error message to the user
// that the date hasn't been selected, and exit this method by calling return:
return;
}
Again, don't use try/catch blocks to handle NullPointerExceptions.

Categories