Skip to content

Instantly share code, notes, and snippets.

@ryansgot
Last active May 12, 2018 09:21
Show Gist options
  • Save ryansgot/a8229078d72d18a1f54572e74bcbc4ac to your computer and use it in GitHub Desktop.
Save ryansgot/a8229078d72d18a1f54572e74bcbc4ac to your computer and use it in GitHub Desktop.
A Typical Loader-Style Forsure DB Usage
package com.fsryan.forsuredb.example;
import android.app.LoaderManager;
import com.fsryan.forsuredb.cursor.FSCursor;
import com.fsryan.forsuredb.cursor.FSCursorLoader;
public class MyActivity extends AppCompatActivity {
MyRecyclerViewAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new MyRecyclerViewAdapter();
getLoaderManager().initLoader(111, null, new LoaderManager.LoaderCallbacks<FSCursor> {
@Override
public Loader<FSCursor> onCreateLoader(int id, Bundle args) {
return new FSCursorLoader<>(MyActivity.this, ForSure.employeesTable()
.joinPositionsTable(FSJoin.Type.INNER)
.then()
.order().byLastName(OrderBy.ORDER_ASC)
.then()); // <-- puts the method call chain back in the employees table Resolver context
}
@Override
public void onLoadFinished(Loader<FSCursor> loader, FSCursor data) {
adapter.changeCursor(data);
}
@Override
public void onLoaderReset(Loader<FSCursor> loader) {
//
}
});
}
}
package com.fsryan.forsuredb.example;
import com.fsryan.forsuredb.cursor.BaseFSCursorRecyclerAdapter;
import com.fsryan.forsuredb.cursor.FSCursor;
import com.fsryan.forsuredb.cursor.FSCursorViewHolder;
public class MyRecyclerViewAdapter extends BaseFSCursorRecyclerAdapter<MyRecyclerViewAdapter.ViewHolder> {
static final EmployeesTable employeesApi = ForSure.employeesTable().getApi();
static final PositionsTable positionsApi = ForSure.positionsTable().getApi();
public MyRecyclerViewAdapter() {
super(true); // <-- tells super class that item ids can be provided independent of item position
}
@Override
protected UserTable api() { // <-- the api that can provide item ids
return employeesApi;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.employee_position_layout, parent, false);
return new ViewHolder(v, viewType);
}
static class ViewHolder extends FSCursorViewHolder {
TextView fNameText;
TextView lNameText;
TextView positionText;
ViewHolder(View itemView, int viewType) {
super(itemView, viewType);
}
@Override
protected void initViewReferences(View targetLayout) {
fNameText = (TextView) targetLayout.findViewById(R.id.fNameText);
lNameText = (TextView) targetLayout.findViewById(R.id.lNameText);
positionText = (TextView) targetLayout.findViewById(R.id.positionText);
}
@Override
protected void populateView(FSCursor cursor) {
fNameText.setText(employeesApi.firstName(cursor));
lNameText.setText(employeesApi.lastName(cursor));
positionText.setText(positionsApi.title(cursor));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment