我有一个tabLayout,其中有Log Fragment选项卡,它将所有传入呼叫详细信息存储在LogsData List Object中并设置。
应用关闭并重新启动后,新的callLogs会更新,但是我想在呼叫处于振铃状态时刷新RecyclerView。

我的LogFragment

public class LogsFragment extends Fragment {
private RecyclerView mRecyclerView;
private ListAdapter mListadapter;
ArrayList data;
TinyDB tinyDB;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_call_log, container, false);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    tinyDB = new TinyDB(getContext());
    data = tinyDB.getListObject("LogData", LogsData.class);

    final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerView.setHasFixedSize(false);
    mRecyclerView.setLayoutManager(layoutManager);

    data = tinyDB.getListObject("LogData", LogsData.class);

    mListadapter = new ListAdapter(data);
    mListadapter.notifyDataSetChanged();
    mRecyclerView.setAdapter(mListadapter);

    return view;
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMyEvent(Object o) {
    /* Refresh your adapter */

    data = tinyDB.getListObject("LogData", LogsData.class);

    mListadapter = new ListAdapter(data);
    mListadapter.notifyDataSetChanged();
    mRecyclerView.setAdapter(mListadapter);
};
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
    private ArrayList<LogsData> dataList;

    public ListAdapter(ArrayList<LogsData> data) {
        this.dataList = data;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView textViewName;
        TextView textViewNumb;
        TextView textViewTime;

        public ViewHolder(View itemView) {
            super(itemView);
            this.textViewName =  itemView.findViewById(R.id.cName);
            this.textViewNumb =  itemView.findViewById(R.id.number);
            this.textViewTime =  itemView.findViewById(R.id.time_stamp);
        }
    }

    @Override
    public ListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.log_list, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.textViewName.setText(dataList.get(position).getName());
        holder.textViewNumb.setText(dataList.get(position).getNumber());
        holder.textViewTime.setText(dataList.get(position).getTime());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Item " + position + " is clicked.", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMyEvent(Object o) {
        /* Refresh your adapter */

        data = tinyDB.getListObject("LogData", LogsData.class);

        mListadapter = new ListAdapter(data);
        mListadapter.notifyDataSetChanged();
        mRecyclerView.setAdapter(mListadapter);
    };
}
@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
}


}

还有我的PhoneStateReceiver类

public class PhoneStateReceiver extends BroadcastReceiver {
public final static String TAG = "SSD";

String incomingNumber;
String state;
DateFormat timeFormat;
Context context;
String date;
String cName;
ArrayList data;


@SuppressLint("UnsafeProtectedBroadcastReceiver")
@Override
public void onReceive(Context context, Intent intent) {

    TinyDB tinydb = new TinyDB(context);
    data = tinydb.getListObject("LogData", LogsData.class);

    try {
        state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        cName = getContactName(incomingNumber, context);
        timeFormat = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm");
        date = timeFormat.format(Calendar.getInstance().getTime());
        Log.d(TAG, date);

        String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        if (phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            data.add(new LogsData(cName, incomingNumber, date));
            tinydb.putListObject("LogData", data);
            Toast.makeText(context, date + " " + incomingNumber + " " + cName, Toast.LENGTH_SHORT).show();
        }





 EventBus.getDefault().post(new Object());

    } catch (Exception e) {
        e.printStackTrace();
    }

}

public String getContactName(final String phoneNumber, Context context) {
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

    String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME};

    String contactName = "";
    Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            contactName = cursor.getString(0);
        }
        cursor.close();
    }

    return contactName;

}


}

我更新了密码,请查看。

最佳答案

使用EventBus来做
在您的LogsFragment中,准备订阅者是这样的:

@Subscribe(threadMode = ThreadMode.MAIN)
public void onMyEvent(Object o) {
    /* Refresh your adapter */

    data = tinyDB.getListObject("LogData", LogsData.class);

    mListadapter = new ListAdapter(data);
    mListadapter.notifyDataSetChanged();
    mRecyclerView.setAdapter(mListadapter);
};


然后在您的PhoneStateReceiver中这样呼叫您的订户:

EventBus.getDefault().post(new Object());


不要忘记registerunregister您的订户LogsFragment
您可以在here中阅读有关EventBus的更多信息。

07-24 09:55