ListView不会更新数据

时间:2015-02-01 00:18:26

标签: android listview

我使用ListView来显示手机上SQLite数据库文件中的数据,当活动开始时,它会很好地显示数据,但是当我打开第二个活动时,数据必须根据我在该活动中执行的操作,为了实现此更改,我使用了onActivityResult(),当resultCode中的所有内容都正确时,它使用了RefreshList方法。 这是代码示例:

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

这些是我的全局变量:

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;

这是从AsyncTask扩展到后台工作的RefreshList代码,不会炸毁主UI线程:

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的方法效果很好。 请帮帮我。

2 个答案:

答案 0 :(得分:1)

首先,确保您的SQL查询返回数据 其次,您需要更新适配器的内容。通过致电clear()来做到这一点;然后添加ListaddAll();然后调用notifyDataSetChanged()更新ListView。

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

答案 1 :(得分:0)

更改mTarjetasCredito中的数据并调用adapter.notifyDataSetChanged()而不是设置新适配器