Reputation: 7825
I just built a Broadcast Receiver with which I can get the incoming text messages, than I split the text message when there's a space and save it into a String[]
.
Now I need to check if in this String[]
is something from my database. For that I created a ArrayList<String>
, which gets all the entries from the corresponding column. Now I need to check if a String in my ArrayList
is the same in my String[]
from the text message, but I don't know how to realize that.
Is there an easy and fast way to check that, also I need to know which String is in both of them?
SmileySmsReceiver:
package de.retowaelchli.filterit.services;
import java.util.ArrayList;
import de.retowaelchli.filterit.database.SFilterDBAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class SmileySmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
//Datenbank definieren
SFilterDBAdapter mDbHelper = new SFilterDBAdapter(context);
//---get the SMS message passed in---
Log.d("SmileySmsReceiver", "Yes it calls the onReceive");
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
Log.d("SmileySmsReceiver", "Bundle is not null");
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
Log.d("SmileySmsReceiver","Was steht in der Nachricht?: " + str);
String[] splited = str.split("\\s+");
//Hier werden die Strings der Smileys aus der Datenbank gezogen
mDbHelper.open();
Cursor c = mDbHelper.getAllSFilter();
ArrayList<String> SmileyList = new ArrayList<String>();
c.getColumnIndex(SFilterDBAdapter.KEYWORD);
int ColumnIndex = c.getColumnIndex(SFilterDBAdapter.KEYWORD);
if(c!=null)
{
//Hier werden die Smileys in die ArrayList geschrieben
while(c.moveToNext()){
String infoItem = c.getString( ColumnIndex );
SmileyList.add(infoItem);
}
<------------------------- FROM HERE ON I NEED YOUR GUYS HELP ------------------------------->
}
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
Upvotes: 10
Views: 22773
Reputation: 61578
You can iterate over the array (e.g. with a for
loop over splitted.length
), querying your list with myList.contains(splitted[i])
. The query will return a boolean
.
If you need to check if all items in the array are present in the List, there is a comfortable alternative with myList.containsAll(Arrays.asList(splitted))
There is another concise alternative without looping to check if any item of the array is present in the list :
List splitList = Arrays.asList(splitted);
splitList.retainAll(myList);
splitList.isEmpty()
will then return false
, if any item of the lists is matching.
The contents of the splitList
will show all strings that have matched.
Upvotes: 2
Reputation: 6067
Depending on how large your array and ArrayList are and how many times you will access them, you might want to use a HashSet instead of an ArrayList so that you can get a fast look up with the hashed contains()
method instead of having to iterate through the ArrayList. Something like this:
boolean containsString(ArrayList<String> stringArrayList, String[] stringArray) {
Set<String> stringSet = new HashSet<String>(stringArrayList); // if same input is used reused, might want this outside the method
for (String s : stringArray) {
if (stringSet.contains(s)) {
return true;
}
}
return false;
}
Upvotes: 1
Reputation: 2611
String array[] = ... ;
List<String> list = ... ;
// check if anything in array is in list
for(String str : array)
if(list.contains(str)) doSomething() ;
If you want do something for every match, the code above will work. If you only want to do something once if there was a match (and not for every match), you will need to add a break
statement in the if sentence.
Upvotes: 3
Reputation: 16570
If you have a String myString
and an ArrayList<String> myListOfStrings
, you can check if myString
is in the list like this:
myListOfStrings.contains( myString );
This will return a boolean (true/false) - true if the strings was found, false if not.
In your case you would want to run through the array of strings and match each of them to the SmileyList
like this:
for( String s : splitted ) {
if( SmileyList.contains( s ) ) {
//Do what you need to do if the string is in the SmileyList.
}
}
Upvotes: 15