如果在Parse.com中找不到数据,则显示Toast

时间:2016-03-22 11:42:02

标签: android parse-platform data-retrieval

我使用Parse.com作为我的后端存储。我正在维护一些学生的数据,如姓名,卷号,百分比等。我正在通过在微调器中选择卷号来搜索学生的数据。直到现在它工作正常,能够在列表中显示姓名和手机号码。

但如果在数据库中找不到特定卷号的数据,如何显示Toast。我正在使用这个代码,如果找到数据会得到很好的结果但是如果没有找到结果我无法显示Toast。

我的代码在这里:

    s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            final String result=list.get(position);


            ParseQueryAdapter.QueryFactory<AnywallPost> factory =
                    new ParseQueryAdapter.QueryFactory<AnywallPost>() {
                        public ParseQuery<AnywallPost> create() {

                            final ParseQuery<AnywallPost> query = AnywallPost.getQuery();
                            query.include("user");
                            query.orderByDescending("createdAt");
                            query.whereEqualTo("RollNumber",result);

                            /*
                            //Added by Tara
                           query.findInBackground(new FindCallback<AnywallPost>() {
                               @Override
                               public void done(List<AnywallPost> list, com.parse.ParseException e) {
                                   if (list.size()!=0){
                                       Toast.makeText(getApplicationContext(),"Found",Toast.LENGTH_SHORT).show();
                                   }
                                   else {
                                       Toast.makeText(getApplicationContext(),"not Found",Toast.LENGTH_SHORT).show();
                                   }
                               }
                           }); */


                            return query;
                        }
                    };

            // Set up the query adapter
            postsQueryAdapter = new ParseQueryAdapter<AnywallPost>(SearchStudentList.this, factory) {
                @Override
                public View getItemView(AnywallPost post, View view, ViewGroup parent) {
                    if (view == null) {
                        view = View.inflate(getContext(), R.layout.listview_item, null);
                    }

                    TextView usernameView = (TextView) view.findViewById(R.id.search_username_TV);
                    TextView mobileView = (TextView) view.findViewById(R.id.search_mobile_TV);

                    usernameView.setText(post.getUser().getUsername());
                    mobileView.setText(post.getTextMobile());

                    return view;
                }
            };
            // Disable automatic loading when the adapter is attached to a view.
            postsQueryAdapter.setAutoload(false);

            // Disable pagination, we'll manage the query limit ourselves
            postsQueryAdapter.setPaginationEnabled(false);
            ListView postsListView = (ListView) findViewById(R.id.search_student_LV);
            postsListView.setAdapter(postsQueryAdapter);

            postsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    final AnywallPost item = postsQueryAdapter.getItem(position);
                    selectedPostObjectId = item.getObjectId();



                    String selectedpostMobile = item.getTextMobile();
                    Toast.makeText(getApplicationContext(), selectedpostMobile, Toast.LENGTH_SHORT).show();

                }
            });

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

这是我的Log cat,请检查一下:

      03-22 17:19:15.682  20634-                     
          20644/com.example.avenirlabs_pc1.testblooddonorapp W/art﹕       
           Suspending all threads took: 328.725ms

      03-22 17:19:15.886  20634-
           20634/com.example.avenirlabs_pc1.testblooddonorapp        
         D/AndroidRuntime﹕ Shutting down VM
      03-22 17:19:15.900  20634-    
          20634/com.example.avenirlabs_pc1.testblooddonorapp         
           E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.avenirlabs_pc1.testblooddonorapp, PID: 20634
java.lang.RuntimeException: This query has an outstanding network connection. You have to wait until it's done.
        at com.parse.ParseQuery.checkIfRunning(ParseQuery.java:272)
        at com.parse.ParseQuery.doWithRunningCheck(ParseQuery.java:871)
        at com.parse.ParseQuery.doInBackground(ParseQuery.java:895)
        at com.parse.ParseQuery.findInBackground(ParseQuery.java:930)
        at com.parse.ParseQueryAdapter.loadObjects(ParseQueryAdapter.java:347)
        at com.parse.ParseQueryAdapter.loadObjects(ParseQueryAdapter.java:327)
        at com.example.avenirlabs_pc1.testblooddonorapp.SearchDonorList.doListQuery(SearchDonorList.java:673)
        at com.example.avenirlabs_pc1.testblooddonorapp.SearchDonorList.onLocationChanged(SearchDonorList.java:633)
        at com.google.android.gms.location.internal.zzk$zzb.handleMessage(Unknown Source)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5343)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
       03-22 17:19:16.087  20634-
        20649/com.example.avenirlabs_pc1.testblooddonorapp W/art﹕ Suspending  
         all threads took: 7.403ms

        03-22 17:19:16.100  20634-
         20649/com.example.avenirlabs_pc1.testblooddonorapp I/art﹕ Background 
              sticky concurrent mark sweep GC freed 23032(1037KB) AllocSpace     
        objects, 5(652KB) LOS objects, 10% free, 17MB/19MB, paused 9.673ms             
        total 162.031ms
           03-22 17:19:17.267  20634-
         20649/com.example.avenirlabs_pc1.testblooddonorapp I/art﹕ Background 
      partial concurrent mark sweep GC freed 34071(1339KB) AllocSpace       
       objects, 48(2MB) LOS objects, 39% free, 15MB/25MB, paused 2.722ms 
  total 123.637ms

0 个答案:

没有答案