我正在尝试搜索我的解析成分的数组列是否包含用户给定的成分,由于某种原因,没有查询对我有用,它总是返回null。
有什么建议吗?
感谢。
我的代码:
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);
//}
}
});
}
});
}
}
答案 0 :(得分:1)
我遇到了类似的问题,我试图获得一个我保存在解析中并放入文本视图的String数组(我还将用户用户名保存在单独的密钥中),这是一个解决方案:
**
存储数组
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();
}
}
**
从解析中获取字符串数组并放入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
}
}
});
}
}
老实说,我希望它有所帮助。
答案 1 :(得分:0)
我看到没有人为你回答这个问题,所以这是一个充满希望的答案:
如Parse文档所述,你应该使用whereEqualTo(key,value)而不是 whereContains(key,value)来搜索数组字段。
官方文件是 - &gt; https://parse.com/docs/relations_guide,示例如下
” 假设在这个游戏应用程序中,我们希望确保每个Game对象都与Parse User相关联。我们可以这样实现:
将对象添加到数据库
ParseObject game = new ParseObject("Game");
game.put("createdBy", ParseUser.getCurrentUser());
从数据库表的array类型字段
中检索对象ParseQuery<ParseObject> gameQuery = ParseQuery.getQuery("Game");
gameQuery.whereEqualTo("createdBy", ParseUser.getCurrentUser());
当我尝试检索用户正在接收的所有消息以在列表中显示时,这对我来说非常有用。我希望它可以帮助你找到一些饮用水。 (不)口渴,我的朋友:)