Paula Perez
Paula Perez

Reputation: 119

is SQLiteDatabase syncronous or async?

I have an app that opens a new intent. The new intent inserts some rows on the datadabase, then finish(); it and come back to the first screen where the data should have refreshed with the new row. The code I have do not work on execution but works when debuging... so maybe there is some lag somewhere... maybe? how to wait for the query to finish?

Here is my code:

on the first screen:

setting the adapter and 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();
}

the getAllFilters method on the DB class

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;
}

any ideas?

Upvotes: 0

Views: 60

Answers (2)

Michael Dougan
Michael Dougan

Reputation: 1698

After you refresh your list, it should be enough to call notifyDataSetChanged(), however, I usually just reset the adapter:

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

Upvotes: 1

jspcal
jspcal

Reputation: 51904

since you're replacing the contents of the list, call adapter.notifyDataSetChanged() to refresh the entire list.

Upvotes: 0

Related Questions