Skip to content

Instantly share code, notes, and snippets.

@keima
Created September 21, 2012 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keima/3763291 to your computer and use it in GitHub Desktop.
Save keima/3763291 to your computer and use it in GitHub Desktop.
public class HistoryActivity extends ExpandableListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Calendarクラス生成
Calendar currentTime = Calendar.getInstance();
// データベースからリスト生成のもととなるデータを抽出
DatabaseHelper dbHelper = new DatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
DatabaseDao databaseDao = new DatabaseDao(db);
List<Database> databaseList = databaseDao.findAll();
db.close();
// ExpandableListを作成。親と子のリストをつくり、データセット。
List<Map<String, String>> dateList = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> urlList = new ArrayList<List<Map<String, String>>>();
int[] stringRes = {
R.string.groupTitle_today,
R.string.groupTitle_yesterday,
R.string.groupTitle_within_a_week,
R.string.groupTitle_past,
};
// 「今日」「昨日」「1週間」「過去」で割り振る
for (int i = 0; i < 4; i++) {
Map<String, String> groupElement = new HashMap<String, String>();
groupElement.put("GROUP_TITLE", getString(stringRes[i]));
dateList.add(groupElement);
}
// ここあたり非常に汚い。つらい。。。
List<Map<String, String>> childElementsToday = new ArrayList<Map<String, String>>();
List<Map<String, String>> childElementsYesterday = new ArrayList<Map<String, String>>();
List<Map<String, String>> childElementsWithinWeek = new ArrayList<Map<String, String>>();
List<Map<String, String>> childElementsPast = new ArrayList<Map<String, String>>();
for (Database data : databaseList) {
Map<String, String> child = new HashMap<String, String>();
Uri uri = Uri.parse(data.getUrl());
child.put("CHILD_TITLE", uri.getLastPathSegment());
if (data.getIsSent() == 0) {
child.put("SUMMARY", getString(R.string.childSummary_unsent));
} else {
child.put("SUMMARY", getString(R.string.childSummary_sent));
}
// rowidをListに保持させておく
child.put("DATABASE_ROWID", String.valueOf(data.getRowid()));
if (getDiffDay(currentTime.getTimeInMillis() - data.getDate()) == 0) {
childElementsToday.add(child);
} else if (getDiffDay(currentTime.getTimeInMillis() - data.getDate()) == 1) {
childElementsYesterday.add(child);
} else if (getDiffDay(currentTime.getTimeInMillis() - data.getDate()) >= 2
&& getDiffDay(currentTime.getTimeInMillis() - data.getDate()) <= 6) {
childElementsWithinWeek.add(child);
} else {
childElementsPast.add(child);
}
}
urlList.add(childElementsToday);
urlList.add(childElementsYesterday);
urlList.add(childElementsWithinWeek);
urlList.add(childElementsPast);
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
// Context
this,
// Group(親)のリスト
dateList,
// Group(親)のレイアウト
android.R.layout.simple_expandable_list_item_1,
// Group(親)のリストで表示するMapのキー
new String[] {
"GROUP_TITLE"
},
// Group(親)のレイアウト内での文字を表示するTextViewのID
new int[] {
android.R.id.text1
},
// Child(子)のリスト
urlList,
// Child(子)のレイアウト
android.R.layout.simple_expandable_list_item_2,
// Child(子)のリストで表示するMapのキー
new String[] {
"CHILD_TITLE", "SUMMARY"
},
// Child(子)のレイアウト内での文字を表示するTextViewのID
new int[] {
android.R.id.text1, android.R.id.text2
}
);
setListAdapter(adapter);
getExpandableListView().setOnChildClickListener(this);
}
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
@SuppressWarnings("unchecked")
Map<String, String> child = (Map<String, String>) parent.getExpandableListAdapter()
.getChild(groupPosition, childPosition);
int rowid = Integer.valueOf(child.get("DATABASE_ROWID"));
Intent intent = new Intent(this, HistoryDetailActivity.class);
intent.putExtra("rowId", rowid);
startActivity(intent);
return super.onChildClick(parent, v, groupPosition, childPosition, id);
}
/**
* UNIX時間の差分から、何日離れているかを返す
*
* @param diff
*/
public int getDiffDay(long diff) {
diff /= 1000; // ミリ秒から秒へ
diff /= 60; // 秒から分へ
diff /= 60; // 分から時へ
diff /= 24; // 時から日へ
return (int) diff;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment