I tried to present the ListActiviry code as a fragment for later addition to the NavigationDrawerActivity container. When adding to the container an error occurs. How to add such a fragment?
Fragment code:
public class EventsFragment extends ListFragment { static DatabaseHelper sqlHelper; static Cursor userCursor; static SimpleCursorAdapter userAdapter; static Context context; static int day; static int month; static ListView listView; static DatePicker date; public EventsFragment() { this.context = MainActivity.context; sqlHelper = new DatabaseHelper(context.getApplicationContext()); try { sqlHelper.createDataBase(); } catch (IOException e) { e.printStackTrace(); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View result = inflater.inflate(R.layout.fragment_events, container,false); date = (DatePicker)result.findViewById(R.id.date); listView = getListView(); return result; } public static void updater(){ day = date.getDayOfMonth(); month = date.getMonth(); try { sqlHelper.openDataBase(); Log.i("DB-E", "open-s " + sqlHelper.getReadableDatabase().toString()); userCursor = sqlHelper.getReadableDatabase().rawQuery("SELECT * FROM " + DatabaseHelper.TABLE + " WHERE " + DatabaseHelper.COLUMN_MONTH + "=" + month + " AND " + DatabaseHelper.COLUMN_DAY + "=" + day, null); Log.i("DB-E", "SELECT * FROM " + DatabaseHelper.TABLE + " WHERE " + DatabaseHelper.COLUMN_MONTH + "=" + month + " AND " + DatabaseHelper.COLUMN_DAY + "=" + day); String[] eventsTitle = new String[]{DatabaseHelper.COLUMN_TITLE}; userAdapter = new SimpleCursorAdapter(context, android.R.layout.simple_list_item_1, userCursor, eventsTitle, new int[]{android.R.id.text1}, 0); listView.setAdapter(userAdapter); } catch (Exception ex){} } protected int getDay(String date){ int r = 0; r=+getIntFromChar(date.charAt(0))*10; r+=getIntFromChar(date.charAt(1)); return r; } protected int getMonth(String date){ int r = 0; r=+getIntFromChar(date.charAt(3))*10; r+=getIntFromChar(date.charAt(4)); return r; } protected int getIntFromChar(char c){ switch (c){ case '1' : return 1; case '2' : return 2; case '3' : return 3; case '4' : return 4; case '5' : return 5; case '6' : return 6; case '7' : return 7; case '8' : return 8; case '9' : return 9; case '0' : return 0; } return -1; } }
The activity code from which the fragment is:
public class EventsActivity extends ListActivity { DatabaseHelper sqlHelper; Cursor userCursor; SimpleCursorAdapter userAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_events); Log.i("date", "" + DateFormat.getDateInstance(DateFormat.DATE_FIELD).format(new Date())); Log.i("date", ""+getDay(DateFormat.getDateInstance(DateFormat.DATE_FIELD).format(new Date()))); int month = getMonth(DateFormat.getDateInstance(DateFormat.DATE_FIELD).format(new Date())); int day = getDay(DateFormat.getDateInstance(DateFormat.DATE_FIELD).format(new Date())); ListView listView = getListView(); sqlHelper = new DatabaseHelper(getApplicationContext()); try { sqlHelper.createDataBase(); } catch (IOException e) { e.printStackTrace(); } try { sqlHelper.openDataBase(); Log.i("DB-E", "open-s " + sqlHelper.getReadableDatabase().toString()); userCursor = sqlHelper.getReadableDatabase().rawQuery("SELECT * FROM " + DatabaseHelper.TABLE + " WHERE " + DatabaseHelper.COLUMN_MONTH + "=" + month + " AND " + DatabaseHelper.COLUMN_DAY + "=" + day, null); Log.i("DB-E", "SELECT * FROM " + DatabaseHelper.TABLE + " WHERE " + DatabaseHelper.COLUMN_MONTH + "=" + month + " AND " + DatabaseHelper.COLUMN_DAY + "=" + day); String[] eventsTitle = new String[]{DatabaseHelper.COLUMN_TITLE}; userAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, userCursor, eventsTitle, new int[]{android.R.id.text1}, 0); listView.setAdapter(userAdapter); } catch (Exception ex){} } @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_events, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { Intent intent = new Intent(EventsActivity.this,SettingsActivity.class); startActivity(intent); return true; } return super.onOptionsItemSelected(item); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); } protected int getDay(String date){ int r = 0; r=+getIntFromChar(date.charAt(0))*10; r+=getIntFromChar(date.charAt(1)); return r; } protected int getMonth(String date){ int r = 0; r=+getIntFromChar(date.charAt(3))*10; r+=getIntFromChar(date.charAt(4)); return r; } protected int getIntFromChar(char c){ switch (c){ case '1' : return 1; case '2' : return 2; case '3' : return 3; case '4' : return 4; case '5' : return 5; case '6' : return 6; case '7' : return 7; case '8' : return 8; case '9' : return 9; case '0' : return 0; } return -1; } }
getActivity()
method or (better) from the argument of theonAttach()
method - Yuriy SPb ♦