SQLiteDatabase是同步还是异步?

时间:2019-05-28 16:19:28

标签: java android android-intent android-sqlite

我有一个打开新意图的应用程序。新的意图在数据库上插入一些行,然后完成();并返回到第一个屏幕,该屏幕上的数据应以新行刷新。我拥有的代码无法执行,但可以在调试时使用...所以也许某处有些滞后...也许?如何等待查询完成?

这是我的代码:

在第一个屏幕上:

设置适配器和onResume

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    v= inflater.inflate(R.layout.filters_fragment,container,false);

    createFragmentWidgets();


    myrecyclerview = (RecyclerView) v.findViewById(R.id.contact_recycleview);
    recyclerAdapter = new RecyclerViewAdapterFilters(getContext(), lstFilters);
    myrecyclerview.setLayoutManager(new LinearLayoutManager((getActivity())));
    myrecyclerview.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
    myrecyclerview.setAdapter(recyclerAdapter);

    return v;
}


@Override
public void onResume()
{  
// After a pause OR at startup
    super.onResume();

    lstFilters = mydb.getAllFilters("BL");
    recyclerAdapter.notifyDataSetChanged();
}

数据库类上的getAllFilters方法

public List<Filter> getAllFilters(String type) {
    List<Filter> lstFilter  = new ArrayList<>();

    //hp = new HashMap();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from " + FILTERS_TABLE_NAME+" where type='"+type+"'", null );
    res.moveToFirst();

    while(res.isAfterLast() == false){
        lstFilter.add(new Filter (res.getInt(res.getColumnIndex(FILTERS_COLUMN_ID)),
                res.getString(res.getColumnIndex(FILTERS_COLUMN_NAME)),
                res.getString(res.getColumnIndex(FILTERS_COLUMN_NUMBER)),
                res.getString(res.getColumnIndex(FILTERS_COLUMN_PHOTO)),
                res.getString(res.getColumnIndex(FILTERS_COLUMN_TYPE))));
        res.moveToNext();
    }

    res.close();
    db.close();

    return lstFilter;
}

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

刷新列表后,只需调用notifyDataSetChanged()就足够了,但是,我通常只重置适配器即可。

    adapter = new ClaimListAdapter(this, thisContext, claimItems);
    mClaimList.setAdapter(adapter);

答案 1 :(得分:0)

由于要替换列表的内容,因此请致电adapter.notifyDataSetChanged()以刷新整个列表。