ListView控件不更新数据 [英] ListView doesn't update data

查看:129
本文介绍了ListView控件不更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个ListView来改变按照我的操作显示来自我的电话上的SQLite数据库文件,活动开始时,它显示的数据很好,但是当我打开第二个活动中,数据有数据做这项活动,来实现这种变化我用它有一个RefreshList方法时,一切都在结果code正确的方法的onActivityResult。
这里的code例如:

I'm using a ListView to show data from a SQLite database file on my phone, when the activity starts, it show the data well, but when I open a second activity, the data has to change according to the operation I do in that activity, to implement this change I used the onActivityResult method which have a RefreshList method when everything is correct in the resultCode. Here's the code example:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==RESULT_CORRECT ){
        if(resultCode==RESULT_OK){
            RefreshList();
        }
    }else{
        Toast.makeText(this,"Cancelled Operation",Toast.LENGTH_SHORT).show();
    }
}

这是我的全局变量:

These are my global variables:

private String LOG_TAG=Credito.class.getSimpleName();

private DatabaseManager DB;

private ListView lstCREDITO;
private List<TarjetasCredito> mTarjetasCredito=new ArrayList<>();

private static TarjetasCredito tarjetasCredito;

private ArrayAdapter<TarjetasCredito> mAdapter;

private static int RESULT_CORRECT=1;

这是RefreshList code,从一个AsyncTask的扩展做的工作的背景和不炸掉主UI线程:

This is the RefreshList code that extends from an AsyncTask to do the work in the background and dont blow up the main UI thread:

private void RefreshList() {
    GetDataAsync getDataAsync=new GetDataAsync();
    getDataAsync.execute((Void)null);
}

private class GetDataAsync extends AsyncTask<Void,Void,Boolean>{
    @Override
    protected Boolean doInBackground(Void... params) {
        try{
            Cursor cursor=null;
            DB.SQLSelect="SELECT COUNT(*) FROM "+TABLAS.vistas.visTarjetasCredito+"";
            cursor=DB.db.rawQuery(DB.SQLSelect,null);
            cursor.moveToFirst();
            int cuantos=cursor.getInt(0);
            if(cuantos!=0){
                mTarjetasCredito.clear();
                DB.SQLSelect="SELECT * FROM "+TABLAS.vistas.visTarjetasCredito+"";
                cursor=DB.db.rawQuery(DB.SQLSelect,null);
                cursor.moveToFirst();
                for(int i=0;i<cursor.getCount();i++){
                    mTarjetasCredito.add(new TarjetasCredito(cursor.getString(0),cursor.getInt(1),cursor.getDouble(2),cursor.getDouble(3),cursor.getInt(4),cursor.getInt(5),cursor.getString(6),cursor.getString(7)));
                    cursor.moveToNext();
                }
                DB.db.close();
                cursor.close();
                return true;
            }else{
                return false;
            }
        }catch (Exception ex){
            Log.e(LOG_TAG,ex.getMessage());
            return false;
        }
    }

    @Override
    protected void onPostExecute(Boolean success) {
        if(success){
            populateListView();
            creditoCallback();
        }else{
            Toast.makeText(getApplicationContext(),"No hay datos.",Toast.LENGTH_SHORT).show();
        }
    }
}

private void populateListView() {
    mAdapter=new MyListAdapter();
    lstCREDITO.setAdapter(null);
    lstCREDITO.setAdapter(mAdapter);
}

private class MyListAdapter extends ArrayAdapter<TarjetasCredito>{
    public MyListAdapter(){
        super(getApplicationContext(),R.layout.item_view_credito,mTarjetasCredito);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView=convertView;

        if(itemView==null){
            itemView=getLayoutInflater().inflate(R.layout.item_view_credito,parent,false);
        }

        TarjetasCredito tarjetasCredito=mTarjetasCredito.get(position);

        TextView numero=(TextView)itemView.findViewById(R.id.txtNUMEROTARJETA);
        numero.setText(tarjetasCredito.getNumeroTarjeta());

        TextView banco=(TextView)itemView.findViewById(R.id.txtBANCO);
        banco.setText(tarjetasCredito.getNombreBanco());

        TextView saldo=(TextView)itemView.findViewById(R.id.txtSALDO);
        saldo.setText(String.valueOf(tarjetasCredito.getSaldoDisponible()));

        return itemView;
    }
}

我不明白为什么它只有在活动启动工作,但不能当其他活动关闭,的onActivityResult和RefreshList的方法效果很好。
请帮我。

I don't understand why it only works when the activity is started but not when the other activity closes and the method of the onActivityResult and the RefreshList works well. Please help me.

推荐答案

首先,请确保您的SQL查询返回的数据。

其次,你需要更新你的适配器的内容。通过调用做到这一点清();然后添加列表的addAll();然后调用 notifyDataSetChanged()来更新ListView控件。

First, make sure that your SQL query is returning data.
Second, you need to update the content of your Adapter. Do this by calling clear(); then add the List through addAll(); then call notifyDataSetChanged() to update the ListView.

private void populateListView() {
    if(mAdapter==null) {
        mAdapter=new MyListAdapter();
        lstCREDITO.setAdapter(null);
        lstCREDITO.setAdapter(mAdapter);
    }
    else {
        mAdapter.clear();
        mAdapter.addAll(mTarjetasCredito);
        mAdapter.notifyDataSetChanged();
    }
}

这篇关于ListView控件不更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆