概要
状態:試行錯誤中
閲覧数:1,287
投稿日:2013-10-09
更新日:2013-10-09
・読んでコード理解しようとしたけど挫折
・特に、「ArrayAdapterクラスの下り」が理解できない
・もしかしたらListviewを最小構成で動かすところからやってみないと駄目かもしれない
・面倒くさいけど、方針変換して「1から作成」してみるしかない
・特に、「ArrayAdapterクラスの下り」が理解できない
・もしかしたらListviewを最小構成で動かすところからやってみないと駄目かもしれない
・面倒くさいけど、方針変換して「1から作成」してみるしかない
新プロジェクト作成
アプリケーション名変更
アプリ名
変更内容
・DbDiary→日記
変更箇所
▼/DbDiary/res/values/strings.xml
<string name="app_name">日記</string>
参照箇所
▼/DbDiary/AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="android.style.system.ActivityTop"
android:label="@string/app_name" >
・エミュレーター再起動
・表示変更を確認
トップ画面一番上の日付
概要
・先ずは、「トップ画面一番上の日付」表示に挑戦
・具体的には、「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※どうやら順番を間違えたらしい。先に、データベース設定をしなければいけなかったようだ
現状確認
予想
・恐らく、デフォルトのデータベース名で、データベースが作成されているはず
確認作業
1.エミュレータ起動
2.「adb shell」コマンド
・予想通り、デフォルトのデータベース名でデータベースが作成されていたので、一旦rmでデータベース削除
データベース設定
概要
・データベース作成は、3つのデータベースクラスをコピペしたことで既に対応済
※1から作成しない理由は、この3つのクラスについては基本的に定例な処理と判断したため
データベース設定
データベース名変更
▼/DbDiary/src/android/style/systemdb/DatabaseOpenHelperItem.java
/**
* データベース処理クラス
*/
public class DatabaseOpenHelperItem extends SQLiteOpenHelper {
/* DataBase名称
* 定数
*/
private static final String DB_NAME_ITEM = "ANDROIDSTYLESYSTEM_ITEM";
テーブル名変更
▼/DbDiary/src/android/style/systemdb/RecordItem.java
/* テーブル名 */
public static final String TABLE_NAME_ITEM = "tbl_androidstylesystem";