无法将ArrayList传递给另一个Activity

时间:2015-10-16 01:35:39

标签: android listview android-activity arraylist parameters

晚上好,我无法将ListView活动传递给另一个活动。传递给另一个的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());
}

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你可以:

    getIntent().putParcelableArrayListExtra(listaImovel)
      Imovel need to implment Parcelable 
你可以看看Parcelable; 要么 :     AActvt put list int application或static var;     BActvt从应用程序获取列表或静态var;

相关问题