Permalink
Browse files

done T07.03-GetAllTheData

  • Loading branch information...
GinaHsu committed Sep 27, 2017
1 parent d268dfc commit 7e2da25b5c1374430d31d00b86b5920e129468d3
@@ -12,16 +12,18 @@
private Context mContext;
// TODO (8) Add a new local variable mCount to store the count of items to be displayed in the recycler view
private int mCount;
/**
* Constructor using the context and the db cursor
*
* @param context the calling context/activity
*/
// TODO (9) Update the Adapter constructor to accept an integer for the count along with the context
public GuestListAdapter(Context context) {
public GuestListAdapter(Context context, int count) {
this.mContext = context;
// TODO (10) Set the local mCount to be equal to count
mCount = count;
}
@Override
@@ -41,7 +43,8 @@ public void onBindViewHolder(GuestViewHolder holder, int position) {
// TODO (11) Modify the getItemCount to return the mCount value rather than 0
@Override
public int getItemCount() {
return 0;
return mCount;
}
@@ -1,17 +1,26 @@
package com.example.android.waitlist;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.example.android.waitlist.data.TestUtil;
import com.example.android.waitlist.data.WaitlistContract;
import com.example.android.waitlist.data.WaitlistDbHelper;
public class MainActivity extends AppCompatActivity {
private GuestListAdapter mAdapter;
// TODO (1) Create a local field member of type SQLiteDatabase called mDb
private SQLiteDatabase mDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -27,17 +36,22 @@ protected void onCreate(Bundle savedInstanceState) {
waitlistRecyclerView.setLayoutManager(new LinearLayoutManager(this));
// Create an adapter for that cursor to display the data
mAdapter = new GuestListAdapter(this);
//mAdapter = new GuestListAdapter(this);
// TODO (2) Create a WaitlistDbHelper instance, pass "this" to the constructor as context
WaitlistDbHelper dbHelper = new WaitlistDbHelper(this);
// TODO (3) Get a writable database reference using getWritableDatabase and store it in mDb
mDb = dbHelper.getWritableDatabase();
// TODO (4) call insertFakeData from TestUtil and pass the database reference mDb
TestUtil.insertFakeData(mDb);
// TODO (7) Run the getAllGuests function and store the result in a Cursor variable
Cursor cursor = getAllGuests();
// TODO (12) Pass the resulting cursor count to the adapter
mAdapter = new GuestListAdapter(this, cursor.getCount());
// Link the adapter to the RecyclerView
waitlistRecyclerView.setAdapter(mAdapter);
@@ -54,7 +68,20 @@ public void addToWaitlist(View view) {
}
// TODO (5) Create a private method called getAllGuests that returns a cursor
private Cursor getAllGuests(){
// TODO (6) Inside, call query on mDb passing in the table name and projection String [] order by COLUMN_TIMESTAMP
return mDb.query(
WaitlistContract.WaitlistEntry.TABLE_NAME,
null,
null,
null,
null,
null,
WaitlistContract.WaitlistEntry.COLUMN_TIMESTAMP
);
}
// TODO (6) Inside, call query on mDb passing in the table name and projection String [] order by COLUMN_TIMESTAMP
}
@@ -41,6 +41,12 @@ public static void insertFakeData(SQLiteDatabase db){
cv.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, 45);
list.add(cv);
cv = new ContentValues();
cv.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME, "Gina");
cv.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, 40);
list.add(cv);
//insert all guests in one transaction
try
{

0 comments on commit 7e2da25

Please sign in to comment.