DeviceMessageListRecyclerViewAdapter.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.Intelligent.FamilyU.adapter;
  2. import android.content.Context;
  3. import android.support.v7.widget.RecyclerView;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.LinearLayout;
  8. import android.widget.TextView;
  9. import com.Intelligent.FamilyU.R;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. public class DeviceMessageListRecyclerViewAdapter extends RecyclerView.Adapter<DeviceMessageListRecyclerViewAdapter.ViewHolder> {
  13. private List<HashMap<String, String>> mlist;
  14. private Context context;
  15. private OnItemClickListener mOnItemClickListener;
  16. public static interface OnItemClickListener {
  17. void onItemClick(int position);
  18. void onItemLongClick(int position);
  19. }
  20. public DeviceMessageListRecyclerViewAdapter(Context context, List<HashMap<String, String>> list, OnItemClickListener onItemClickListener) {
  21. super();
  22. mlist = list;
  23. this.context = context;
  24. mOnItemClickListener = onItemClickListener;
  25. }
  26. public static class ViewHolder extends RecyclerView.ViewHolder {
  27. // each data item is just a string in this case
  28. public TextView title;
  29. public TextView message;
  30. public TextView time;
  31. public LinearLayout ll;
  32. public ViewHolder(View v) {
  33. super(v);
  34. }
  35. }
  36. // Create new views (invoked by the layout manager)
  37. @Override
  38. public DeviceMessageListRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
  39. int viewType) {
  40. // create a new view
  41. View convertView = LayoutInflater.from(context)
  42. .inflate(R.layout.listview_add_device_message_list_item, parent, false);
  43. ViewHolder viewHolder = new ViewHolder(convertView);
  44. viewHolder.title = (TextView) convertView.findViewById(R.id.title);
  45. viewHolder.message = (TextView) convertView.findViewById(R.id.message);
  46. viewHolder.time = (TextView) convertView.findViewById(R.id.time);
  47. viewHolder.ll = (LinearLayout) convertView.findViewById(R.id.ll);
  48. return viewHolder;
  49. }
  50. // Replace the contents of a view (invoked by the layout manager)
  51. @Override
  52. public void onBindViewHolder(ViewHolder holder, final int position) {
  53. // - get element from your dataset at this position
  54. // - replace the contents of the view with that element
  55. HashMap<String, String> map = mlist.get(position);
  56. holder.title.setText(map.get("title"));
  57. holder.message.setText(map.get("message"));
  58. holder.time.setText(map.get("time"));
  59. holder.ll.setOnClickListener(new View.OnClickListener() {
  60. @Override
  61. public void onClick(View v) {
  62. mOnItemClickListener.onItemClick(position);
  63. }
  64. });
  65. holder.ll.setOnLongClickListener(new View.OnLongClickListener() {
  66. @Override
  67. public boolean onLongClick(View v) {
  68. mOnItemClickListener.onItemLongClick(position);
  69. return true;
  70. }
  71. });
  72. }
  73. // Return the size of your dataset (invoked by the layout manager)
  74. @Override
  75. public int getItemCount() {
  76. return mlist.size();
  77. }
  78. public void refreshData(List<HashMap<String, String>> list) {
  79. if (null != mlist) {
  80. mlist.clear();
  81. }
  82. mlist.addAll(list);
  83. notifyDataSetChanged();
  84. }
  85. public List<HashMap<String, String>> getAllList() {
  86. return mlist;
  87. }
  88. }