Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if String in String[] is in ArrayList<string>

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();
            }                         
        }
    }
like image 251
safari Avatar asked Oct 26 '11 08:10

safari


1 Answers

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.
   }
}
like image 57
kaspermoerch Avatar answered Sep 28 '22 20:09

kaspermoerch