DataModel不能强制转换为java.util.ArrayList



我找不到问题是什么。Logcat显示:

" cause by: java.lang.ClassCastException:不能将datamodel强制转换为java.util.ArrayList"

我尝试了不同的解决方案,但都不起作用:

  • Parcelable
  • @SuppressWarning

有人能告诉我我的代码有什么问题吗?

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);
}
}

你这样做是为了传递对象,

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.

,在接收端,您期望ArrayList。应该是这样的

dataModel = (DataModel) getIntent().getSerializableExtra("dataModelList");

from activity从活动1或适配器传递数组

Intent i=new Intent(context, NewsActivity.class);
Bundle bundle =new Bundle();
bundle.putSerializable("ARRAYLIST",(Serializable)model);
bundle.putString("position", String.valueOf(position));
i.putExtra("BUNDLE",bundle);
context.startActivity(i);

和第二个活动接收像这样传递给第二个活动的数组。

Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");
flipdatalist = (ArrayList<FlipModel>) args.getSerializable("ARRAYLIST");
position= Integer.parseInt(args.getString("position"));

相关内容

最新更新