无法使用查询从解析数据库中检索字符串

时间:2015-07-02 00:40:52

标签: java android parse-platform

我试图从解析对象中检索信息,但我存储的字符串保持等于null。

以下是我保存对象的方法

        // get current user
        ParseObject studentClasses = new ParseObject("StudentClasses");

        // register their periods into database
        studentClasses.put("student_id", ParseUser.getCurrentUser());
        studentClasses.put("first_period", ClassSelected_Period[PERIOD1]);
        studentClasses.put("second_period", ClassSelected_Period[PERIOD2]);
        studentClasses.put("third_period", ClassSelected_Period[PERIOD3]);
        studentClasses.put("fourth_period", ClassSelected_Period[PERIOD4]);
        studentClasses.put("fifth_period", ClassSelected_Period[PERIOD5]);
        studentClasses.put("sixth_period", ClassSelected_Period[PERIOD6]);
        studentClasses.put("seventh_period", ClassSelected_Period[PERIOD7]);

        // save the information into database
        studentClasses.saveInBackground();

它完全保存了我的数据库。 student_id是指向用户的指针,其余列是字符串。

我想检索所有这些字符串并在查询解析时将它们放在数组中它不起作用

这是我的查询

        // check if a user is not cached
        ParseUser currentUser = ParseUser.getCurrentUser();
        if (currentUser == null)
        {
            // prompt user to Register screen
            // create intent to start activity
            Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            // start new activity
            startActivity(intent);

            // stop current activity
            finish();
        }

        // query database for user's classes
        ParseQuery<ParseObject> query = ParseQuery.getQuery("StudentClasses");
        query.whereEqualTo("student_id", ParseUser.getCurrentUser());

        query.getFirstInBackground(new GetCallback<ParseObject>()
        {
            @Override
            public void done (ParseObject parseObject, ParseException e)
            {
                if (e == null)
                {
                    // retrieved the object
                    userClasses[PERIOD1] = parseObject.getString("first_period");
                    userClasses[PERIOD2] = parseObject.getString("second_period");
                    userClasses[PERIOD3] = parseObject.getString("third_period");
                    userClasses[PERIOD4] = parseObject.getString("fourth_period");
                    userClasses[PERIOD5] = parseObject.getString("fifth_period");
                    userClasses[PERIOD6] = parseObject.getString("sixth_period");
                    userClasses[PERIOD7] = parseObject.getString("seventh_period");
                }
                else
                {
                    // failed lookup. Do something here
                    Toast.makeText(getApplicationContext(),"Exception Thrown" ,Toast.LENGTH_SHORT).show();
                }
            }
        });

我查看了解析文档,它看起来应该可以工作,但它不会保存字符串

感谢任何帮助或评论!

编辑:我展示了更多我的查询部分代码,以显示当前用户

4 个答案:

答案 0 :(得分:2)

在这里我假设,你的解析数据库中存储了数据..它工作正常..所以要检索它尝试以下..

 ParseQuery<ParseObject> query = ParseQuery.getQuery("StudentClasses");
    query.whereEqualTo("student_id", ParseUser.getCurrentUser());

    query.findInBackground(new FindCallback<ParseObject>()
    {
        @Override
        public void done (List<ParseObject> list, ParseException e)
        {
            if (e == null)
            {
               for(int i=0; i < list.size(); i++) {
                userClasses[PERIOD1] = list.get(i).getString("first_period");
                userClasses[PERIOD2] = list.get(i).getString("second_period");
                userClasses[PERIOD3] = list.get(i).getString("third_period");
                userClasses[PERIOD4] = list.get(i).getString("fourth_period");
                userClasses[PERIOD5] = list.get(i).getString("fifth_period");
                userClasses[PERIOD6] = list.get(i).getString("sixth_period");
                userClasses[PERIOD7] = list.get(i).getString("seventh_period");
               }
            }
            else
            {
              Toast.makeText(getApplicationContext(),"Exception Thrown" ,Toast.LENGTH_SHORT).show();
            }
        }
    });
希望它有所帮助!

答案 1 :(得分:0)

在获取内容以使用该功能时尝试

parseObject.get("the_name_of_the_column")

哪个实际应该可以正常工作

答案 2 :(得分:0)

您需要了解的是,您的代码中studentClasses.saveInBackground();是异步调用,您需要在SaveCallback内部进行查询。然后,您可以确保在查询运行时保存的数据位于数据库中。 您收到了null数据,因为您在解析之前保存了数据。

 ParseObject studentClasses = new ParseObject("StudentClasses");
            // register their periods into database
            studentClasses.put("student_id", ParseUser.getCurrentUser());
            studentClasses.put("first_period", ClassSelected_Period[PERIOD1]);
            studentClasses.put("second_period", ClassSelected_Period[PERIOD2]);
            studentClasses.put("third_period", ClassSelected_Period[PERIOD3]);
            studentClasses.put("fourth_period", ClassSelected_Period[PERIOD4]);
            studentClasses.put("fifth_period", ClassSelected_Period[PERIOD5]);
            studentClasses.put("sixth_period", ClassSelected_Period[PERIOD6]);
            studentClasses.put("seventh_period", ClassSelected_Period[PERIOD7]);

            // save the information into database
           studentClasses.saveInBackground(new SaveCallback() {
       public void done(ParseException e) {
         if (e == null) {
           ParseQuery<ParseObject> query = ParseQuery.getQuery("StudentClasses");
           query.whereEqualTo("student_id", ParseUser.getCurrentUser());
           query.getFirstInBackground(new GetCallback<ParseObject>()
            {
            @Override
            public void done (ParseObject parseObject, ParseException e)
            {
                if (e == null)
                {
                    // retrieved the object
                    userClasses[PERIOD1] = parseObject.getString("first_period");
                    userClasses[PERIOD2] = parseObject.getString("second_period");
                    userClasses[PERIOD3] = parseObject.getString("third_period");
                    userClasses[PERIOD4] = parseObject.getString("fourth_period");
                    userClasses[PERIOD5] = parseObject.getString("fifth_period");
                    userClasses[PERIOD6] = parseObject.getString("sixth_period");
                    userClasses[PERIOD7] = parseObject.getString("seventh_period");
                }
                else
                {
                    // failed lookup. Do something here
                    Toast.makeText(getApplicationContext(),"Exception Thrown" ,Toast.LENGTH_SHORT).show();
                }
            }
        });
         } else {
          // myObjectSaveDidNotSucceed();
         }
       }
     });

希望这有帮助

答案 3 :(得分:0)

在睡了一觉之后再进行更多研究和修补我发现了问题。问题不在我的代码中;它在我的网络中...不完全是。您看到我的代码运行良好...运行时间很快(我的手机可以处理它);但是,方法

query.findInBackground(new FindCallback<ParseObject>()

异步运行意味着它不会以我的其他代码的速度运行。如果你考虑它是有道理的,因为它必须发送并等待数据库的响应。这就是为什么当我在里面干杯时,数据就在字符串中,但是当我试图在方法之外做几行之后,数据就不存在了。它没有了。

这是一个例子。我在创建时使用此代码

进行祝酒
@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // initialise and set toolbar as actionbar
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        // initialize nav bars
        initNavBars();

        // initialize drawer layout
        NavigationView navView = (NavigationView) findViewById(R.id.navigation_view);

        // initialize nav drawer
        navDrawer = (DrawerLayout) findViewById(R.id.drawer);
        initNavDrawer(navDrawer);

        // initialize layout manager for recycler view
        RecyclerView.LayoutManager mainLayoutManager = new LinearLayoutManager(this);

        // initialize data for all classes before setting adapter
        initClassData();  // <---- MY PARSE QUERY IS IN THIS METHOD

        // set the adapter for recycler view
        RecyclerView.Adapter mainAdapter = new MainRecyclerAdapter(classrooms);

        // initialize recycler view elements
        RecyclerView mainRecyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);

        // add layout manager to recycler view
        mainRecyclerView.setLayoutManager(mainLayoutManager);

        // add adapter to recycler view
        mainRecyclerView.setAdapter(mainAdapter);

        Toast.makeText(getApplicationContext(), userClasses[PERIOD1], Toast.LENGTH_SHORT).show(); // <----- HERE IS MY TOAST
    }

当我运行它时,toast为空,因为该字符串仍为null,但如果我使用此代码运行它

 @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // initialise and set toolbar as actionbar
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        // initialize nav bars
        initNavBars();

        // initialize drawer layout
        NavigationView navView = (NavigationView) findViewById(R.id.navigation_view);

        // initialize nav drawer
        navDrawer = (DrawerLayout) findViewById(R.id.drawer);
        initNavDrawer(navDrawer);

        // initialize layout manager for recycler view
        RecyclerView.LayoutManager mainLayoutManager = new LinearLayoutManager(this);

        // initialize data for all classes before setting adapter
        initClassData(); // <---- PARSE QUERY IS STILL IN THIS METHOD

        // set the adapter for recycler view
        RecyclerView.Adapter mainAdapter = new MainRecyclerAdapter(classrooms);

        // initialize recycler view elements
        RecyclerView mainRecyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);

        // add layout manager to recycler view
        mainRecyclerView.setLayoutManager(mainLayoutManager);

        // add adapter to recycler view
        mainRecyclerView.setAdapter(mainAdapter);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                // Do something after 5s = 5000ms
                Toast.makeText(getApplicationContext(), userClasses[PERIOD1], Toast.LENGTH_SHORT).show();
            }
        }, 5000);
    }

在这个例子中,我强迫吐司等待五秒钟以允许

query.getFirstInBackground(new GetCallback<ParseObject>()

完成查询数据库,当我运行它时,toast正确显示字符串。

因此,如果您的程序依赖于解析查询来获取重要数据,则必须以允许一秒或两秒传递以让解析查询返回的方式构造代码。

或者您可以在本地存储数据,然后您可以使用数据库作为数据的备份,并检查以确保每次用户想要切换数据时都是相同的。

谢谢你Ajay和Rasika我仍然会试图弄清楚为什么它不起作用,如果它不是你。