0

I want to return all values of dbManagement.ORIGINATING_ADDRESS from this function but it just gives me last one value to blockedNumber. I know it'll give last one value but how can i get all value. kindly help

public String selectBlockedNumbers() {
        Cursor cursor = dbManagement.selectBlockedNumbers();
        String blockedNumber = null;
        cursor.moveToFirst();
        for(int i = 0; i < cursor.getCount(); i++) {
            blockedNumber = cursor.getString(cursor.getColumnIndex(dbManagement.ORIGINATING_ADDRESS));
            cursor.moveToNext();
        }

        Toast.makeText(this, blockedNumber, Toast.LENGTH_LONG).show();
        return blockedNumber;

    }

In

Cursor cursor = dbManagement.selectBlockedNumbers();

the function selectBlockedNumbers() consist of following query:

cursor = db.rawQuery("SELECT " + ORIGINATING_ADDRESS + " FROM " + TABLE_BLOCK_LIST, null);

1 Answers1

0

When the for-loop runs for the last time, blockedNumbers will contain the last number retrieved. So, instead of returning a String, rather returns a List<String>. In that case your code should change into

public List<String> selectBlockedNumbers() {
        Cursor cursor = dbManagement.selectBlockedNumbers();
        List<String> blockedNumbers = new ArrayList<String>();
        cursor.moveToFirst();
        for(int i = 0; i < cursor.getCount(); i++) {
            String blockedNumber = cursor.getString(cursor.getColumnIndex(dbManagement.ORIGINATING_ADDRESS));
            blockedNumbers.add(blockedNumber);
            cursor.moveToNext();
        }

        Toast.makeText(this, blockedNumber, Toast.LENGTH_LONG).show();
        return blockedNumbers;    
    }
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • thanks for help. it return List but i required only String type. – bc120201817 Zohaib Siddiq Apr 23 '16 at 20:52
  • basically i want to retreive these blockedNumbers and need to compare with a String – bc120201817 Zohaib Siddiq Apr 23 '16 at 20:53
  • Can you please share the code where you do the comparison? I can then suggest how you can modify it to work with the returned `List` – ishmaelMakitla Apr 23 '16 at 20:55
  • I want to compare with String s = "123"; – bc120201817 Zohaib Siddiq Apr 23 '16 at 20:58
  • In that case you want to check if any of the blocked numbers is "123" - you do this: `List blockedNumbers = selectBlockedNumbers(); for(String blockedNumber : blockedNumbers){ if("123".equals(blockedNumber)){ //do something here? } }` – ishmaelMakitla Apr 23 '16 at 21:03
  • Sorry, the code formatting got out of hand - please see my comment above yours. I show how you can then compare the returned numbers against "123" - please check if this solves your problem. – ishmaelMakitla Apr 23 '16 at 21:09
  • your effort highly appreciated. but i want a function which return only String so after it i just put this function to another file to compare values. – bc120201817 Zohaib Siddiq Apr 23 '16 at 21:14
  • In that case, please understand that it is not possible to return more than one String value as you desire (unless you'd rather return comma-separated string - containing blocked numbers separated by comma like "123,456") - please see related questions and consider their suggestions - http://stackoverflow.com/questions/15285270/is-it-possible-to-return-more-than-one-value-from-a-method-in-java, and http://stackoverflow.com/questions/457629/how-to-return-multiple-objects-from-a-java-method as well as http://stackoverflow.com/questions/4756558/return-more-than-one-value-from-a-function-in-java. – ishmaelMakitla Apr 23 '16 at 21:20