Reputation: 83
I have problem with some basic Android knowledge. My problem is I don't know how to fill in a value from ArrayList
. Currently I have 2 applications, one application sending an information through intent, and another application receiving the information.
In the first application, it will receive some information for specific printer's error. All the information I have to show in other application as a notification. The notification (the second application) I have implemented it, it is can show the notification. The notification is carried by a String
type and with intent it can sent to second apps.
I have one method called updateUI()
. Because I have to keep updated the Interface and the information for my first application, I'm using a service. So, updateUI()
in every certain seconds will be called.
I have ArrayList, called error
. This list will collect all the information:
ArrayList<String> error;
I have declared the notification in first application:
String notification;
Right now I want to fill this String from ArrayList. Before doing that I have declared temporary String called temp. This string will avoid the same notification that will be sent to notification app when updateUI()
has been called again.
for (int i = 0; i < error.size(); i++) {
if (error.get(i) != null) {
notification= error.get(i);
if (notification.equals(temp)) {
notification = null;
}
else
{
temp = notification;
}
}
}
This is how I sent the notification:
Intent broadCastIntent = new Intent("SendMessage");
broadCastIntent.putExtra("ERROR", notification);
sendBroadcast(broadCastIntent);
If the arrayList contains one element, it works fine. But if there are 2 events happen, so the error array have 2 elements, It will only show the second element.I want to sent the first element as a notification followed by second elements.
Example:
in error
has 2 elements
error = {"Ink Black is empty","Ink Magenta is Empty",null,null};
when we go to for
statement
First index:
for (int i = 0; i < error.size(); i++) {
if (error.get(0) != null) { ---------> Error.get[0] = "Ink Black..."
notification= error.get(i); ---> notification = "Ink Black..."
if (notification.equals(temp)) {
notification = null;
}
else
{
temp = notification;---> temp= "Ink Black..."
}
}
}
Second index:
for (int i = 0; i < error.size(); i++) {
if (error.get(1) != null) { --------> Error.get[1] = "Ink Magenta..."
notification= error.get(1);----> notification = "Ink Magenta..."
if (notification.equals(temp)) {---> "(Ink Magenta...".equals("Ink Black...")
notification = null;
}
else
{
temp = notification; ----->temp= "Ink Magenta..."
}
}
}
After went to for
statement the notification only contained "Ink Magenta..". Because of that the notification apps only show :Ink Magenta..." Anybody knows how to distinguish elements of Arraylist
so I can sent it one by one?
I have tried without for statement, but I have got null pointer if there is only one element inside the arraylist
.
if (!error.get(0).isEmpty())
{
if (error.get(0).equals(temp))
{
if (!error.get(1).isEmpty() && !error.get(1).equals(tempA))
{
notification = error.get(1);
tempA = error.get(1);
}
else
{
notification= null;
}
}
else
{
notification= error.get(0);
temp = error.get(0);
}
}
else
{
notification= null;
}
Anybody knows how to solve my problem? So that I can use other elements
Upvotes: 0
Views: 283
Reputation: 2672
If you want to ensure uniqueness for the items that you add to your arraylist, don't use arraylist. Use Set (for example HashSet) because Set is A collection that contains no duplicate elements.
After you have done getting notifications, that is adding items to your set object, call sendBroadcast(broadCastIntent)
for each item
Upvotes: 0
Reputation: 1364
if (!error.get(1).isEmpty() && !error.get(1).equals(tempA))
you need to check whether either size of your arraylist is greater than 1, or check if error.get(i) != null
Upvotes: 0
Reputation: 24853
You have not initialized your ArrayList. So, without initializing if you are trying to work around with an object it will end you up in Null pointer exception.
Try to initialize your list..
error = new ArrayList<String>();
And, then if you want to add an element to ArrayList you have to add like:
error.add("Ink Black is empty");
error.add("Ink Magenta is Empty");
Upvotes: 1