如何在“回收”视图中获取Hashmap数据

时间:2017-12-02 06:49:50

标签: android android-recyclerview hashmap

我见过很多关于Hashmap数据的例子,但我没有按要求获取数据。

这是我的代码:

public class QuickGraphAdapter extends RecyclerView.Adapter<QuickGraphAdapter.GraphHolder> {

        private Context context;
        private HashMap<String, ArrayList<CoinPriceGraph>> GraphValue;
        private String TAG = QuickGraphAdapter.class.getSimpleName();
        public QuickGraphAdapter(DashBoardActivity dashBoardActivity, HashMap<String, ArrayList<CoinPriceGraph>> grStringArrayListHashMap) {
            this.context = dashBoardActivity;
            this.GraphValue = grStringArrayListHashMap;

        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public QuickGraphAdapter.GraphHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_list_quick_graph_view, parent, false);
            GraphHolder graphHolder = new GraphHolder(view);
            return graphHolder;
        }

        @Override
        public void onBindViewHolder(QuickGraphAdapter.GraphHolder holder, int position) {
            for (Map.Entry<String, ArrayList<CoinPriceGraph>> graph : GraphValue.entrySet()) {
               ArrayList<CoinPriceGraph> graphs = graph.getValue();
              Log.e(TAG, "Key Name: " + graph.getKey());
            holder.tvGraphName.setText(graph.getKey());


DataPoint[] dataPoint = new DataPoint[priceValue.size()];
        for (int i = 0; i < priceValue.size(); i++) {
            Log.e(TAG, "Price Value: " + priceValue.get(i).getCoinCCPrice());
            long unixSeconds = Long.parseLong(priceValue.get(i).getCoinCCTime());
            Date date = new Date(unixSeconds * 1000L); // *1000 is to convert seconds to milliseconds
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); // the format of your date
            String formattedDate = sdf.format(date);
            dataPoint[i] = new DataPoint(Double.parseDouble(formattedDate.replace(":", ".")), Double.parseDouble(priceValue.get(i).getCoinCCPrice()));
        }

        LineGraphSeries graphSeries = new LineGraphSeries<>(dataPoint);
        holder.gvCoinGraph.getViewport().setYAxisBoundsManual(true);
        graphSeries.setDrawDataPoints(true);
        graphSeries.setDataPointsRadius(6);
        graphSeries.setThickness(3);
        graphSeries.setDrawBackground(true);
        graphSeries.setColor(context.getResources().getColor(R.color.blue));
        graphSeries.setBackgroundColor(context.getResources().getColor(R.color.gry_color));
        holder.gvCoinGraph.addSeries(graphSeries);
        holder.gvCoinGraph.getViewport().setXAxisBoundsManual(true);
        holder.gvCoinGraph.getViewport().setScalable(true);
        holder.gvCoinGraph.getViewport().setScrollable(true);

            }


        }

        @Override
        public int getItemCount() {
            return GraphValue.size();
        }
        public class GraphHolder extends RecyclerView.ViewHolder {

            TextView tvGraphName;
            GraphView gvCoinGraph;

            public GraphHolder(View itemView) {
                super(itemView);
                tvGraphName = (TextView) itemView.findViewById(R.id.tv_quick_graph_name);
                gvCoinGraph = (GraphView) itemView.findViewById(R.id.gv_quick_graph);
            }
        }
    }
  

在上面我得到了哈希键值,所有值都是在log cat中打印的   但问题是在recycleview中重复最后一个键值。   需要添加所有哈希值new textview

添加在循环器视图中绘制的图形代码以及哪个hashkey

1 个答案:

答案 0 :(得分:0)

更改你的onBindViewHolder();

@Override
        public void onBindViewHolder(QuickGraphAdapter.GraphHolder holder, int position) {
            for (Map.Entry<String, ArrayList<CoinPriceGraph>> graph : GraphValue.entrySet()) {
               ArrayList<CoinPriceGraph> graphs = graph.getValue();
              Log.e(TAG, "Key Name: " + graph.getKey());
            holder.tvGraphName.setText(graph.getKey());
            }


        }
相关问题