'java.lang.ClassCastException: com.github.mikephil.charting.data.Entry cannot be cast to com.github.mikephil.charting.data.PieEntry
Accepting data from database and displaying it on Table Layout and using that data creating a piechart with MpAndroidChart Library but getting the error as stated above
I am transfering arraylist from one activity to other using putextra and getSerializableExtra while accepting "the error is shown as above"
I am sharing my code please help
yValue is my arraylist where data is stored
transfering it to mylist using intent but this is generating error
My code:-
public class DisplayDate extends MainActivity{
public ArrayList<PieEntry> yValues = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_date);
doInBackground();
pieChart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent pieIntent = new Intent(DisplayDate.this,DisplayPiechart.class);
pieIntent.putExtra("Pie Values",yValues);
startActivity(pieIntent);
}
});
}
public void doInBackground()
{
ConnectionHelper connectString = new ConnectionHelper();
connect = connectString.connectionClass();
Log.w("Android","Connecting....");
if (connect == null)
{
Log.w("Android","Connection Failed");
ConnectionResult = "Check Your Internet Connection";
}
else
{
Log.w("Android","Connected");
String query = "Select No_,Name,Date,[Net Amount],[Gross Amount] from [CRONUS LS 1010 W1 Demo$Store] as str,[CRONUS LS 1010 W1 Demo$Transaction Header] as trans where str.[No_] = trans.[Store No_] and trans.[Date] between '"+Actual_Start+"' and '"+Actual_End+"' ";
try
{
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(query);
Log.w("Android","database Connected");
while (rs.next()) {
TR = new TableRow(this);
TV1.setText(rs.getString("No_"));
TV2.setText(rs.getString("Name"));
TV4.setText(rs.getString("Date"));
TV5.setText(rs.getString("Net Amount"));
TV3.setText(rs.getString("Gross Amount"));
TL.addView(TR);
c = TV3.getText().toString();
a = Float.parseFloat(c);
d = TV1.getText().toString();
yValues.add(new PieEntry(a,d));
}
ConnectionResult = "Successful";
isSuccess = true;
connect.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
class DisplayPiechart extends DisplayDate{
com.github.mikephil.charting.charts.PieChart pieChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_piechart);
Intent i = getIntent();
ArrayList<PieEntry> myList = (ArrayList<PieEntry>) i.getSerializableExtra("Pie Values");
//Error at below line
PieDataSet dataSet = new PieDataSet(myList, "Stores");
dataSet.setSliceSpace(2f);
dataSet.setSelectionShift(6f);
dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
PieData data = new PieData(dataSet);
data.setValueTextSize(10f);
data.setValueTextColor(Color.BLACK);
pieChart.setData(data);
}
}
Solution 1:[1]
This is a very old question but was running into the same issue. I'm new at this so take it with a grain of salt. There is probably a better way of solving it but posting it in case it helps someone.
I solved it by creating an ArrayList in the receiving activity as such:
ArrayList<Entry> receivedData;
receivedData = new ArrayList<>();
Then stored the data passed from the first activity into this ArrayList:
receivedData = getIntent().getParcelableArrayListExtra("data");
// Get size of ArrayList
int size = receivedData.size();
// Getting individual X and Y values from each ArrayList element
for(int k = 0; k < size; k++){
// Adding X and Y values as entries in a different
// ArrayList barEntriesArrayList
addDataPointsToGraph((int) receivedData.get(k).getX(), receivedData.get(k).getY());
}
And here is that Method:
private void addDataPointsToGraph(int x, float y){
barEntriesArrayList.add(new BarEntry(x, y));
}
I then supplied this ArrayList barEntriesArrayList to the dataSet
// creating a new bar data set.
barDataSet2 = new BarDataSet(barEntriesArrayList, getResources().getString(R.string.valuePerDay));
I was using these two tutorials as a guide when generating my graph
https://www.geeksforgeeks.org/how-to-create-a-barchart-in-android/
https://learntodroid.com/how-to-display-a-bar-chart-in-your-android-app/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | GreddyDC5 |