2016-04-16 5 views
0

にオブジェクトIDとParseUserを取得できませんフラグメントでそのオブジェクトIDParse.comから特定のユーザーを取得しようとしています。デバッガから、findinBackground、getInBackground、getFirstinbackgroundのコードは決して実行されないようです。はフラグメント

(フルコードでは)私のgetFirstInBackground方法

package com.example.jamesytl.hostel; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

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

import java.util.List; 

/** 
* Created by jamesytl on 15/4/2016. 
*/ 
public class hosteldetails extends Fragment{ 
    String ownerID,email,hostelno,roomtype,desc,status,area,name; 
    int price; 
    ImageView image; 
    TextView txtname,txtemail,txthostelno,txtprice,txtroomtype,txtdesc,txtstatus,txtarea; 
    hostelwithoutparse currenthostel; 
    Bundle bundle; 
    ParseUser owner; 
    byte[] imgByte; 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.hosteldetails, container, false); 
     bundle = this.getArguments(); 
     if (bundle != null) 
      currenthostel = bundle.getParcelable("item_selected_key"); 
     else 
      Toast.makeText(getActivity().getBaseContext(), "Failed", Toast.LENGTH_SHORT).show(); 

     hostelno = currenthostel.getHostelNo(); 
     price = currenthostel.getHostelPrice(); 
     roomtype = currenthostel.getHostelRoom(); 
     desc = currenthostel.getHostelDescription(); 
     status = currenthostel.getHostelStatus(); 
     area = currenthostel.getHostelArea(); 
     ownerID = currenthostel.getOwner(); 
     imgByte = currenthostel.getImgByte(); 

     ParseQuery<ParseUser> query = ParseUser.getQuery(); 
     query.whereEqualTo("objectId", ownerID); 
     query.getFirstInBackground(new GetCallback<ParseUser>() { 
      @Override 
      public void done(ParseUser object, ParseException e) { 
       name = object.getUsername(); 
       email = object.getEmail(); 
      } 
     }); 
     txtname = (TextView) v.findViewById(R.id.tvDOwnerName); 
     txtarea = (TextView) v.findViewById(R.id.tvDArea); 
     txtprice = (TextView) v.findViewById(R.id.tvDprice); 
     txthostelno = (TextView) v.findViewById(R.id.tvDHostelNo); 
     txtroomtype = (TextView) v.findViewById(R.id.tvDRoom); 
     txtdesc = (TextView) v.findViewById(R.id.tvDDesc); 
     txtstatus = (TextView) v.findViewById(R.id.tvDStatus); 
     txtemail = (TextView) v.findViewById(R.id.tvDEmail); 
     image = (ImageView) v.findViewById(R.id.imgDHostel); 


     Bitmap bitmap = BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length); 
     image.setImageBitmap(bitmap); 

     txtname.setText(name); 
     txtemail.setText(email); 
     txtarea.setText(area); 
     txtprice.setText("RM" + String.valueOf(price)); 
     txthostelno.setText(hostelno); 
     txtroomtype.setText(roomtype); 
     txtdesc.setText(desc); 
     txtstatus.setText(status); 
     return v; 
    } 

} 

私はこの作業を行うことができますどのように任意の手掛かり?それは私がそれをバックグラウンドで実行することができない原因となるフラグメントで実行していることに関連していますか?

+0

フラグメントの完全なコードを投稿できますか?私たちはそれがどこにあるのかを知る必要があります。 –

+0

更新のためにありがとうございます。あなたのメソッドが呼び出されていないかどうかをどのように知っていますか? –

+0

メソッドの位置にブレークポイントを置くと、デバッガはそのメソッドで停止しますか? –

答えて

1

これまでのところ、実際のコードに問題はありません。あなたのコメントによれば、query.getFirstInBackground(new GetCallback<ParseUser>()の部分が実行され、なぜ2番目の部分が​​が実行されないのか不思議です。

理由はquery.getFirstInBackground()は(バックグラウンドでデータをフェッチ)がバックグラウンドで実行される意味、非同期タスクであり、それがフェッチ終了したとき、それはpublic void done()でコードを実行することです。

は、だから私の提案は、彼らはフェッチが終了した 後に実行されるようにあなたは、 public void done()方法でコード を置くことです。次のように入力します。

query.getFirstInBackground(new GetCallback<ParseUser>() { 
     @Override 
     public void done(ParseUser object, ParseException e) { 
      name = object.getUsername(); 
      email = object.getEmail(); 

      Bitmap bitmap = BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length); 
      image.setImageBitmap(bitmap); 

      txtname.setText(name); 
      txtemail.setText(email); 
      txtarea.setText(area); 
      txtprice.setText("RM" + String.valueOf(price)); 
      txthostelno.setText(hostelno); 
      txtroomtype.setText(roomtype); 
      txtdesc.setText(desc); 
      txtstatus.setText(status); 
      return v; 
     } 
    }); 
+0

となっていましたが、今度はParseUserの情報が表示されますので、説明と回答に本当に感謝しています。非同期タスクなので、私の古いコードは、実際には、ページが正しく更新されずにテキストボックスに値が正しく割り当てられたという情報を取得したことを意味しますか? – JamesYTL

+0

あなたはまさに正しいです:)タイミングの問題でした! –

+0

これは私の時間を無駄にした。待っているように見えるように進捗バーを追加するだけでいいと思う。もう一度あなたの時間と教えに感謝します。大いに感謝します:D – JamesYTL

関連する問題