【问题标题】:Trouble passing ArrayList to another Activity无法将 ArrayList 传递给另一个 Activity
【发布时间】:2016-01-14 15:46:35
【问题描述】:

晚上好,我无法将 ListView 和 Activity 传递给另一个。传递给另一个的 Activity 代码如下,称为 FiltrarImoveis.class :

for (int i = 0; i < jsonArray.length(); i++) {

                    Imovel imv = new Imovel();

                    JSONObject child = jsonArray.getJSONObject(i);

                    String finalidade=child.getString("finalidadeImovel");

                    String tipo = child.getString("tipoImovel");

                    String genero = child.getString("generoImovel");

                    String descricao = child.getString("descricaoImovel");

                    String responsavel = child.getString("responsavelImovel");

                    String telefoneResponsavel = child.getString("telefoneResponsavel");

                    String situacaoImovel = child.getString("situacaoImovel");

                    String valor = child.getString("valor");
                    imv.setFinalidade(finalidade);
                    imv.setTipo(tipo);
                    imv.setGenero(genero);
                    imv.setDescricao(descricao);
                    imv.setResponsavel(responsavel);
                    imv.setTelefoneResponsavel(telefoneResponsavel);
                    imv.setSituacao(situacaoImovel);
                    imv.setValor(valor);

                    listaImovel.add(imv);
                }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        //showParsedJSON.setText(output);
        carregar(listaImovel);

    }
}
}

public void carregar(ArrayList<Imovel> listaImovel){
    Intent intent = new Intent(this,DetalhesImoveis.class);
    intent.putExtra("lista",listaImovel);
    startActivity(intent);
}

从ListView继承的类如下,调用DetalhesImoveis.class:

private ListView lvImoveis;
ArrayList<Imovel> listaImoveis;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_detalhes_imoveis);

listaImoveis = (ArrayList<Imovel>) getIntent().getSerializableExtra("lista");
lvImoveis = (ListView)findViewById(R.id.lvImoveis);

try {
    ArrayAdapter<Imovel> adpImovel = new ArrayAdapter<Imovel>(DetalhesImoveis.this, android.R.layout.simple_list_item_1, listaImoveis);

    lvImoveis.setAdapter(adpImovel);
    //Log.v("LISTAiMOVEIS", "ELEMENTOS DA LISTA: " +listaImoveis.get(0).getDescricao().toString() );
}catch(Exception e){
    Log.v("logs","ERROR CAUSED BY THE EXCEPTION LIST: "+e.toString());
}

}

【问题讨论】:

    标签: android listview android-activity arraylist parameters


    【解决方案1】:

    我们不应该使用putExtra()for ArrayList 而不是使用putStringArrayListExtra() 例如:
    public void carregar(ArrayList<Imovel> listaImovel){ Intent intent = new Intent(this,DetalhesImoveis.class); intent.putStringArrayListExtra("lista",listaImovel);
    并在 DetalhesImoveis.class 中获取 Arraylist,例如:
    listaImoveis = getIntent().getStringArrayListExtra("lista");

    【讨论】:

    • 但使用 intent.putStringArrayListExtra ,它需要一个 String 类型的数组
    • 我不知道你为什么使用 &lt;Imovel&gt; ,你在 listaImovel 添加的值只是字符串。所以你可以将它作为字符串本身传递。
    • 我发现,错误在返回JSON对象的webservice中。
    【解决方案2】:

    你可以:

        getIntent().putParcelableArrayListExtra(listaImovel)
          Imovel need to implment Parcelable 
    

    你可以看看 Parcelable; 要么 : AActvt Put list int application 或 static var ; BActvt 从应用程序或静态变量中获取列表;

    【讨论】:

      猜你喜欢
      • 2016-07-01
      • 2017-01-25
      • 2018-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      相关资源
      最近更新 更多