发生推送通知时更新recyclelerview

时间:2016-06-18 21:18:34

标签: android push-notification android-recyclerview recycler-adapter gcmlistenerservice

我正在使用Android应用程序,我使用gcm推送通知在我的应用程序中接收通知并在回收站视图中显示它。为此,我使用GcmListenerService。在我的片段中,我从广播接收器获取数据,它没问题,我没有任何问题。

public class NotifListFragment extends Fragment {

ParseJSON pjson ;
SharedPreferences prefs;
String message ;
String title ;
String timestamp ;

private BroadcastReceiver mRegistrationBroadcastReceiver;
private RecyclerView rv;
MessageAdapter mAdapter ;
private RecyclerView.LayoutManager mLayoutManager;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View drawer = inflater.inflate(R.layout.fragment_notif_list, container, false);
    rv = (RecyclerView) drawer.findViewById(R.id.recyclerview);
    rv.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(getActivity());
    rv.setLayoutManager(mLayoutManager);
    prefs = getActivity().getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);



    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
                // new push notification is received
                Toast.makeText(getActivity(), "Push notification is received!", Toast.LENGTH_LONG).show();

                message = intent.getStringExtra("message");
                title = intent.getStringExtra("title");
                timestamp = intent.getStringExtra("timestamp");
                Log.d("message -->", message);
                Log.d("title -->" , title);   // I get them and they're okay
                Log.d("time -->", timestamp);

                mAdapter = new MessageAdapter(getActivity(),getDataSet(message,title,timestamp,1));

                rv.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();

            }
        }
    };

    return drawer;
}


private ArrayList<MessageObj> getDataSet(String message,String title,String date,int size) {
    ArrayList results = new ArrayList<MessageObj>();

    for (int index = 0; index < size ; index++) {
        MessageObj obj = new MessageObj(message,title,date);
        results.add(index, obj);
    }
    return results;
}

但我的问题是如何将它们添加到回收站视图中。实际上我有两个问题。首先,我不能在每次收到推送通知时增加回收站视图的大小,并强制将静态大小设置为适配器,否则应用程序停止并发生空指针异常。第二个是当app关闭并再次打开时,没有行,所有项目都消失了,我不想成为这个。这是我的适配器。感谢并为长期问题道歉。

public class MessageAdapter extends RecyclerView
        .Adapter<MessageAdapter
       .DataObjectHolder> {
   private static String LOG_TAG = "MessageAdapter";
   public ArrayList<MessageObj> mDataset;
   Context c ;
private static MyClickListener myClickListener;

public static class DataObjectHolder extends RecyclerView.ViewHolder
        implements View
        .OnClickListener {
    TextView title;
    TextView message;
    TextView date ;

    public DataObjectHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.messageTitle);
        message = (TextView) itemView.findViewById(R.id.messageText);
        date = (TextView) itemView.findViewById(R.id.messageDate);
        Log.i(LOG_TAG, "Adding Listener");
       // itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        myClickListener.onItemClick(getAdapterPosition(), v);
    }
}

public void setOnItemClickListener(MyClickListener myClickListener) {
    this.myClickListener = myClickListener;
}


public MessageAdapter(Context context ,ArrayList<MessageObj> myDataset) {
    mDataset = myDataset;
    c = context;
}

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

    DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
    return dataObjectHolder;
}

@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
    holder.title.setText(mDataset.get(position).getTitle());
    holder.message.setText(mDataset.get(position).getMessage());
    holder.date.setText(mDataset.get(position).getDate());
}

public void addItem(MessageObj messageObj, int index) {
    mDataset.add(index, messageObj);
    notifyItemInserted(index);
}

public void deleteItem(int index) {
    mDataset.remove(index);
    notifyItemRemoved(index);
}

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

public interface MyClickListener {
    public void onItemClick(int position, View v);
}
}

0 个答案:

没有答案