一直在尝试从 Parse.com 中检索特定用户,并在片段中使用对象ID 。从调试器看来,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 :(得分:1)
好的,到目前为止您的实际代码没有任何问题。根据您的评论,query.getFirstInBackground(new GetCallback<ParseUser>()
部分确实已执行,您想知道为什么第二部分:public void done(ParseUser object, ParseException e)
未执行。
原因是query.getFirstInBackground()
是一个异步任务,这意味着它将在后台运行(在后台获取数据),当它完成提取时,它将执行public void done()
中的代码。
因此,我的建议是您将代码 IN public void done()
方法,以便执行 AFTER 完成提取。如下:
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;
}
});