Android自定义listView数组适配器第一次不起作用

时间:2018-01-16 11:47:52

标签: android listview firebase firebase-realtime-database

我有arrayAdapter的自定义listView。第一次安装应用程序时,它不会根据firebase值显示数据(显示默认数据)。但是如果我从缓存/最近清除应用程序,那么一切都运行正常。

这意味着在清除缓存/最近时,适配器在第二次运行时工作正常。

我做错了什么?

我已经应用adapter.notifyDataSetChanged();但没有任何反应。

清除最新/缓存后,一切正常。

由于

下面是我的适配器文件中的代码

=============================================== =========================

public class SaveFilteredContactsDetailsListAdapter extends ArrayAdapter<SaveFilteredContactDetails> implements View.OnClickListener{

    Context mContext;

    public SaveFilteredContactsDetailsListAdapter(ArrayList<SaveFilteredContactDetails> data, Context context) {
        super(context, R.layout.textforcontacts, data);
        this.dataSet = data;
        this.mContext=context;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        SaveFilteredContactDetails dataModel = getItem(position);

        ViewHolder viewHolder; 

        final View result;

        if (convertView == null) {

            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.textforcontacts, parent, false);
           result=convertView;

            convertView.setTag(viewHolder);




            /////firebase value checks////////
            firebaseInitializations();
            mDatabaseRef.addValueEventListener(new ValueEventListener() {

                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot ds:dataSnapshot.getChildren()){
                        RequestManager requestManager =new RequestManager();


                        ///////////check if any received request and waiting for responses, make response view available?/////////
                        if(ds.child("requests")
                         .child(UserID)
                         .hasChild(dataModel.get_idFromFirebase()))
                        {
                            requestManager.setReqStatus(ds.child("requests")
                                                      .child(UserID)
                                                      .child(dataModel.get_idFromFirebase())
                                                      .getValue(RequestManager.class)
                                                      .getReqStatus());
                            String tempStatus = requestManager.getReqStatus();

                            //Toast.makeText(mContext, tempStatus, Toast.LENGTH_SHORT).show();
                            if(tempStatus.equals("received")) {
                            viewHolder.pendingResponse.setVisibility(View.VISIBLE);
                            viewHolder.trackLayout.setVisibility(View.GONE);
                            viewHolder.unBlockedResponse.setVisibility(View.GONE);
                        }
                        if(tempStatus.equals("cancel"))
                        {
                            viewHolder.pendingResponse.setVisibility(View.GONE);
                            viewHolder.trackLayout.setVisibility(View.GONE);
                            viewHolder.unBlockedResponse.setVisibility(View.GONE);
                        }
                        if(tempStatus.equals("released"))
                        {
                            viewHolder.pendingResponse.setVisibility(View.GONE);
                            viewHolder.trackLayout.setVisibility(View.GONE);
                            viewHolder.unBlockedResponse.setVisibility(View.GONE);
                        }
                        if(tempStatus.equals("blocked"))
                        {
                            viewHolder.pendingResponse.setVisibility(View.GONE);
                            viewHolder.trackLayout.setVisibility(View.GONE);
                            viewHolder.unBlockedResponse.setVisibility(View.VISIBLE);
                        }
                        if(tempStatus.equals("unblocked"))
                        {
                            viewHolder.pendingResponse.setVisibility(View.GONE);
                            viewHolder.trackLayout.setVisibility(View.GONE);
                            viewHolder.unBlockedResponse.setVisibility(View.GONE);
                        }
                        if(tempStatus.equals("accepted"))
                        {
                            viewHolder.pendingResponse.setVisibility(View.GONE);
                            viewHolder.trackLayout.setVisibility(View.VISIBLE);
                            viewHolder.unBlockedResponse.setVisibility(View.GONE);
                        }
                    }

                    ///////////////check if we sent request to current user and whats the status?////////////////
                    if(ds.child("requests")
                         .child(dataModel.get_idFromFirebase())
                         .hasChild(UserID))
                    {
                        requestManager.setReqStatus(ds.child("requests")
                                                      .child(dataModel.get_idFromFirebase())
                                                      .child(UserID)
                                                      .getValue(RequestManager.class)
                                                      .getReqStatus());
                        String tempStatus = requestManager.getReqStatus();

                        //Toast.makeText(mContext, tempStatus, Toast.LENGTH_SHORT).show();
                        if(tempStatus.equals("received"))
                        {
                            viewHolder.releaseRequests.setVisibility(View.GONE);
                            viewHolder.pendingRequests.setVisibility(View.VISIBLE);
                            viewHolder.sendRequests.setVisibility(View.GONE);
                            viewHolder.blockedResponse.setVisibility(View.GONE);

                        }
                        if(tempStatus.equals("accepted"))
                        {
                            viewHolder.pendingRequests.setVisibility(View.GONE);
                            viewHolder.sendRequests.setVisibility(View.GONE);
                            viewHolder.releaseRequests.setVisibility(View.VISIBLE);
                            viewHolder.blockedResponse.setVisibility(View.GONE);

                        }
                        if(tempStatus.equals("rejected"))
                        {
                            viewHolder.releaseRequests.setVisibility(View.GONE);
                            viewHolder.pendingRequests.setVisibility(View.GONE);
                            viewHolder.sendRequests.setVisibility(View.VISIBLE);
                            viewHolder.blockedResponse.setVisibility(View.GONE);
                        }
                        if(tempStatus.equals("blocked"))
                        {
                            viewHolder.releaseRequests.setVisibility(View.GONE);
                            viewHolder.pendingRequests.setVisibility(View.GONE);
                            viewHolder.sendRequests.setVisibility(View.GONE);
                            viewHolder.blockedResponse.setVisibility(View.VISIBLE);
                        }
                        if(tempStatus.equals("unblocked"))
                        {
                            viewHolder.releaseRequests.setVisibility(View.GONE);
                            viewHolder.pendingRequests.setVisibility(View.GONE);
                            viewHolder.sendRequests.setVisibility(View.VISIBLE);
                            viewHolder.blockedResponse.setVisibility(View.GONE);
                        }
                    }

                    ///////////////check online offline status?////////////////
                    if(ds.child("onOffStatus")
                         .hasChild(dataModel.get_idFromFirebase()))
                    {
                        requestManager.setOnlineOfflineStatus(ds.child("onOffStatus")
                                                                .child(dataModel.get_idFromFirebase())
                                                                .getValue(RequestManager.class)
                                                                .isOnlineOfflineStatus());
                        if(requestManager.isOnlineOfflineStatus())
                            viewHolder.onOffStatusDot.setVisibility(View.VISIBLE);
                        else
                            viewHolder.onOffStatusDot.setVisibility(View.GONE);
                    }
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        result=convertView;
    }

=============================================== ==========================

我编码错了吗?

1 个答案:

答案 0 :(得分:0)

亲爱的@karthik vishnu kumar, 感谢代码中的纠正。

解决方案是,在之前的活动中,我编写了firebase实例,以便在onStop()上脱机。所以我需要从代码中删除它。

我学到的是,如果将在下一个活动中使用,那么永远不会使用firebase实例离线