【问题标题】:Issues with working with arraylists, listviews and intents使用数组列表、列表视图和意图的问题
【发布时间】:2018-05-10 05:11:26
【问题描述】:

我正在开发 android studio,它是一个在列表视图中显示某些车辆数据的应用程序,数据存储在数组列表中,并且属于名为 Vehicle 的类。

在列表视图上,想法是显示汽车的车牌、颜色等。它工作正常,我需要做的是,当您单击一个项目时,它会将您发送到另一个可以编辑的意图汽车的数据,这里我遇到了我的问题,我如何告诉那个意图它正在编辑谁的数据?

listaVehiculos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent editar = new Intent(Lista.this,Editar.class);
            //editar.putExtra("Placa",listaVehiculos.getItemAtPosition(i).toString());
            editar.putExtra("Codigo",listaVehiculos.getItemIdAtPosition(i));
            editar.putParcelableArrayListExtra("Vehiculos",Vehiculos);
            startActivity(editar);
        }
    });

这是我在 listview 所在视图上的代码,listavehiculos 是 listview 本身,Vehiculos 是 arraylist,我还需要发送到另一个活动是我选择了哪个 listview 项目,以便它知道哪个要编辑的数组列表的元素。

在其他活动中我有这个

editar = (Button)findViewById(R.id.editar);
    Bundle extras = getIntent().getExtras();
    final ArrayList<Vehiculo> Vehiculos3 = extras.getParcelableArrayList("Vehiculos");
    dos= extras.getString("Placa");

我一直在搞乱不同的 putExtra 值,但我不知道如何返回位置,或者告诉我选择了哪个对象。

【问题讨论】:

    标签: android listview android-intent arraylist


    【解决方案1】:

    Bundle

    当你使用putExtra时,你没有使用Bundle。所以你只是intent.getParcelableArrayListExtra("Vehiculos");intent.getStringExtra("Placa");来获得价值。

    并确保ArrayList&lt;? extends Parcelable&gt; value

    试试

     Intent intent = getIntent();
     final ArrayList<Vehiculo> Vehiculos3 = intent.getParcelableArrayListExtra("Vehiculos");
     dos= intent.getStringExtra("Placa");
    

    【讨论】:

      【解决方案2】:

      你不需要使用捆绑包

      改变,

       Bundle extras = getIntent().getExtras();
      final ArrayList<Vehiculo> Vehiculos3 = extras.getParcelableArrayList("Vehiculos");
      dos= extras.getString("Placa");
      

      Intent i = getIntent();
      

      final ArrayList<Vehiculo> Vehiculos3 = i.getParcelableArrayListExtra("Vehiculos"); dos= i.getStringExtra("Placa");

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多