回收视图没有显示

时间:2018-04-13 13:40:07

标签: android firebase

我无法看到回收视图,也不确定它出错的地方。如果你可以帮助我会很棒。

片段

public class CallDurationFragment extends Fragment {

    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    ArrayList<CallHistroy> callList = new ArrayList<>();
    ArrayList<CallHistroy> callListTemp = new ArrayList<>();
    ArrayList<CallHistroy> callObject = new ArrayList<>();
    ArrayList<CallHistroy> callListText = new ArrayList<>();
    private CallDurationFragment.OnFragmentInteractionListener mListener;
    private RecyclerView rvCall;
    private RecyclerView.Adapter callAdaptor;
    private RecyclerView.LayoutManager eLayoutManager;
    private EditText phoneNumber;
    private static final int        DIALOG_DATE_PICKER  = 100;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_callduration_list, container, false);

        phoneNumber = (EditText) getActivity().findViewById(editTextSearchKeyPhoneNo);


        Context context = getActivity();
        rvCall = (RecyclerView) rootView.findViewById(R.id.rvCallDuration);
        rvCall.setHasFixedSize(true);
        rvCall.setLayoutManager(eLayoutManager);
        eLayoutManager = new LinearLayoutManager(getActivity());
        phoneNumber.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                callListTemp =  getAllCallRecords();
                for(int i = 0 ; i < callListTemp.size() ; i++)
                    if(callListTemp.get(i).getPhoneNumber().contains(s.toString()) )
                        callListText.add(callListTemp.get(i));
                callList = calculateIncomingOutgoing();
                callAdaptor = new CallDurationAdapter(getActivity(), callList);
                rvCall.setAdapter(callAdaptor);
            }
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
            }
        });
        callList = calculateIncomingOutgoing();
        callAdaptor = new CallDurationAdapter(getActivity(), callList);
        rvCall.setAdapter(callAdaptor);
        return rootView;
    }
    public ArrayList<CallHistroy> getAllCallRecords(){
        DatabaseReference ref = database.getReference();
        ref.child("Call").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                callObject.clear();
                HashMap<String,Object> call = null;
                Iterator<DataSnapshot> items = dataSnapshot.getChildren().iterator();
                while(items.hasNext()){
                    DataSnapshot item = items.next();
                    Log.e("Listener",item.toString() );
                    call =(HashMap<String, Object>) item.getValue();
                    callObject.add(new CallHistroy(call.get("phoneNumber").toString(),call.get("mode").toString(),call.get("duration").toString(), call.get("date").toString(),item.getKey()));
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
        return callObject;
    }

    public ArrayList<CallHistroy> calculateIncomingOutgoing(){
        if(callListText.size() > 0){
            callList = callListText;
        }else {
            callList = getAllCallRecords();
        }
        ArrayList<CallHistroy> callHistroyIncomingOutgoing = new ArrayList<>();
        if(callList.size() > 0){
            if(callList.get(0).getMode().equals("Outgoing")) {
                callHistroyIncomingOutgoing.add(new CallHistroy(callList.get(0).getPhoneNumber(), "0", callList.get(0).getDuration()));
            }else{
                callHistroyIncomingOutgoing.add(new CallHistroy(callList.get(0).getPhoneNumber(), callList.get(0).getDuration(), "0"));
            }
        }
        for( int i = 1  ; i < callList.size() ; i++){
            boolean setValue = false;
            for (int j = 0; j < callHistroyIncomingOutgoing.size() ;j++){
                if( callHistroyIncomingOutgoing.get(j).getPhoneNumber().equals(callList.get(i).getPhoneNumber())){
                    setValue = true;

                     int incoming = Integer.parseInt(callHistroyIncomingOutgoing.get(j).getIncomingDuration());
                    int outgoing =  Integer.parseInt( callHistroyIncomingOutgoing.get(j).getOutgoingDuration());
                    int duration = Integer.parseInt( callList.get(i).getDuration());
                    if(callList.get(i).getMode().equals("Outgoing")) {
                        callHistroyIncomingOutgoing.get(j).updateDuration(String.valueOf(incoming), String.valueOf(outgoing + duration));
                    }else{
                        callHistroyIncomingOutgoing.get(j).updateDuration(String.valueOf(incoming + duration), String.valueOf(outgoing));
                    }
                }
            }
            if(!setValue) {
                if(callList.get(i).getMode() == "Outgoing") {
                    callHistroyIncomingOutgoing.add(new CallHistroy(callList.get(i).getPhoneNumber(), "0", callList.get(i).getDuration()));
                }else{
                    callHistroyIncomingOutgoing.add(new CallHistroy(callList.get(i).getPhoneNumber(), callList.get(i).getDuration(), "0"));
                }
            }
        }
        return  callHistroyIncomingOutgoing;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof CallListFragment.OnFragmentInteractionListener) {
            mListener = (CallDurationFragment.OnFragmentInteractionListener) context;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onPause(){
        super.onPause();
    }

    @Override
    public void onDestroyView(){
        super.onDestroyView();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

适配器

RecyclerView.Adapter<CallDurationAdapter.ViewHolder> {

    private List<CallHistroy> mCalls;
    private Context mContext;

    public CallDurationAdapter(Context context, List<CallHistroy> calls) {
        mCalls = calls;
        mContext = context;
    }

    private Context getContext() {
        return mContext;
    }

    @Override
    public CallDurationAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View callDurationView = inflater.inflate(R.layout.item_call_duration_details, parent, false);

        // Return a new holder instance
        ViewHolder viewHolder = new ViewHolder(callDurationView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(CallDurationAdapter.ViewHolder viewHolder, final int position) {
        // Get the data model based on position
        final CallHistroy ch = mCalls.get(position);

        // Set item views based on your views and data model
            viewHolder._phoneNoTextView.setText(ch.getPhoneNumber());
            viewHolder._incomingTextView.setText(ch.getIncomingDuration());
            viewHolder._outgoingTextView.setText(ch.getOutgoingDuration());
            final String key = ch.getKey();
    }


    @Override
    public int getItemCount() {
        return mCalls.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView _phoneNoTextView;
        public TextView _incomingTextView;
        public TextView _outgoingTextView;


        public ViewHolder(View itemView) {

            super(itemView);
            _phoneNoTextView = (TextView) itemView.findViewById(R.id.rvs_duration_phone_no);
            _incomingTextView = (TextView) itemView.findViewById(R.id.rvs_duration_outing_total_call);
            _outgoingTextView = (TextView) itemView.findViewById(R.id.rvs_duration_incoming_total_call);

        }
    }
}

活动

public class CallDurationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_duration);
        CallDurationFragment newFragment = new CallDurationFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.recordFragmentContainer, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public void refreshCallDuration(View v) {
        Intent intent = new Intent(this, CallDurationActivity.class);
        startActivity(intent);
    }

    protected void onPause() {
        super.onPause();
    }

    protected void onStop() {
        super.onStop();
    }

    protected void onDestroy() {
        super.onDestroy();
    }

    public void onBackCallDuration(View v) {
        if (getFragmentManager().getBackStackEntryCount() > 0) {
            getFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }

}

对象

public class CallHistroy implements Comparable<CallHistroy> {

    String phoneNumber;
    String mode;
    String duration;
    String date;
    String key;
    String incomingDuration;
    String outgoingDuration;
    int totalIncoming;
    int totalOutgoing;

    public CallHistroy(String _phontNumber, String _incomingDuration, String _outgoingDuration ){
        setPhoneNumber( _phontNumber );
        setIncomingDuration( _incomingDuration );
        setOutgoingDuration( _outgoingDuration );
    }

    public CallHistroy(String _date, int _totalIncoming, int _totalOutgoing ){
        setDate(_date);
        setTotalIncoming( _totalIncoming );
        setTotalOutgoing( _totalOutgoing );
    }

    public void updateCall(int _totalIncoming, int _totalOutgoing){
        setTotalIncoming( _totalIncoming );
        setTotalOutgoing( _totalOutgoing );
    }

    public void updateDuration(String _incomingDuration, String _outgoingDuration){
        setOutgoingDuration( _incomingDuration );
        setIncomingDuration( _outgoingDuration );
    }

    public CallHistroy(String _phoneNumber, String _mode, String _duration, String _date ){
        setPhoneNumber( _phoneNumber );
        setDuration( _duration );
        setDate( _date );
        setMode( _mode );
    }

    public CallHistroy(String _phoneNumber, String _mode, String _duration, String _date, String _key ){
        setPhoneNumber( _phoneNumber );
        setDuration( _duration );
        setDate( _date );
        setMode( _mode );
        setKey( _key );
    }

    public String getIncomingDuration() {
        return incomingDuration;
    }

    public String getOutgoingDuration() {
        return outgoingDuration;
    }

    public int getTotalIncoming() {
        return totalIncoming;
    }

    public int getTotalOutgoing() {
        return totalOutgoing;
    }

    public void setIncomingDuration(String incomingDuration) {
        this.incomingDuration = incomingDuration;
    }

    public void setOutgoingDuration(String outgoingDuration) {
        this.outgoingDuration = outgoingDuration;
    }

    public void setTotalIncoming(int totalIncoming) {
        this.totalIncoming = totalIncoming;
    }

    public void setTotalOutgoing(int totalOutgoing) {
        this.totalOutgoing = totalOutgoing;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public String getDate() {
        return date;
    }

    public String getMode() {
        return mode;
    }

    public String getDuration() {
        return duration;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    @Override
    public int compareTo(@NonNull CallHistroy callHistroyObject) {

        String[] part = callHistroyObject.date.split("/");
        String[] part1 = date.split("/");
        int date = Integer.parseInt(part[0]);
        int month = Integer.parseInt(part[1]);
        String _year = part[2];
        int year = Integer.parseInt(_year.substring(0,4));

        int date1 = Integer.parseInt(part1[0]);
        int month1 = Integer.parseInt(part1[1]);
        String _year1 = part1[2];
        int year1 = Integer.parseInt(_year1.substring(0,4));
        if(year > year1)
            return  -1;
        else if(month > month1)
            return  -1;
        else if(date >date1)
            return -1;
        else
            return 0;
    }
}

1 个答案:

答案 0 :(得分:1)

您在布局管理器创建之前设置了

rvCall.setLayoutManager(eLayoutManager);
eLayoutManager = new LinearLayoutManager(getActivity());

应该是

eLayoutManager = new LinearLayoutManager(getActivity());
rvCall.setLayoutManager(eLayoutManager);
相关问题