I couldn’t find what the problem is. Logcat is saying :
“Caused by: java.lang.ClassCastException: com.example.project.model.DataModel cannot be cast to java.util.ArrayList”
I tried different solutions but none worked:
- Parcelable
- @SuppressWarning
Could someone show me what is wrong in my code please?
DataModel.Java
public class DataModel implements Serializable { ArrayList<FeaturesModel> features; String title; public DataModel(ArrayList<FeaturesModel> featuresModels, String s) { features =featuresModels; title=s; } public ArrayList<FeaturesModel> getFeatures() { return features; } public void setFeatures(ArrayList<FeaturesModel> features) { this.features = features; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
TableActivity.Java
public class TableActivity extends AppCompatActivity { ArrayList<DataModel> dataModelList = new ArrayList<>(); EditText edtTableCount; Button btnCreateTable; TableAdapter adapter; RecyclerView recylerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_table); recylerView = findViewById(R.id.rvTables); btnCreateTable = findViewById(R.id.btnCreateTable); btnCreateTable.setOnClickListener(view -> { edtTableCount = findViewById(R.id.edtTableCount); String getTableCount = edtTableCount.getText().toString(); int tableCount = Integer.parseInt(getTableCount); for (int i = 0; i< tableCount; i++){ dataModelList.add(new DataModel(new ArrayList<>(),"Masa " + i)); } edtTableCount.setVisibility(GONE); btnCreateTable.setVisibility(GONE); recylerView.setVisibility(View.VISIBLE); }); setAdapter(); } public void setAdapter(){ CustomItemClickListener listener = (v, position) -> { Intent i = new Intent(TableActivity.this,DetailsActivity.class); i.putExtra("position",position); i.putExtra("dataModelList",dataModelList); startActivity(i); }; adapter = new TableAdapter(this,dataModelList,listener); recylerView.setAdapter(adapter); } }
DetailsActivity.Java
public class DetailsActivity extends AppCompatActivity { ArrayList<DataModel> dataModelList = new ArrayList<>(); DataModel dataModel; EditText edtDeviceInfo, edtBrand, edtModel, edtProcessor, edtRam, edtGraphicCard, edtOperationSystem, edtHardDisc, edtPrograms, edtDescription; Button btnAdd; int position = 0; String keyDeviceInfo = "keyDeviceInfo", keyBrand = "keyBrand", keyModel = "keyModel", keyProcessor = "keyModel", keyRam = "keyModel", keyGraphicCard = "keyModel", keyOperationSystem = "keyModel", keyHardDisc = "keyModel", keyPrograms = "keyModel", keyDescription = "keyModel" ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_details); edtDeviceInfo = findViewById(R.id.edtDeviceInfo); edtBrand = findViewById(R.id.edtBrand); edtModel = findViewById(R.id.edtModel); edtProcessor = findViewById(R.id.edtProcessor); edtRam = findViewById(R.id.edtRam); edtGraphicCard = findViewById(R.id.edtGraphicCard); edtOperationSystem = findViewById(R.id.edtOperationSystem); edtHardDisc = findViewById(R.id.edtHardDisc); edtPrograms = findViewById(R.id.edtPrograms); edtDescription = findViewById(R.id.edtDescription); btnAdd = findViewById(R.id.btnAdd); dataModelList = (DataModel) getIntent().getSerializableExtra("dataModelList"); position = getIntent().getIntExtra("position",0); dataModel = dataModelList.get(position); btnAdd.setOnClickListener(view -> { setInformations( edtDeviceInfo.getText().toString(), edtBrand.getText().toString(), edtModel.getText().toString(), edtProcessor.getText().toString(), edtRam.getText().toString(), edtGraphicCard.getText().toString(), edtOperationSystem.getText().toString(), edtHardDisc.getText().toString(), edtPrograms.getText().toString(), edtDescription.getText().toString() ); }); } public void setInformations(String deviceInfo, String brand, String model, String processor, String ram, String graphiccard, String operationsystem, String harddisc, String programs, String description){ edtDeviceInfo.setText(deviceInfo); edtBrand.setText(brand); edtModel.setText(model); edtProcessor.setText(processor); edtRam.setText(ram); edtGraphicCard.setText(graphiccard); edtOperationSystem.setText(operationsystem); edtHardDisc.setText(harddisc); edtPrograms.setText(programs); edtDescription.setText(description); } }
Advertisement
Answer
You are passing object on intent by doing this,
i.putExtra("dataModelList", dataModelList.get(position)) // dataModelList.get(position) here you are not fetching the list but rather getting single object from list based on position. So, it's object not ArrayList.
and on receiver side, you are expecting ArrayList
.
It should be like this
dataModel = (DataModel) getIntent().getSerializableExtra("dataModelList");