如何在回收视图中获取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);
            }
        }
    }

在上面我得到哈希键值,所有值都是在日志猫中打印 但是问题是回收视图中重复的最后一个键值。 需要添加所有哈希值新的textview

添加图形代码,在回收器视图中绘制了hashkey

更改您的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());
            }

        }

最新更新