基本的にデータベース情報をうまく表示するアプリをまとめています。
主な活動:
public class MainActivity extends FragmentActivity {
public static List<Entry> entryTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MySQLiteHelper db = new MySQLiteHelper(this);
entryTitles= db.getEntryTitles();
.....
}
....
}
ナビゲーション ドロワーのフラグメント
public class PositionsFrag extends Fragment {
public static EntryListAdaptor entryAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
int i = getArguments().getInt(ARG_ENTRY_NUMBER);
switch(i){
case 0:
entryAdapter = new EntryListAdapter(getActivity(), MainActivity.entryTitles);
lv.setAdapter(entryAdapter);
break;
.....
}
}
}
最後に、アクション バー ボタンの 1 つをクリックすると、新しいアクティビティが開き、ユーザーはフィールドを新しいエントリに追加できます。この新しいアクティビティ (創造的には NewActivity.java と呼ばれる) の「完了」アクション バー ボタンは、理論的には、新しいエントリ オブジェクトを作成し、この新しいオブジェクトをエントリ データベースに追加し、各エントリの最初の要素を文字列として新しいリストを作成し、リストが更新されたことを entryAdapter に通知します。
public class NewActivity extends Activity {
db = new MySQLiteHelper(this);
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_done:
db.newEntry(new Entry(title, date, location));
MainActivity.entryTitles= db.getEntryTitles();
PositionsFrag.entriesAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), db.printFirstTitle(), Toast.LENGTH_SHORT).show();
finish();
....
}
}
}
これを実行すると、トーストは最初のエントリのタイトル フィールドを正しく表示します (データベースが少なくとも新しいエントリを取得し、SQLite 読み取り関数を正常に記述したことを示します)。ただし、PositionFragment の ListView は変更されません。
これを機能させる方法に関するガイダンスや、より良い構造の提案をいただければ幸いです。
乾杯!