CheckBox isSelected() method always returns false - java

I am working on an Android application where I have to check the CheckBox selection onClick() so I made this
if (checkBoxRM.isSelected() == true) {
isSelectedValue = "True";
//debug
Toast.makeText(MainActivity.this, "Kiejölés értéke: " + isSelectedValue, Toast.LENGTH_SHORT).show();
} else {
//debug
isSelectedValue = "False";
Toast.makeText(MainActivity.this, "Kiejölés értéke: " + isSelectedValue, Toast.LENGTH_SHORT).show();
}
But seems like isSelectedValue is always false.

Instead of isSelected() perform isChecked().

You have to cast the checkBox to enable isChecked()
This works:
boolean selected = ((CheckBox)view.findViewById(R.id.main_cbSelectAll)).isChecked();
This not (here, you can only choose "isSelected"):
boolean selected = view.findViewById(R.id.main_cbSelectAll).isChecked();
If you want to use it as an SQLite-statement, you have to transform to an integer:
int selected = ((CheckBox)view.findViewById(R.id.main_cbSelectAll)).isChecked() ? 1 : 0;

Related

can't delete from getContentResolver().delete

I can't delete a conversation from getContentResolver, I don't know in which part am doing mistakes, as I also searched about these but can't help myself and I also tried different sols which were given on stackoverflow but same result & thanks a lot in advance.
Here is the code:
public static boolean deleteSmsofContact(Context context, String number,
boolean deleteLocked)
{
int result;
if (deleteLocked) {
//changes values
String[] selectionArgs=new String[]{number};
String selection= ""+"address=?";
//
result = context.getContentResolver().delete(Uri.parse("content://sms/"),selection,selectionArgs);
// Log.d("UF","WOW "+result+" " +number);
} else {
result = context.getContentResolver().delete(Constants.URI_SMS,
"address=? AND locked=?", new String[] { number, "1" });
}
if (result > 0) {
return true;
}
return false;
}
Here is the method from which I am calling:
boolean result = Utils.deleteSmsofContact(InboxActivity.this, sms.getNumber(), true);
if (result) {
dataList.remove(threadPosition);
iAdapter.notifyDataSetChanged();
Toast.makeText(InboxActivity.this,"Removed",Toast.LENGTH_LONG).show();
}else
{
Toast.makeText(InboxActivity.this,"cant removed",Toast.LENGTH_LONG).show();
}
Well I posted it but did not get the answer so finally I searched a lot on this and the correct answer is until or unless your app is not set a default you can't delete any sms or whole conversation.
Follow this link it will make your app set a default OR you will be able to delete.

How to get value of selected radioButton of buttonGroup

How to get value of selected radioButton?
I tried using buttonGroup1.getSelection().getActionCommand() (as posted in some of answers here) but it is not working.
Also, i am temporarily using this code but i want to know is this a good practice or not?
//Consider that maleRButton and femaleRButton are two radioButtons of
//same buttonGroup
String getGender()
{
if(maleRButton.isSelected())
{
return "Male";
}
else if(femaleRButton.isSelected())
{
return "Female";
}
else
{
return null;
}
}
I tried using buttonGroup1.getSelection().getActionCommand()
That approach will work, but for some reason it looks like you manually need to set the action command when you create the button. For example:
JRadioButton maleButton = new JRadioButton( "Male" );
maleButton.setActionCommand( maleButton.getText() );
This acutally seems like a bit of a bug to me since usually the action command defaults to the text if the action command is not set.
If you have several buttons you probably should do it this way :
String getSelectedButton()
{
for (Enumeration<AbstractButton> buttons = buttonGroup1.getElements(); buttons.hasMoreElements();) {
AbstractButton button = buttons.nextElement();
if (button.isSelected()) {
return button.getText();
}
}
return null;
}
String gender=group.getSelection().getActionCommand();
It will work but it show null value.
int selectedRadioButtonID = radio_group.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if (selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
answerList.add(selectedRadioButtonText);
} else {
Toast.makeText(this, "select radio button", Toast.LENGTH_SHORT).show();
return false;
}
For Deatils, check this

how to assert for boolean value in selenium webdriver

I have a flag of type boolean "worklogMatch". I need to assert this flag for 'true'. If this is not true, I would need to print the ERROR value. Here is my code,
List<WebElement> worklogObj = driver().findElements(By.xpath("//div[#class='ng-scope']");
boolean worklogMatch = false;
ArrayList<String> worklogDescriptions = new ArrayList<String>();
for(int i=0; i<worklogObj.size(); i++) {
worklogDescriptions.add(worklogObj.get(i).getText());
log.info("Text of the worklogs: "+worklogObj.get(i).getText());
if (worklogObj.get(i).getText().equals(worklogDescription)){
worklogMatch = true;
break;
}
}
assertTrue(worklogMatch == true, "worklog Description "+worklogDescription + " is not saved. List of "+ "worklog description found for the incident " +incidentNumber +" is" + worklogDescriptions);
When I assert this, the boolean variable worklogMatch is set as false as the String that I'm looking for is not present in the ArrayList. The test case fails as expected. However, the message is not dispalyed in the console. My questions,
Is my way of assertion valid? I'm asserting a boolean variable with 'true'
Why is my message not being printed when the assertion fails?
Yes, way of assertion is correct, one thing that i've changed is not compare worklogMatch to true in assertion
According to the doc, message should be written as first arg
now assert will look like:
assertTrue("worklog Description "+worklogDescription + " is not saved. List of "+ "worklog description found for the incident " +incidentNumber +" is" + worklogDescriptions, worklogMatch);

Data validation within data validation

I first want to validate that the user entered a value and to make sure to exit if 'cancel' was pushed. Then, I want to validate that the String releaseDateString is in the correct format at the same time as converting the String to java.sql.Date.
The first validation is taking place but then the JOptionPane carries on repeating itself and does not even consider the try and catch following it.
Here is my method
boolean retry = false;
java.sql.Date releaseDate = null;
String releaseDateString = "";
String title = "";
while (!retry) {
while(!retry){//field is validated to make sure a value was entered and to exit if cancel was pushed
releaseDateString = JOptionPane.showInputDialog("Please input the release date of the movie (yyyy-mm-dd)");
qtd.stringValidation(releaseDateString);
}
try { //the date is validated to make sure it is in the correct format
releaseDate = java.sql.Date.valueOf(releaseDateString);
} catch (Exception e) {
retry = false;
JOptionPane.showMessageDialog(null, "Make sure you enter a date in the format of 'dd-mm-yyy'");
}
}
It links to this method
public static boolean stringValidation(String attribute){
boolean retry = false;
if (attribute == null){
System.exit(0);
}
else if (attribute.equals("")) //if the cancel button is selected or no value was entered into the
{
JOptionPane.showMessageDialog(null, "Make sure you enter a character into the textbox");
}
else {
retry = true;
}
return retry;
}
When you do this,
qtd.stringValidation(releaseDateString);
You aren't assigning the result to retry. I believe you wanted,
retry = qtd.stringValidation(releaseDateString);

How do I determine who fired onContextItemSelected?

I am using an ExpandableListView, same way they do in this sample code:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html
The ExpandableListView gets populated with categories and their subcategories (once I click on a category). Example:
-Dairy (category)
-Milk (sub category)
-cheese (sub category)
When I long-click on milk or cheese, a menu pops up using this function:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
ExpandableListContextMenuInfo info =(ExpandableListContextMenuInfo) menuInfo;
String selectedWord = ((TextView) info.targetView).getText().toString();
menu.setHeaderTitle(selectedWord.split(",")[1]); //set header
String itemId = selectedWord.split(",")[0];
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("action", "getProducts"));
nameValuePairs.add(new BasicNameValuePair("subcat_id", itemId));
String response = helper.makeHttpRequest(nameValuePairs);
String[] items = response.split(";");
for (int i=0; i<items.length; i++){
menu.add(0, 0, 0, items[i]);
}
}
Then, when I click on one of the items in the menu that pops up, I want to know which item in the list was selected (If I click on 'milk'. for example, the menu has "1% milk", "2% milk" etc._
This function gets fired:
#Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
String title = ((TextView) info.targetView).getText().toString();
String selected="";
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
Toast.makeText(this, title + " selected: " + selected+ " " + childPos + " clicked in group " + groupPos , Toast.LENGTH_SHORT).show();
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title + " selected: " + selected + " " + childPos + " clicked in group " + groupPos , Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
But I don't find a way to determine which item exactly in the list was clicked.
Any ideas?
Thank you in advance! Please let me know if I need to be more clear.
Each item that you add to the ContextMenu should have its own unique identifier. This is useful for when you need to figure out which menu item was selected. So adding menu items needs to look like this: menu.add(0, unique_id, 0, items_name);. Then when you want to determine which item was selected you do something like this in onContextItemSelected using the item id:
switch (item.getItemId()){
case UNIQUE_ID_1:
//handle what to do
break;
case UNIQUE_ID_2:
//handle what to do
break;
....
So you may need to change your implementation slightly. I would create a unique id as an int constant for each possible case and add each one specifically to the menu. It may a little more code but it will be so much easier to handle.
Also just a suggestion since you are new here: when someone provides a good answer, you should accept it by clicking the check mark next to the answer. Good luck!

Categories

Resources