My Array will not add anything to the list after the first and keeps returning null when i search for anything after.
Is there something wrong with this method?
public void addItem(Item newItem) throws DuplicateItemException {
Item tempItem;
if(itemList == null)
itemList.add(newItem);
try {
tempItem = findItem(newItem.ID);
if(tempItem == null) {
itemList.add(newItem);
}
else {
throw new DuplicateItemException(newItem.ID + "already exists");
}
}
catch (ItemNotFoundException e) {
itemList.add(newItem);
}
}
In your code:
if(itemList == null)
itemList.add(newItem);
if itemList is indeed null, how can you add to it?
Use this instead
if(itemList == null) {
itemList = new ArrayList<>();
itemList.add(newItem);
}
Related
I'm trying to delete items in my ListView when delete Button is clicked, but it is not working and I can't figure it out.
Here is an Example of my Code for deleting an entry based on the instance "Student":
public void deleteButton(ActionEvent actionEvent) {
if (notesListView.getSelectionModel().isEmpty()) {
InfoModal.show("Bitte wählen Sie die zu löschende Notiz aus.");
return;
}
boolean delete = ConfirmationModal.show("Soll die Notiz gelöscht werden?");
if (delete) {
Dao<Notepad, Integer> notepadDao = db.getNotepadDao();
try {
if (this.objectType instanceof Student) {
Dao<StudentNotepad, Integer> studentNotepadDao = db.getStudentNotepadDao();
for (StudentNotepad n : studentNotepadDao) {
if (notesListView.getSelectionModel().getSelectedItem().equals(n.getNotepad())) {
SceneManager.getInstance().getLoaderForScene(SceneType.NOTESTAB_WINDOW).
<NotesTabController>getController().notesListView.getItems().remove(n.getNotepad());
studentNotepadDao.delete(n);
notepadDao.delete(n.getNotepad());
}
}
}
}
}
}
This is how my ListView/ListCell is initiated:
public void setObject(Object object) {
//Getting Object Type (Group, Groupage or Student)
this.objectType = object;
try {
if (this.objectType instanceof Student) {
ObservableList<Notepad> list = FXCollections.observableArrayList();
for (StudentNotepad s : db.getStudentNotepadDao()) {
if ((db.getLoggedInUser() == s.getNotepad().getUser()) && ((((Student) this.objectType).getId() == s.getStudent().getId()))) {
list.add(s.getNotepad());
}
}
notesListView.setItems(list);
notesListView.getItems().clear(); //not needed if list is definitly empty
db.getStudentNotepadDao().queryForAll().stream()
.map(StudentNotepad::getNotepad)
.filter(n -> n.getUser().equals(db.getLoggedInUser()))
.forEach(notesListView.getItems()::add);
notesListView.setCellFactory(new Callback<ListView<Notepad>, ListCell<Notepad>>() {
public ListCell<Notepad> call(ListView<Notepad> param) {
return new ListCell<Notepad>() {
#Override
protected void updateItem(Notepad item, boolean empty) {
super.updateItem(item, empty);
String style = "";
if (!empty && item != null) {
setText(item.getNotepadName());
switch (item.getNotepadPriority()) {
case "Hoch":
style = "-fx-background-color: red";
break;
case "Mittel":
style = "-fx-background-color: yellow";
break;
case "Niedrig":
style = "-fx-background-color: green";
break;
case "Neutral":
style = "-fx-background-color: grey";
break;
}
}
else {
setText("");
}
setStyle(style);
}
};
}
});
}
} catch(SQLException e) {
e.printStackTrace();
}
}
Additionally, my List is not refreshing after changing the Object within the same ObjectType. Example: Changing from Student1 to Student2. Changing between ObjectTypes themselves like Student1 to Group1 works. I think that the problem lays in the filter function of my ListCell.
Does someone have an idea? I'm struggeling with this problem for hours and can't figure it out!
I'm trying to read the text from all the child nodes of an AccessiblityNodeInfo.
I tried getting AccessiblityNodeInfo.getParent() and AccessibilityNodeInfo.getChildCount()
for(int i=0; i<accessibilityNodeInfo.getChildCount();i++){
accessibilityNodeInfo.getChild(i).getText();
}
Accessed the child nodes as mentioned above to get the text from the child nodes. but it is not providing the all the text on the screen.
Can anyone suggest me what to do get the text?
private List<CharSequence> getAllChildNodeText(AccessibilityNodeInfoCompat infoCompat) {
List<CharSequence> contents = new ArrayList<>();
if (infoCompat == null)
return contents;
if (infoCompat.getContentDescription() != null) {
contents.add(infoCompat.getContentDescription().toString().isEmpty() ? "unlabelled" : infoCompat.getContentDescription());
} else if (infoCompat.getText() != null) {
contents.add(infoCompat.getText().toString().isEmpty() ? "unlabelled" : infoCompat.getText());
} else {
getTextInChildren(infoCompat, contents);
}
if (infoCompat.isClickable()) {
if (infoCompat.getClassName().toString().contains(Button.class.getSimpleName())) {
if (contents.size() == 0) {
contents.add("Unlabelled button");
} else {
contents.add("button");
}
}
contents.add("Double tap to activate");
}
return contents;
}
private void getTextInChildren(AccessibilityNodeInfoCompat nodeInfoCompat, List<CharSequence> contents) {
if (nodeInfoCompat == null)
return;
if (!nodeInfoCompat.isScrollable()) {
if (nodeInfoCompat.getContentDescription() != null) {
contents.add(nodeInfoCompat.getContentDescription());
} else if (nodeInfoCompat.getText() != null) {
contents.add(nodeInfoCompat.getText());
}
if (nodeInfoCompat.getChildCount() > 0) {
for (int i = 0; i < nodeInfoCompat.getChildCount(); i++) {
if (nodeInfoCompat.getChild(i) != null) {
getTextInChildren(nodeInfoCompat.getChild(i), contents);
}
}
}
}
}
I have a list of items of object. I am creating dynamic buttons as per the size of a list using for loop.
I am showing some data when its the first item of the loop. Now /i want to switch the data when I will click the buttons.
Buttons will have the numbers of the item, if three items are present in the list , three buttons are there named one, two, three.
So if I click on first I want to show data of first item, if second clicked data of second item respectively.
I tried to do it, but I am not able to get the result.
#Override
public void doPostExecute(List<Payment> list1)
{
list.addAll(list1);
for(final Payment payment : list) {
i++;
Button myButton = new Button(getActivity());
LinearLayoutCompat.LayoutParams lp = new LinearLayoutCompat.LayoutParams(60,30);
linear_buttons.addView(myButton, lp);
myButton.setText(String.valueOf(i));
myButton.setBackgroundResource(R.drawable.edit_button);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (payment.getPaidAmount() != null && !payment.getPaidAmount().equals("null"))
txt_paid.setText("$" + payment.getPaidAmount());
if (payment.getProductStatus() != null && !payment.getProductStatus().equals("null")) {
if (payment.getProductStatus().equals("1")) {
txt_status.setText(getString(R.string.paid));
button_complain.setVisibility(View.VISIBLE);
button_more.setVisibility(View.GONE);
} else if (payment.getProductStatus().equals("0")) {
txt_status.setText(getString(R.string.unPaid));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.GONE);
} else if (payment.getProductStatus().equals("5")) {
txt_status.setText(getString(R.string.processing));
button_complain.setVisibility(View.VISIBLE);
button_more.setVisibility(View.GONE);
} else if (payment.getProductStatus().equals("6")) {
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
txt_status.setText(getString(R.string.releasedStatus));
} else if (payment.getProductStatus().equals("8")) {
txt_status.setText(getString(R.string.disputeStatus));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
} else if (payment.getProductStatus().equals("9")) {
txt_status.setText(getString(R.string.disputeResolvedStatus));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
} else if (payment.getProductStatus().equals("2")) {
txt_status.setText(getString(R.string.completedStatus));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
}
if (payment.getTpAmount() != null && !payment.getTpAmount().equals("null")) {
String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getTpAmount()));
txt_tp_amount.setText("$" + amount);
}
if (payment.getCommission() != null && !payment.getCommission().equals("null")) {
String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getCommission()));
txt_comission.setText("$" + amount);
}
if (payment.getTax() != null && !payment.getTax().equals("null")) {
String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getTax()));
txt_tax.setText("$" + amount);
}
if (payment.getPayeEmail() != null && !payment.getPayeEmail().equals("null")) {
txt_payee_email.setText(payment.getPayeEmail());
}
if (payment.getPayerEmail() != null && !payment.getPayerEmail().equals("null")) {
txt_payer_email.setText(payment.getPayerEmail());
}
if (payment.getPayerName() != null && !payment.getPayerName().equals("null")) {
txt_payer_name.setText(payment.getPayerName());
}
if (payment.getVendorId() != null && !payment.getVendorId().equals("null")) {
txt_vendor_id.setText(payment.getVendorId());
}
}
}
});
if(i==1) {
if (payment.getPaidAmount() != null && !payment.getPaidAmount().equals("null"))
txt_paid.setText("$" + payment.getPaidAmount());
if (payment.getProductStatus() != null && !payment.getProductStatus().equals("null")) {
if (payment.getProductStatus().equals("1")) {
txt_status.setText(getString(R.string.paid));
button_complain.setVisibility(View.VISIBLE);
button_more.setVisibility(View.GONE);
} else if (payment.getProductStatus().equals("0")) {
txt_status.setText(getString(R.string.unPaid));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.GONE);
} else if (payment.getProductStatus().equals("5")) {
txt_status.setText(getString(R.string.processing));
button_complain.setVisibility(View.VISIBLE);
button_more.setVisibility(View.GONE);
} else if (payment.getProductStatus().equals("6")) {
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
txt_status.setText(getString(R.string.releasedStatus));
} else if (payment.getProductStatus().equals("8")) {
txt_status.setText(getString(R.string.disputeStatus));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
} else if (payment.getProductStatus().equals("9")) {
txt_status.setText(getString(R.string.disputeResolvedStatus));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
} else if (payment.getProductStatus().equals("2")) {
txt_status.setText(getString(R.string.completedStatus));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
}
if (payment.getTpAmount() != null && !payment.getTpAmount().equals("null")) {
String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getTpAmount()));
txt_tp_amount.setText("$" + amount);
}
if (payment.getCommission() != null && !payment.getCommission().equals("null")) {
String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getCommission()));
txt_comission.setText("$" + amount);
}
if (payment.getTax() != null && !payment.getTax().equals("null")) {
String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getTax()));
txt_tax.setText("$" + amount);
}
if (payment.getPayeEmail() != null && !payment.getPayeEmail().equals("null")) {
txt_payee_email.setText(payment.getPayeEmail());
}
if (payment.getPayerEmail() != null && !payment.getPayerEmail().equals("null")) {
txt_payer_email.setText(payment.getPayerEmail());
}
if (payment.getPayerName() != null && !payment.getPayerName().equals("null")) {
txt_payer_name.setText(payment.getPayerName());
}
if (payment.getVendorId() != null && !payment.getVendorId().equals("null")) {
txt_vendor_id.setText(payment.getVendorId());
}
}
}
}
}
How can I achieve this? please help.. Thank you.
You can set an onClickLister to the button in that row which will have a reference to that row's data. Use an intent to start the detailed activity and add the clicked item's data as an extra.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//Inflate your row
button.setOnClickListener(new OnClickListener(){
#Override
//On click function
public void onClick(View view) {
HashMap<String, String> item = contactsList.get(position);
//Create the intent to start another activity
Intent intent = new Intent(view.getContext(), DetailActivity.class);
intent.putExtra("data", item);
view.getContext().startActivity(intent);
}
});
});
in the receiving activity, you can get the data from the intent
HashMap<String, String> data = getIntent().getExtras().getParcelableExtra("data");
You are using the variable i as a global variable and using i++ inside the loop. So inside the onClick method the if(i == 1) is either always false, or always true.
Make variable local and use myButton.setId(i); inside your for loop where you create buttons.
Now inside OnClickListener you can use int id = v.getId(); and use if(id == 1)
I was trying to delete objects from Parse Class where requesterUsername is equals to current Username. I have more than 7 rows of data in Parse database but when I execute the method below objects.size() returns 0 which was not my expectation and it does not delete any rows from the database.
I am clueless here. Any help will be appreciated. Thanks in advance.
public void requestGride(View view){
if(requestActive == false) {
Log.i("MyApp", "Gride requested");
ParseObject request = new ParseObject("Requests");
request.put("requesterUsername", ParseUser.getCurrentUser().getUsername());
ParseACL parseACL = new ParseACL();
parseACL.setPublicWriteAccess(true);
request.setACL(parseACL);
request.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
infoTextView.setText("Finding Gride..");
grideX.setText("Cancel GrideX");
requestActive = true;
}
}
});
}else{
infoTextView.setText("GrideX Cancelled");
grideX.setText("GrideX");
requestActive = false;
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Requests");
query.whereEqualTo("requesterUsername",ParseUser.getCurrentUser().getUsername());
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if(e == null){
if(objects.size() > 0){
for (ParseObject adds : objects){
adds.deleteInBackground();
}
}
}
}
});
}
}
I am working on an application in which have two navigation button namely 'previous and next' which on tap load stories respectively. When i tap these buttons gently they work fine button when i tap next button to the last index continuously without any break then the previous button does not work or similarly when i start tapping previous button to the very first index i am unable to move forward.
Remember this only happens at the extreme cases, onClick is not called, don't know why.
My code is as follows, please help me out. Thanks in advance.
this the code of onClick, which works fine in all cases except the extreme cases, when buttons are not tapped gently.
public void onClick(View v) {
if (visible == true) {
Log.e("visible", "true");
return;
}
visible = true;
try {
Log.e("Now", "On Click");
pDialog = new ProgressDialog(StoriesListController.this);
pDialog.setIndeterminate(true);
pDialog.setIcon(R.drawable.icon_small);
pDialog.setCancelable(true);
if (isFavoriteList == true) {
pDialog.setMessage("Loading favorites...");
} else {
pDialog.setMessage("Loading data...");
}
pDialog.setTitle("Please wait");
pDialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface arg0) {
if (cancelableHeavyWorker != null) {
cancelableHeavyWorker.setHandler(null);
}
finish();
}
});
pDialog.show();
if (v == next) {
if (ind < items.size() - 1) {
ind++;
loadAndShowNextActivity();
}
} else if (v == previous) {
if (ind > 0) {
ind--;
loadAndShowNextActivity();
}
}
// realeaseMemoryIfneededofStory(ind);
} catch (Exception e) {
Log.e("Error", "--.OnClink Message" + e.toString());
e.printStackTrace();
}
}
Moreover, the boolean variable "visible" is being set to false in the callback function.
The Code of Call Back Function is as follows:
private void fetchTopStoryDetailCallback(Object resultVector) {
System.out.println("fetchTopStoryDetailCallback");
try {
Vector<?> v = (Vector<?>) resultVector;
boolean completedOrFailed = ((Boolean) v.elementAt(0)).booleanValue();
if (completedOrFailed == true) {
boolean slideshow = ((Boolean) v.elementAt(1)).booleanValue();
Object result = v.elementAt(2);
String res[] = (String[]) result;
if (slideshow) {
if (Utils.topStorySlidesArrayList != null && Utils.topStorySlidesArrayList.size() > 0) {
Intent articleActivityIntent = new Intent(this, SlideShowActivity.class);
articleActivityIntent.putExtra("storyData", (String[]) result);
articleActivityIntent.putExtra("back", back);
articleActivityIntent.putStringArrayListExtra("sid", slideids);
// articleActivityIntent.putExtra("contentType", )
articleActivityIntent.putExtra("addkey", ADDS_KEY);
showActivityInContoller(articleActivityIntent);
hidePrgressgingDailog();
} else {
hidePrgressgingDailog();
this.closeActivity = true;
showMessage("Error", "This story encounter an error while opening. Check your Internet Connection and try later");
}
} else {
if (res[3] != null && (!(res[3].equalsIgnoreCase("null")) && (!res[3].equals("")))) {
Intent slideshowActivtyIntent = new Intent(this, ArticleActivity.class);
slideshowActivtyIntent.putExtra("storyData", (String[]) result);
slideshowActivtyIntent.putExtra("back", back);
slideshowActivtyIntent.putExtra("addkey", ADDS_KEY);
showActivityInContoller(slideshowActivtyIntent);
hidePrgressgingDailog();
} else {
hidePrgressgingDailog();
this.closeActivity = true;
showMessage("Error", "This story encounter an error while opening. Check your Internet Connection and try later");
}
}
} else {
showMessage("Error", "This story encounter an error while opening. Check your Internet Connection and try later");
hidePrgressgingDailog();
}
} catch (Exception e) {
Log.e("StoriesController", "Message = " + e.toString());
e.printStackTrace();
}
finally {visible = false; }
}
this is how visiblity of the buttons is set...!!!!
private void adjustButtonsVisibility() {
try {
if (items.size() == 1) {
next.setEnabled(false);
next.setImageResource(R.drawable.navigator_next_disable);
previous.setEnabled(false);
previous.setImageResource(R.drawable.navigator_previous_disable);
//hidePrgressgingDailog();
return;
}
if (ind == 0) {
previous.setEnabled(false);
next.setEnabled(true);
previous.setImageResource(R.drawable.navigator_previous_disable);
next.setImageResource(R.drawable.story_next_arrow);
hidePrgressgingDailog();
} else if (ind > 0 && ind < items.size() - 1) {
previous.setEnabled(true);
next.setEnabled(true);
previous.setImageResource(R.drawable.story_previous_arrow);
next.setImageResource(R.drawable.story_next_arrow);
}
if (ind == items.size() - 1) {
previous.setEnabled(true);
next.setEnabled(false);
previous.setImageResource(R.drawable.story_previous_arrow);
next.setImageResource(R.drawable.navigator_next_disable);
hidePrgressgingDailog();
}
} catch (Exception e) {
Log.e("Error", "--,adjustButtonsVisibility Message = " + e);
e.printStackTrace();
}
}
Do this
if (ind == 0) {
previous.setEnabled(false);
previous.setImageResource(R.drawable.navigator_previous_disable);
next.setImageResource(R.drawable.story_next_arrow);
hidePrgressgingDailog();
}
else if (ind > 0 && ind < items.size() - 1) {
previous.setEnabled(true);
next.setEnabled(true);
previous.setImageResource(R.drawable.story_previous_arrow);
next.setImageResource(R.drawable.story_next_arrow);
}
else if (ind == items.size() - 1) {
previous.setEnabled(true);
next.setEnabled(false);
previous.setImageResource(R.drawable.story_previous_arrow);
next.setImageResource(R.drawable.navigator_next_disable);
hidePrgressgingDailog();
}