1

i am trying to search if an array column i have of ingredients in parse contains the given ingredients by the user,for some reason,no query works for me,it always returns null.

any suggestions?

thanks.

enter image description here

my code:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import android.app.Activity;
import android.app.DownloadManager.Query;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class FridgeRecipe extends Activity 
{
    ListView ingsListView;
    EditText ingredientEt;
    TextView mainTitleTv, endTileTv;
    Button addingBtn, searchBtn, removeBtn;
    ArrayList<IngrdientView> ingredients;
    ArrayList<String>  recipeNameResults;
    final ArrayList<String> ingredientsToCheckInDB = new ArrayList<String>();
    final int idInList = 0;
    IngredientsListAdapter ila;
    AutoCompleteTextView actv;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fridge_recipe);
        ingsListView = (ListView) findViewById(R.id.lIngredientsListView);
        mainTitleTv = (TextView) findViewById(R.id.fridgeRecipeTv);
        endTileTv = (TextView) findViewById(R.id.recipeFindTv);
        actv = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
        addingBtn = (Button) findViewById(R.id.ingAddBtn);
        searchBtn = (Button) findViewById(R.id.recFindBtn);
        removeBtn = (Button) findViewById(R.id.removeButton);
        ingredients = new ArrayList<IngrdientView>();
        recipeNameResults = new ArrayList<String>();
        ila = new IngredientsListAdapter(this, ingredients, ingsListView);
        ingsListView.setAdapter(ila);
        addingBtn.setOnClickListener(new View.OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {
                ila.clear();
                String ingredient = actv.getText().toString().toLowerCase();
                ingredientsToCheckInDB.add(ingredient);
                ingredients.add(new IngrdientView(ingredient, idInList));
                actv.setText("");
                ila.notifyDataSetChanged();
            }
        });


        searchBtn.setOnClickListener(new View.OnClickListener() 
        {


            @SuppressWarnings("unchecked")
            @Override
            public void onClick(View v) 
            {
                for(int i = 0 ; i <ingredientsToCheckInDB.size(); i++)
                {
                    Log.i("item:   !!!", ""+ingredientsToCheckInDB.get(i).toString());
                }
                Log.i("-----------", ""+ingredientsToCheckInDB.size());

                ParseQuery<ParseObject> _query = ParseQuery.getQuery("RecipesNewDb");

                _query.whereContains("IngredientsForSearch", "water");
                _query.findInBackground(new FindCallback<ParseObject>() 
                        {

                    @Override
                    public void done(List<ParseObject> objects, ParseException e) 
                    {
                        if(e==null)
                        {
                            for(int  i = 0 ; i <objects.size() ; i ++)
                            {
                                Log.i("objs ---->","->");//""+objects.get(i).toString());
                            }
                        }
                        else
                        {
                            Log.i("eeeeeeeeee", ""+e);
                        }

                    }
                        });
                //              _query.whereEqualTo("RecipeName", "Lemon Ice Cream");
                //              _query.getFirstInBackground(new GetCallback<ParseObject>() 
                //                      {
                //                  public void done(ParseObject object, ParseException e) 
                //                  {
                //                      if (object == null) {
                //                          Log.d("score", "The getFirst request failed.");
                //                      } else {
                //                          Log.d("score", "Retrieved the object.");
                //                          Log.i("value: ", ""+object.getString("RecipeName"));
                //                      }
                //                  }
                //                      });




                ParseQuery<ParseObject> query = ParseQuery.getQuery("RecipesNewDb");
                query.whereContainsAll("IngredientsForSearch",  ingredientsToCheckInDB);
                query.findInBackground(new FindCallback<ParseObject>() 
                        {

                    @Override
                    public void done(List<ParseObject> objects, ParseException e) 
                    {
                        Log.i("objects:  ", "" +objects.size());
                        // recipeNameResults.add(objects.toString());
                        //if (e == null)
                        //{
                        for (int i = 0; i < objects.size(); i++) 
                        {
                            recipeNameResults.add(objects.get(i).get("RecipeName").toString());
                            Log.i("name:  ",objects.get(i).get("RecipeName") + "");
                        }
                        //}
                        //else {
                        Log.i("e________", "" + e);
                        //}
                    }
                        });
            }
        });

    }

}
3
  • Step through with the debugger and watch the values. You will find the problem in minutes. Log is a usually a lousy way to debug - as it is here. Commented Nov 12, 2014 at 10:59
  • did.couldn't find the reason Commented Nov 12, 2014 at 11:50
  • You examined each of the objects returned by the calls and they are correct? Therefore, your code is correct. You simply haven't learned how to use the debugger properly. The number of Log calls in your code is good evidence that you do not know how to debug. Commented Nov 12, 2014 at 14:06

2 Answers 2

1

I had a similar problem to yours, I was trying to get a String array that I saved in parse and put in a text view (I also saved the users username in a seperate key), here is a solution:

**

  1. Storing the Arrays

    public class names extends ActionBarActivity {
       List<String> list = new ArrayList<String>();
    
    @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_names);
    
       //the button to store the array of strings
      mInterests.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // the empty string is added because of the querying method   
           //  i used in getting back the array.
           list.add("");
    
             // to get the user username and store in a String
            ParseUser parseUser = ParseUser.getCurrentUser();
            String userName = parseUser.getUsername();
    
          //save the String array
            ParseObject parseObject= new ParseObject("Names");
             // Your list can contain more than the empty String
            parseObject.put("StringArray", list);
            parseObject.put("name", userName);
           parseObject.saveInBackground();
        }
      }
    

**

  1. Getting the string array back from parse and putting in a textView

          public class MainActivity extends ActionBarActivity {
      @Override
       protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main_Activity);
          tvInterests = (TextView)findViewById(R.id.userInterests);
    
            ParseUser parseUser = ParseUser.getCurrentUser();
         currentuserUsername = parseUser.getUsername();
    
    
          List list1 = new ArrayList();
         list1.add("");
    
         ParseQuery<ParseObject> query = ParseQuery.getQuery("Names");
         query.whereEqualTo("name", currentuserUsername);
         query.whereContainedIn("StringArray", list1);
    
        query.getFirstInBackground(new GetCallback<ParseObject>() {
        @Override
        public void done(ParseObject parseObject, ParseException e) {
            if(e == null){
                if(parseObject != null){  JSONArray userInterest=parseObject.getJSONArray("StringArray");
    
                    try {
                        String[] resultingArray = userInterest.join(",").split(",");
                       // tvInterest.setText(Arrays.toString(resultingArray));
                        tvInterest.setText(Arrays.toString(resultingArray).replaceAll("\\[|\\]", ""));
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setMessage(e1.getMessage());
                        builder.setTitle("Error changing json array");
                        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // remove the alert dialog
                                dialog.dismiss();
    
                            }
                        });
                        AlertDialog dialog = builder.create();
                        dialog.show();
    
                    }
    
    
    
                }else {
                    // there was an error
                }
            }else{
                //there was an error
            }
        }
    });
       }
    }
    

    I honestly hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

0

I saw that nobody answered this question for you, so here's a hopefully sufficient answer:

as documented by the Parse docs, you should use whereEqualTo(key, value) instead of whereContains(key, value) to search array fields.

The official doc is --> https://parse.com/docs/relations_guide and there example is as follows

" Suppose in this game app, we want to make sure that every Game object is associated with a Parse User. We can implement this like so:

To add the the object to the database

ParseObject game = new ParseObject("Game");
game.put("createdBy", ParseUser.getCurrentUser());

To retrieve the object from the database table's field of type array

ParseQuery<ParseObject> gameQuery = ParseQuery.getQuery("Game");
gameQuery.whereEqualTo("createdBy", ParseUser.getCurrentUser());

That worked perfectly for me when trying to retrieve all messages a user is receiving to display in a list. I hope it helps you in finding some water to drink. (Don't) stay thirsty, my friend :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.