-1

Code snippet

 public void parsequery()
 {

        ParseQuery<ParseObject> query = ParseQuery.getQuery("Products");
        query.whereEqualTo("StoreID", StoreID);
        query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {

                    for(int j=0; j<array.size();j++){


                            ParseQuery<ParseObject> query1 = ParseQuery.getQuery("Products");
                            c = array.get(j);
                            Log.d("C",""+c);
                            query1.whereEqualTo("SubCategory", c);
                            query1.findInBackground(new FindCallback<ParseObject>() {
                                public void done(List<ParseObject> objects1, ParseException e) {
                                    if (e == null) {

                                        productList.clear();
                                        Log.d("Inner LOOP",""+objects1.size());
                                        for(int i=0; i<objects1.size();i++)
                                        {

                                            SingleRow temp1 = new SingleRow(objects1.get(i).getString("Name"),objects1.get(i).getString("Description"),objects1.get(i).getInt("Price"),objects1.get(i).getString("Store"),objects1.get(i).getObjectId(),1);
                                            productList.add(temp1);
                                        }
                                        productList  temp11 = new productList(productList);

                                        productList1.add(temp11);

                                    }else{
                                        Log.d("score", "Error: " + e.getMessage());
                                    }

                                }

                                }); 
                    }

                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {

                                for(int k =0; k<array.size();k++){

                                Log.d("ProductList22",""+productList1.get(k).getProductList());
                                ArrayList<SingleRow>  temp2 =  productList1.get(k).getProductList();
                                SubcategorySinglerow temp = new SubcategorySinglerow(array.get(k),temp2);
                                categoryList.add(temp);
                                bool=true;  
                                }
                                myList.addHeaderView(header, null, false);
                                Log.d("categoryListAdapter", ""+categoryList);
                                listAdapter = new MyListAdapter(getActivity(), categoryList);
                                myList.setAdapter(listAdapter);
                                layout.setVisibility(View.GONE);
                                myList.setVisibility(View.VISIBLE);
                            }
                        }, 5000);

                } else {
                    Log.d("score", "Error: " + e.getMessage());
                }
    }
        });
 }

Here i am passing value productList1.get(k).getProductList(); to subcategorysinglerow class via productList which i am clearing after every for loop execution because i need to add new values to it. Hence it is passing finally value of productList for every productList1 in class productList as java is pass by value. Is there a way to pass value by reference in java. How do i solve my problem.

2 Answers2

0

You, probably, can make your variable a member of the class.

Or you can do

class MyValue {
 public int val;
}

MyValue v = new MyValue();
v.val = 1;

// In other class
void modify(MyValue v) {
   v.val = 2;
}
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
-1

Java isn't pass by value, not really. All variables for classes in Java are references. Think of them as C++ pointers (they're really usually pointers to pointers, but that's needless details for now). So you're passing a pointer by value, which is the same thing as passing the value pointed to by reference. If you change a value in the reference, it will be changed globally. The only exceptions to this is for basic data types- int, boolean, double, etc because they aren't references.

Now there are some classes that are immutable- there are no functions that change their internal values. For these you can easily get around it by passing a data structure that has the actual data you want as an instance variable..

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127