Reputation: 93
I had seen many examples regarding Hashmap
Data but I am not getting the data as required.
Here is my code:
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);
}
}
}
In above i am get the hash key value, all value is print in log cat but problem is repeated last key value in recycleview. need to add All hash value new textview
add the graph code which plot in recycler view also which hashkey
Upvotes: 0
Views: 2808
Reputation: 911
change your 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());
}
}
Upvotes: 2