【问题标题】:ListView empty after Activity pauseActivity 暂停后 ListView 为空
【发布时间】:2014-11-27 01:42:00
【问题描述】:

所以我有这个活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lista_hoteles);

    listaHoteles = (ListView) findViewById(R.id.listaHoteles);
    hoteles = new Operaciones(getApplicationContext()).getHoteles("");

    new CargarHotels().execute();
}

有了这个异步任务:

class CargarHotels extends AsyncTask<Void, Void, Void> {

    protected void onPreExecute() {

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        try {
                hotAdapter = new HotelesAdapter(getApplicationContext(), hoteles);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {

                        listaHoteles.setAdapter(hotAdapter);

                    }
                });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

当我进入此活动时,他们一切正常,Listview 显示酒店列表和他的评级,但每当我按下主页按钮并转到另一个应用程序(即 Whatsapp、Facebook 等)时,他们就会返回我的 Activity 列表视图显示为空。

【问题讨论】:

    标签: android listview android-activity onresume


    【解决方案1】:

    嗯,一种解决方案是让您的onResume() 活动重新填充ListView。因此,只需像这样覆盖您的onResume()

    @Override
    public void onResume() {
        super.onResume();
        hoteles = new Operaciones(getApplicationContext()).getHoteles("");
        new CargarHotels().execute();
    }
    

    【讨论】:

    • 谢谢!我使用 onResume() 方法,仅此而已:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多