-6

I have a little problem with Logic Java in my android Apps. I need to filter Array with this Condition :

Category A = 0 , B = 1 , C = 2 , D = AllNumber except 1 and 2

This is Button OnClickListener :

buttonAll.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                filteredSubGroupList = (ProgramMethod.filterArray(subGroupList, "main-group-nr", "" ,false));
                genericAdapterSubCategory.notifyDataSetChanged();
                genericAdapterArticle.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

    buttonFood.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                filteredSubGroupList = (ProgramMethod.filterArray(subGroupList, "main-group-nr", Integer.toString(1) ,true));
                genericAdapterSubCategory.notifyDataSetChanged();
                genericAdapterArticle.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

    buttonBeverage.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                filteredSubGroupList = (ProgramMethod.filterArray(subGroupList, "main-group-nr", Integer.toString(2) ,true));
                genericAdapterSubCategory.notifyDataSetChanged();
                genericAdapterArticle.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

    buttonOther.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            // D Value In here 

        }
    });

How to get the D value? , I read this and this but i dont have solution, Thanks in advance ..

Community
  • 1
  • 1
MrX
  • 953
  • 2
  • 16
  • 42
  • Please show your existing code that does any filtering – OneCricketeer Feb 03 '17 at 03:10
  • @cricket_007 I only want to know how i can get the D value – MrX Feb 03 '17 at 03:12
  • you got it , i add my code – MrX Feb 03 '17 at 03:20
  • Without a [mcve] of your problem and a clear description of your inputs and expected outputs, it's hard to know what you are asking. What JSON do you have? What is `subGroupList`? – OneCricketeer Feb 03 '17 at 03:26
  • filterArray have 3 params, subGroupList is array from my JSON, main-group-nr is field in JSONarray which i want to get the value, and the last params is the number that i want to filter, if the D condition is AllNumber except 1 & 2 how to get that value – MrX Feb 03 '17 at 03:34

3 Answers3

2

Based on the links you sent, it looks like you want to generate a random number that matches some specific conditions. To do that, you will have to keep generating random numbers until you get a number that is fine for you.

Example: (with the condition you stated)

Random random = new Random(); //your random number generator

int value;
int max = 10; //max value you want to get, exclusive
int min = 1 //minimum value you want to get, inclusive

do
{
    value = random.nextInt(max - min) + min; //generates a random number between min and max
}
while(value == 1 || value == 2); //will restart if value is 1 or 2, as you asked
Winter
  • 3,894
  • 7
  • 24
  • 56
  • i think i dont need max value, because the D value can be any number except 1 and 2, or can i set max value with unlimeted number? – MrX Feb 03 '17 at 03:23
  • @Mario You can use Integer.MAX_VALUE – OneCricketeer Feb 03 '17 at 03:27
  • @MarioMargoPradipta Nothing is "unlimited". Any integer in Java should between in `Integer.MAX_VALUE` and `Integer.MIN_VALUE`. Are you sure that's what you are looking for ? If so, replace min and max with those 2 values but by design, this doesn't make much sense. – Winter Feb 03 '17 at 22:11
0

Assuming that you have some Category class within a list and availability of some function to either filter the list directly of just create a new one....

List<Category> list = new ArrayList<Category>();

... 
List<Category> filtered = new ArrayList<Category>();
for (Category c : list) {
    if (c.a == 0 && c.b == 1 && c.c == 2 && !(c.d == 1 || c.d == 2))  {
        filtered.add(c);
    } 
} 

Or perhaps I don't understand what you're asking about "getting the D value"

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • `getting the D value` i mean i want to know, how i can get all number except n number, that is it – MrX Feb 03 '17 at 03:22
  • Again, unclear what "n number" means – OneCricketeer Feb 03 '17 at 03:23
  • -.- look at my Condition, D can be any number except 1 and 2. How can i get filter that number(etc 0,4,5,6,7,n,n,n, except 1 and 2) – MrX Feb 03 '17 at 03:25
  • From what you're asking me, sounds like you need a Hashset of illegal values (because it's the inverse of the things to keep), then use the contains method to check and filter out integers you don't want – OneCricketeer Feb 03 '17 at 03:30
0

I'm sorry for my Bad English so you all not uderstand with Question. I've solved my problem with this code

 for(int i=3; i<filteredSubGroupList .length(); i++){
                try {
                    filteredSubGroupList = (ProgramMethod.filterArray(subGroupList, "main-group-nr", String.valueOf(3), false));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

LOGCAT :

I/System.out: 3
I/System.out: [{"zknr":98,"bezeich":"CIGARETTE","fibukonto":"14","departement":1,"betriebsnr":98,"main-group-nr":3},{"zknr":700,"bezeich":"DISCOUNT","fibukonto":"00","departement":1,"betriebsnr":0,"main-group-nr":30}]

It's make me get all int except 1 & 2. Sorry again for my bad English, thanks for every suggest and answer .

MrX
  • 953
  • 2
  • 16
  • 42