繁体   English   中英

在android studio中,我试图从firebase中获取文本并使用datasnapshot将其中继到一个片段中

[英]In android studio, I'm trying to take text from firebase and relay it into a fragment using datasnapshot

我正在学习教程并获得了这段代码,但它崩溃了。 我不明白为什么。

我已经使用了调试器,它运行了这个,但是它说 mPostId 为空。 由于在线查看,我禁用了即时运行设置,但没有用。

public class ViewPostFragment extends Fragment {

    private static final String TAG = "ViewPostFragment";

    //widgets
    private TextView mContactSeller, mTitle, mDescription, mPrice, mLocation, mSavePost;
    private ImageView mClose, mWatchList, mPostImage;

    //vars
    private String mPostId;
    private Post mPost;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPostId = (String) getArguments().get(getString(R.string.arg_post_id));
        Log.d(TAG, "onCreate: got the post id: " + mPostId);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_view_post, container, false);
        mContactSeller = (TextView) view.findViewById(R.id.post_contact);
        mTitle = (TextView) view.findViewById(R.id.post_title);
        mDescription = (TextView) view.findViewById(R.id.post_description);
        mPrice = (TextView) view.findViewById(R.id.post_price);
        mLocation = (TextView) view.findViewById(R.id.post_location);
        mClose = (ImageView) view.findViewById(R.id.post_close);
        mWatchList = (ImageView) view.findViewById(R.id.add_watch_list);
        mPostImage = (ImageView) view.findViewById(R.id.post_image);
        mSavePost = (TextView) view.findViewById(R.id.save_post);

        init();

        hideSoftKeyboard();

        return view;
    }

    private void hideSoftKeyboard(){
        final Activity activity = getActivity();
        final InputMethodManager inputManager = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);

        inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    private void init() {
        getPostInfo();
    }

    private void getPostInfo(){
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

        Query query = reference.child("posts")
                .orderByValue()
                .equalTo(mPostId);

        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                DataSnapshot singleSnapshot = dataSnapshot.getChildren().iterator().next();
                if(singleSnapshot != null){
                    mPost = singleSnapshot.getValue(Post.class);
                    Log.d(TAG, "onDataChange: found the post: " + mPost.getTitle());

                    mTitle.setText(mPost.getTitle());
                    mDescription.setText(mPost.getDescription());

                    String price = "FREE";
                    if(mPost.getPrice() != null){
                        price = "$" + mPost.getPrice();
                    }
                    mPrice.setText(price);
                    String location = mPost.getCity() + ", " + mPost.getState_province() + ", " +
                            mPost.getCountry();
                    mLocation.setText(location);
                    UniversalImageLoader.setImage(mPost.getImage(), mPostImage);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

逻辑猫

2019-04-04 10:18:23.852 21110-21110/ie.bs E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ie.bs, PID: 21110
    java.util.NoSuchElementException
        at java.util.Collections$EmptyIterator.next(Collections.java:4235)
        at com.google.firebase.database.DataSnapshot$1$1.next(com.google.firebase:firebase-database@@16.1.0:296)
        at com.google.firebase.database.DataSnapshot$1$1.next(com.google.firebase:firebase-database@@16.1.0:287)
        at ie.bs.ViewPostFragment$1.onDataChange(ViewPostFragment.java:99)

这是错误在 getPostInfo() 方法中指向的行。

DataSnapshot singleSnapshot = dataSnapshot.getChildren().iterator().next();

谢谢你。

我知道这来晚了,但我希望它可以帮助某人。

尝试在您的 onDataChange 方法中执行此操作。

for(DataSnapshot snapshot: datasnapshot.getChildren() ){
   mPost = singleSnapshot.getValue(Post.class);
    //Handle your logic here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM