MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Toolbar mToolBar;
private NavigationView mDrawer;
private ActionBarDrawerToggle mdrawerToggle;
private DrawerLayout mDrawerLayout;
private Button alarmButton;
private AlarmManager alarmManager;
private PendingIntent sender;
private RecyclerView listReminder;
private RemindersAdapter adapter;
List<ListInfo> data;
ListInfo infoData;
Button addReminderBtn;
MyDBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDrawer();
initView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mdrawerToggle.onConfigurationChanged(newConfig);
}
public void initDrawer(){
mToolBar = (Toolbar) findViewById(R.id.app_bar);
mDrawer = (NavigationView) findViewById(R.id.main_drawer);
setSupportActionBar(mToolBar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mdrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
mToolBar,
R.string.drawer_open,
R.string.drawer_close);
mDrawerLayout.setDrawerListener(mdrawerToggle);
// indicator based on whether the drawerlayout is in open or closed
mdrawerToggle.syncState();
}
public void initView(){
listReminder = (RecyclerView) findViewById(R.id.listData);
dbHandler = new MyDBHandler(this);
adapter = new RemindersAdapter(this, dbHandler.getAllData_a());
listReminder.setAdapter(adapter);
listReminder.setLayoutManager(new LinearLayoutManager(this));
addReminderBtn = (Button) findViewById(R.id.addBtn);
addReminderBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.addBtn:
Intent addReminder = new Intent(this, AddReminderActivity.class);
startActivity(addReminder);
break;
}
}
}
MyDBHandler.java
public class MyDBHandler extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 6;
private static final String DATABASE_NAME = "paroah.db";
public static final String TABLE_REMINDER = "reminders";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE_REMINDER = "title";
public static final String COLUMN_DESC_REMINDER = "desc";
public static final String COLUMN_DATE_REMINDER = "date_created";
public MyDBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = " CREATE TABLE "
+TABLE_REMINDER+ "(" +
COLUMN_ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"+
COLUMN_TITLE_REMINDER + " TEXT ,"+
COLUMN_DESC_REMINDER + " TEXT ,"+
COLUMN_DATE_REMINDER + " TEXT "+
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// db.execSQL("DROP TABLE IF EXIST " + TABLE_REMINDER); // onCreate(db);
Log.d("aoi", "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
try {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_REMINDER);
onCreate(db);
} catch (SQLException e) {
Log.d("aoi", "getting exception "
+ e.getLocalizedMessage().toString());
}
}
public void addReminder(ListInfo reminder ){
ContentValues values = new ContentValues();
values.put(COLUMN_TITLE_REMINDER, reminder.getTitle());
values.put(COLUMN_DESC_REMINDER, reminder.getDesc());
values.put(COLUMN_DATE_REMINDER, reminder.getDate());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_REMINDER, null, values);
db.close();
}
public void printDatabase(){
SQLiteDatabase db = getWritableDatabase();
}
public List<ListInfo> getAllData_a(){
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM "+TABLE_REMINDER;
Cursor cursor=db.rawQuery(query, null);
List<ListInfo> data=new ArrayList<>();
while (cursor.moveToNext()){
int _id=cursor.getInt(cursor.getColumnIndex(COLUMN_ID));
String title = cursor.getString(cursor.getColumnIndex(COLUMN_TITLE_REMINDER));
String desc = cursor.getString(cursor.getColumnIndex(COLUMN_DESC_REMINDER));
String date = cursor.getString(cursor.getColumnIndex(COLUMN_DATE_REMINDER));
ListInfo current = new ListInfo();
current.set_id(_id);
current.title = title;
current.desc = desc;
current.date = date;
data.add(current);
}
return data;
}
}
RemindersAdapter.java
public class RemindersAdapter extends RecyclerView.Adapter<RemindersAdapter.ItemViewHolder> {
private final LayoutInflater inflater;
List<ListInfo> data = Collections.emptyList();
public RemindersAdapter(Context context, List<ListInfo> data){
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.reminder_item, parent, false);
ItemViewHolder holder = new ItemViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
ListInfo current = data.get(position);
holder.title.setText(current.title);
}
@Override
public int getItemCount() {
return data.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder{
TextView title;
public ItemViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.reminderTitle);
}
}
}
有没有其他方法可以使这段代码干净?我需要的是概念和示例,如果你有,或者我需要学习如何使它干净。 TIA !!