【问题标题】:DataModel cannot be cast to java.util.ArrayListDataModel 不能转换为 java.util.ArrayList
【发布时间】:2021-07-31 03:47:37
【问题描述】:

我找不到问题所在。 Logcat 说:

“引起:java.lang.ClassCastException: com.example.project.model.DataModel 不能转换为 java.util.ArrayList"

我尝试了不同的解决方案,但都没有奏效:

  • 可打包
  • @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);

    }

}

【问题讨论】:

  • 您正在向 Intent 添加单个 DataModel 项目,但试图在接收器 Activity 中获取 ArrayList&lt;DataModel&gt; - 错误告诉您这一点。只需转换为 (DataModel) 即可(无需额外添加位置)。注意 - 在您的问题中添加代码 sn-ps 通常是一种很好的做法,而不是指向代码屏幕截图的链接。
  • 您正在通过意图传递 dataModelList.get(position) -- 单个 DataModel 对象,并在接收者活动中检索 ArrayList&lt;DataModel&gt;;而不是DataModel。你需要什么数据?整个列表还是单个对象?基于此,将适当的值传递给意图。
  • 抱歉,我没看懂代码怎么写清楚。我试了几次,但代码搞砸了。让我解释一下我需要什么。我正在使用 recyclerview 在应用程序中创建表。之后,我做了一个活动来设置表格的详细信息。示例:表 0 -> cpu - gpu - os - 等。我想检查位置以找到我在桌子上的哪个位置。然后我会为每张桌子设置一些额外的功能。我会尝试将代码放入主评论中。

标签: java android


【解决方案1】:

您这样做是在传递对象的意图,

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

【讨论】:

    【解决方案2】:

    来自活动 从活动一或适配器传递数组

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

    【讨论】:

      猜你喜欢
      • 2020-12-20
      • 2023-03-29
      • 2013-07-11
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      • 2013-03-24
      相关资源
      最近更新 更多