Skip to content
Snippets Groups Projects
Commit f5d57115 authored by Vietanh Le Trung's avatar Vietanh Le Trung
Browse files

minden perzisztens

parent f62cc045
No related branches found
No related tags found
No related merge requests found
...@@ -51,14 +51,13 @@ public class MainActivity extends AppCompatActivity ...@@ -51,14 +51,13 @@ public class MainActivity extends AppCompatActivity
"subject-list" "subject-list"
).build(); ).build();
subjectRecyclerView = (RecyclerView) findViewById(R.id.SubjectRecyclerView); subjectRecyclerView = (RecyclerView) findViewById(R.id.SubjectRecyclerView);
// use this setting to improve performance if you know that changes // use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView // in content do not change the layout size of the RecyclerView
subjectRecyclerView.setHasFixedSize(true); subjectRecyclerView.setHasFixedSize(true);
loadItemsInBackground();
// use a linear layout manager // use a linear layout manager
subjectLayoutManager = new LinearLayoutManager(this); subjectLayoutManager = new LinearLayoutManager(this);
...@@ -69,7 +68,7 @@ public class MainActivity extends AppCompatActivity ...@@ -69,7 +68,7 @@ public class MainActivity extends AppCompatActivity
subjectAdapter = new SubjectAdapter(this); subjectAdapter = new SubjectAdapter(this);
subjectRecyclerView.setAdapter(subjectAdapter); subjectRecyclerView.setAdapter(subjectAdapter);
loadItemsInBackground();
} }
@Override @Override
......
package com.ireallydontcare.viet.posseidon; package com.ireallydontcare.viet.posseidon;
import android.app.DatePickerDialog; import android.app.DatePickerDialog;
import android.arch.persistence.room.Room;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
...@@ -12,12 +13,14 @@ import android.support.v7.widget.Toolbar; ...@@ -12,12 +13,14 @@ import android.support.v7.widget.Toolbar;
import android.view.View; import android.view.View;
import com.ireallydontcare.viet.posseidon.adapter.EventAdapter; import com.ireallydontcare.viet.posseidon.adapter.EventAdapter;
import com.ireallydontcare.viet.posseidon.data.EventDatabase;
import com.ireallydontcare.viet.posseidon.fragments.NewDialogEventFragment; import com.ireallydontcare.viet.posseidon.fragments.NewDialogEventFragment;
import com.ireallydontcare.viet.posseidon.fragments.NewDialogSubjectFragment; import com.ireallydontcare.viet.posseidon.fragments.NewDialogSubjectFragment;
import com.ireallydontcare.viet.posseidon.model.Event; import com.ireallydontcare.viet.posseidon.model.Event;
import com.ireallydontcare.viet.posseidon.model.Subject; import com.ireallydontcare.viet.posseidon.model.Subject;
import java.util.Calendar; import java.util.Calendar;
import java.util.List;
public class SubjectActivity extends AppCompatActivity public class SubjectActivity extends AppCompatActivity
implements NewDialogEventFragment.NewEventDialogListener { implements NewDialogEventFragment.NewEventDialogListener {
...@@ -29,6 +32,10 @@ public class SubjectActivity extends AppCompatActivity ...@@ -29,6 +32,10 @@ public class SubjectActivity extends AppCompatActivity
private RecyclerView eventRecyclerView; private RecyclerView eventRecyclerView;
private EventAdapter eventAdapter; private EventAdapter eventAdapter;
private RecyclerView.LayoutManager eventLayoutManager; private RecyclerView.LayoutManager eventLayoutManager;
private EventDatabase database;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
...@@ -49,6 +56,8 @@ public class SubjectActivity extends AppCompatActivity ...@@ -49,6 +56,8 @@ public class SubjectActivity extends AppCompatActivity
eventRecyclerView = (RecyclerView) findViewById(R.id.EventRecyclerView); eventRecyclerView = (RecyclerView) findViewById(R.id.EventRecyclerView);
eventRecyclerView.setHasFixedSize(true); eventRecyclerView.setHasFixedSize(true);
eventLayoutManager = new LinearLayoutManager(this); eventLayoutManager = new LinearLayoutManager(this);
...@@ -57,6 +66,13 @@ public class SubjectActivity extends AppCompatActivity ...@@ -57,6 +66,13 @@ public class SubjectActivity extends AppCompatActivity
eventAdapter = new EventAdapter(); eventAdapter = new EventAdapter();
eventRecyclerView.setAdapter(eventAdapter); eventRecyclerView.setAdapter(eventAdapter);
database = Room.databaseBuilder(
getApplicationContext(),
EventDatabase.class,
"event-list"
).build();
loadItemsInBackground();
} }
@Override @Override
...@@ -65,7 +81,7 @@ public class SubjectActivity extends AppCompatActivity ...@@ -65,7 +81,7 @@ public class SubjectActivity extends AppCompatActivity
@Override @Override
protected Event doInBackground(Void... voids) { protected Event doInBackground(Void... voids) {
//newItem.id = database.SubjectDao().insert(newItem); database.EventDao().insert(eventitem);
return eventitem; return eventitem;
} }
...@@ -75,4 +91,21 @@ public class SubjectActivity extends AppCompatActivity ...@@ -75,4 +91,21 @@ public class SubjectActivity extends AppCompatActivity
} }
}.execute(); }.execute();
}; };
private void loadItemsInBackground(){
new AsyncTask<Void, Void, List<Event>>() {
@Override
protected List<Event> doInBackground(Void... voids) {
List<Event> events = database.EventDao().getAllBySubject(subjectId);
return events;
}
@Override
protected void onPostExecute(List<Event> events) {
eventAdapter.update(events);
}
}.execute();
}
} }
...@@ -71,4 +71,10 @@ public class EventAdapter extends RecyclerView.Adapter<EventAdapter.EventViewHol ...@@ -71,4 +71,10 @@ public class EventAdapter extends RecyclerView.Adapter<EventAdapter.EventViewHol
events.add(Event); events.add(Event);
notifyItemInserted(events.size() - 1); notifyItemInserted(events.size() - 1);
} }
public void update(List<Event> events){
this.events.clear();
this.events.addAll(events);
notifyDataSetChanged();
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment