123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package com.Intelligent.FamilyU.adapter;
- import android.content.Context;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import com.Intelligent.FamilyU.R;
- import java.util.HashMap;
- import java.util.List;
- public class CpuAndMemoryListRecyclerViewAdapter extends RecyclerView.Adapter<CpuAndMemoryListRecyclerViewAdapter.ViewHolder> {
- private List<HashMap<String, String>> mlist;
- private Context context;
- private OnItemClickListener mOnItemClickListener;
- public static interface OnItemClickListener {
- void onItemClick(int position);
- void onItemLongClick(int position);
- }
- public CpuAndMemoryListRecyclerViewAdapter(Context context, List<HashMap<String, String>> list, OnItemClickListener onItemClickListener) {
- super();
- mlist = list;
- this.context = context;
- mOnItemClickListener = onItemClickListener;
- }
- public static class ViewHolder extends RecyclerView.ViewHolder {
- // each data item is just a string in this case
- public TextView title;
- public TextView cpuTv;
- public TextView memoryTv;
- public LinearLayout ll;
- public ViewHolder(View v) {
- super(v);
- }
- }
- // Create new views (invoked by the layout manager)
- @Override
- public CpuAndMemoryListRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
- int viewType) {
- // create a new view
- View convertView = LayoutInflater.from(context)
- .inflate(R.layout.listview_cpu_memory_listitem, parent, false);
- ViewHolder viewHolder = new ViewHolder(convertView);
- viewHolder.title = (TextView) convertView.findViewById(R.id.title);
- viewHolder.cpuTv = (TextView) convertView.findViewById(R.id.cpu_tv);
- viewHolder.memoryTv = convertView.findViewById(R.id.memory_tv);
- viewHolder.ll = (LinearLayout) convertView.findViewById(R.id.ll);
- return viewHolder;
- }
- // Replace the contents of a view (invoked by the layout manager)
- @Override
- public void onBindViewHolder(ViewHolder holder, final int position) {
- // - get element from your dataset at this position
- // - replace the contents of the view with that element
- HashMap<String, String> map = mlist.get(position);
- holder.title.setText(map.get("title"));
- String cpuStr = context.getResources().getString(R.string.home_cpu_use);
- String resultCpu = String.format(cpuStr, map.get("cpu"));
- holder.cpuTv.setText(resultCpu);
- String memoryStr = context.getResources().getString(R.string.home_memory_use);
- String result = String.format(memoryStr, map.get("memory"));
- holder.memoryTv.setText(result);
- holder.ll.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- mOnItemClickListener.onItemClick(position);
- }
- });
- holder.ll.setOnLongClickListener(new View.OnLongClickListener() {
- @Override
- public boolean onLongClick(View v) {
- mOnItemClickListener.onItemLongClick(position);
- return true;
- }
- });
- }
- // Return the size of your dataset (invoked by the layout manager)
- @Override
- public int getItemCount() {
- return mlist.size();
- }
- public void refreshData(List<HashMap<String, String>> list) {
- if (null != mlist) {
- mlist.clear();
- }
- mlist.addAll(list);
- notifyDataSetChanged();
- }
- public List<HashMap<String, String>> getAllList() {
- return mlist;
- }
- }
|