カテゴリー:
日記アプリ
閲覧数:501 配信日:2013-10-10 09:14
概要
・先ずは、「トップ画面一番上の日付」表示に挑戦
・具体的には、「Last 2013/10」
レイアウト
レイアウト指定をコピペ
▼/DbDiary/res/layout/top.xml
<LinearLayout
android:id="@+id/LinearLayout_date"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:gravity="center"
android:background="#ffff66">
<TextView
android:id="@+id/TextView_last_month"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="@string/last_month"
android:textStyle="bold"
android:typeface="serif"
android:textSize="15dp"
android:textColor="#0000ff"
android:gravity="center"
android:background="#00ffffff"/>
<TextView
android:id="@+id/TextView_date"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:text="Year/Month"
android:textStyle="bold"
android:typeface="serif"
android:textSize="30dp"
android:textColor="#ff0000"
android:gravity="center"
android:background="#00ffffff"/>
<TextView
android:id="@+id/TextView_next_month"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="@string/next_month"
android:textStyle="bold"
android:typeface="serif"
android:textSize="15dp"
android:textColor="#0000ff"
android:gravity="center"
android:background="#00ffffff"/>
</LinearLayout
@string指定がないため、エラー発生
エラー: エラー: No resource found that matches the given name (at 'text' with value '@string/next_month').
@string指定
・「values-ja/strings.xml」を「res」以下へ配置
▼/DbDiary/res/values-ja/strings.xml
<string name="next_month">翌月</string>
<string name="last_month">先月</string>
この時点の表示
・表示がおかしいのは、カレンダーデータを取得できていないため
@2131034118 Year/Month
データ準備
▼/DbDiary/src/android/style/system/ActivityTop.java
・プロパティ宣言
public class ActivityTop extends Activity {
/* 操作したいItemを宣言 */
private TextView tv_last = null;
private TextView tv_date = null;
private TextView tv_next = null;
・onCreateメソッド内にて、どのItemを操作するのかリソースを割り当て
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.top);
/* どのItemを操作するのかリソースを割り当て */
tv_last = (TextView)findViewById(R.id.TextView_last_month);//先月
tv_date = (TextView)findViewById(R.id.TextView_date);//2013/9
tv_next = (TextView)findViewById(R.id.TextView_next_month);//翌月
}
onResumeメソッド
・アクティビティが前面に来るたびに一覧を更新する、onResumeメソッドを新規作成
・新規作成したクラスのインスタンスを生成し、実行
/* 一覧取得タスクの実行 */
DataLoadTask task = new DataLoadTask();/*作成したクラスのインスタンスを作る*/
task.execute();/*起動*/
バックグラウンド処理
・データ取得するために、バックグラウンドで走らせる処理を記述
・具体的には、「AsyncTaskクラス」を継承した「DataLoadTaskクラス」を新規作成
・引数解説はこちら
/**
* 一覧データの取得と表示を行うタスク
*/
public class DataLoadTask extends AsyncTask<Object, Integer, List<RecordItem>> {
}
・抽象クラス「AsyncTask」では、抽象メソッド「doInBackground」を宣言しているため、継承したクラスでは必ず実装しなければいけない
・doInBackgroundメソッド実装
/**
* 一覧データの取得と表示を行うタスク
*/
public class DataLoadTask extends AsyncTask<Object, Integer, List<RecordItem>> {
/*バックグラウンドで実行させたい処理*/
@Override
protected List<RecordItem> doInBackground(Object... params) {
}
}
エラーメッセージ
・RecordItemクラスがないことが原因
・新規パッケージ作成「android.style.systemdb」
・コピペ → ▼/DbDiary/src/android/style/systemdb/RecordItem.java
・「RecordItem」クラスをコピペ作成したので、後は読み込むだけ
import android.style.systemdb.RecordItem;
・エラーが消える
エラーメッセージ
RecordDaoItem を型に解決できません
・コピペ → ▼/DbDiary/src/android/style/systemdb/RecordDaoItem.javaimport android.style.systemdb.RecordDaoItem;
エラーメッセージ
DatabaseOpenHelperItem を型に解決できません
・コピペ → ▼/DbDiary/src/android/style/systemdb/DatabaseOpenHelperItem.java※どうやら順番を間違えたらしい。先に、データベース設定をしなければいけなかったようだ