CpuAndMemoryListRecyclerViewAdapter.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 CpuAndMemoryListRecyclerViewAdapter extends RecyclerView.Adapter<CpuAndMemoryListRecyclerViewAdapter.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 CpuAndMemoryListRecyclerViewAdapter(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 cpuTv;
  30. public TextView memoryTv;
  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 CpuAndMemoryListRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
  39. int viewType) {
  40. // create a new view
  41. View convertView = LayoutInflater.from(context)
  42. .inflate(R.layout.listview_cpu_memory_listitem, parent, false);
  43. ViewHolder viewHolder = new ViewHolder(convertView);
  44. viewHolder.title = (TextView) convertView.findViewById(R.id.title);
  45. viewHolder.cpuTv = (TextView) convertView.findViewById(R.id.cpu_tv);
  46. viewHolder.memoryTv = convertView.findViewById(R.id.memory_tv);
  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. String cpuStr = context.getResources().getString(R.string.home_cpu_use);
  58. String resultCpu = String.format(cpuStr, map.get("cpu"));
  59. holder.cpuTv.setText(resultCpu);
  60. String memoryStr = context.getResources().getString(R.string.home_memory_use);
  61. String result = String.format(memoryStr, map.get("memory"));
  62. holder.memoryTv.setText(result);
  63. holder.ll.setOnClickListener(new View.OnClickListener() {
  64. @Override
  65. public void onClick(View v) {
  66. mOnItemClickListener.onItemClick(position);
  67. }
  68. });
  69. holder.ll.setOnLongClickListener(new View.OnLongClickListener() {
  70. @Override
  71. public boolean onLongClick(View v) {
  72. mOnItemClickListener.onItemLongClick(position);
  73. return true;
  74. }
  75. });
  76. }
  77. // Return the size of your dataset (invoked by the layout manager)
  78. @Override
  79. public int getItemCount() {
  80. return mlist.size();
  81. }
  82. public void refreshData(List<HashMap<String, String>> list) {
  83. if (null != mlist) {
  84. mlist.clear();
  85. }
  86. mlist.addAll(list);
  87. notifyDataSetChanged();
  88. }
  89. public List<HashMap<String, String>> getAllList() {
  90. return mlist;
  91. }
  92. }