另一个活动中的ArrayAdapter上下文出错

时间:2015-10-15 22:49:11

标签: java android android-arrayadapter android-context

事情是......在一个Activity中我填写一些字段来popularem一个ListView被加载到另一个Acitivity,试图在同一个Activity中显示它并且工作,当我试图在另一个中显示它时发生错误能力,我相信它与上下文有关。

用于填充参数的Activity,如果显示列表,则该函数称为FiltrarImoveis,其中此步骤仅在ArrayAdapter的上下文中。

提供问题的Activity被称为DetalhesImoveis,尝试传递此信息,DetalhesImoveis.this并继续给出错误...以下是给出错误的代码的Activity部分:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ArrayList<String> listaImoveis = new ArrayList<String>();
    listaImoveis.add("foo");
    listaImoveis.add("bar");
    ArrayAdapter<String> adpImovel = new ArrayAdapter<String>(DetalhesImoveis.this,android.R.layout.simple_list_item_1, listaImoveis);
    lvImoveis.setAdapter(adpImovel);
}

完整代码DetalhesImoveis.class:

package com.example.dhjeferson.testemenu;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;

public class DetalhesImoveis extends AppCompatActivity {

    private ListView lvImoveis;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<String> listaImoveis = new ArrayList<String>();
        /*setContentView(R.layout.activity_detalhes_imoveis);
        TextView showParsedJSON = (TextView) findViewById(R.id.json);

        Intent intent = getIntent();
        String output = intent.getStringExtra("output");
        showParsedJSON.setText(output);*/
        /*FiltrarImoveis filtrarImoveis = new FiltrarImoveis();
        listaImoveis = filtrarImoveis.retornaLista();*/
        lvImoveis = (ListView)findViewById(R.id.lvImoveis);

        Imovel imv = new Imovel();
        imv.setGenero("Genero");
        imv.setFinalidade("Finalidade");
        imv.setTipo("Tipo");
        imv.setValor("Valor");
        listaImoveis.add("foo");
        listaImoveis.add("bar");
        try {
            ArrayAdapter<String> adpImovel = new ArrayAdapter<String>(DetalhesImoveis.this, android.R.layout.simple_list_item_1, listaImoveis);

            lvImoveis.setAdapter(adpImovel);
        }catch(Exception e){
            Log.v("logs","ERROR CAUSED BY THE EXCEPTION LIST: "+e.getStackTrace().toString());
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_detalhes_imoveis, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

0 个答案:

没有答案