2015-10-16 49 views
-1

晚上好,我無法將ListView和Activity傳遞給另一個Activity。其傳遞給其他活動代碼是下面稱爲FiltrarImoveis.class:麻煩傳遞ArrayList到另一個活動

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

}

回答

0

我們不應該使用putExtra()作爲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");

+0

但intent.putStringArrayListExtra,它需要一個字符串數組字符串 –

+0

我不知道你爲什麼使用'',你在'listaImovel'中添加的值只是字符串。所以你可以將它作爲String傳遞。 – GIRIDHARAN

+0

我發現,錯誤是在webservice中返回JSON對象。 –

0

U可以:

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

ü可以看某物約Parcelable; 或: AActvt放置列表int應用程序或靜態var;從應用程序或靜態var獲取列表的BActvt;

相關問題