OSDN Git Service

4dfdc6072faa3f6e74f9a3f1fceb9eae5b11c8fd
[evermemo/source.git] / workspace / EverMemo / src / com / yuji / em / NoteListActivity.java
1 package com.yuji.em;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.app.Activity;
7 import android.content.Intent;
8 import android.os.Bundle;
9 import android.text.format.DateUtils;
10 import android.view.View;
11 import android.view.View.OnClickListener;
12 import android.widget.AdapterView;
13 import android.widget.AdapterView.OnItemClickListener;
14 import android.widget.Button;
15 import android.widget.ListView;
16 import android.widget.TextView;
17
18 import com.evernote.edam.notestore.NoteList;
19 import com.yuji.em.common.Constant;
20 import com.yuji.em.data.Note;
21 import com.yuji.em.data.NoteDao;
22 import com.yuji.em.task.NoteListTask;
23 import com.yuji.em.utility.AsyncTaskCommand;
24
25 public class NoteListActivity extends Activity {
26         private ListView listView = null;
27         private Button closeButton = null;
28         private int index = -1;
29
30         @Override
31         public void onCreate(Bundle savedInstanceState) {
32                 super.onCreate(savedInstanceState);
33                 setContentView(R.layout.note_list);
34
35                 try {
36                         listView = (ListView) this.findViewById(R.id.noteListView);
37                         closeButton = (Button) this.findViewById(R.id.noteListCloseButton);
38
39                         listView.setOnItemClickListener(new OnItemClickListener() {
40                                 public void onItemClick(AdapterView<?> parent, View view,
41                             int position, long id) {
42                                         listViewOnItemClickListener(parent, view, position, id);
43                                 }
44                         });
45
46                         closeButton.setOnClickListener(new OnClickListener(){
47                                 public void onClick(View v) {
48                                         closeButtonOnClick();
49                                 }});
50
51                         Bundle extras = getIntent().getExtras();
52                         index  = extras.getInt(Constant.EXTRA_INDEX);
53                         
54                         NoteListTask task = new NoteListTask(this);
55                         AsyncTaskCommand command = new AsyncTaskCommand(this, task);
56                         command.execute("");
57                 } catch (Exception e) {
58                         // TODO
59                         e.printStackTrace();
60                 }
61         }
62         
63         private void closeButtonOnClick(){
64                 Intent intent = new Intent();
65                 setResult(RESULT_CANCELED, intent); 
66                 finish();               
67         }
68
69         private void listViewOnItemClickListener(AdapterView<?> parent, View view,
70             int position, long id) {
71         ListView listView = (ListView) parent;
72         NoteListItem item = (NoteListItem) listView.getItemAtPosition(position);
73
74         Note note = item.getNote();
75                 if (note == null) {
76                         return;
77                 }
78
79                 NoteDao dao = NoteDao.getInstance();
80                 dao.update(this, note, index);
81                 
82                 Intent intent = new Intent();
83                 setResult(RESULT_OK, intent); 
84                 finish();
85         }
86
87 //      private void createList() {
88 //              NoteList noteList = util.getNoteList();
89 //              List<NoteListItem> list = new ArrayList<NoteListItem>();
90 //
91 //              List<Note> notes = noteList.getNotes();
92 //              String oldTitle = "";
93 //              NoteListItem item;
94 //              for (Note note : notes) {
95 //                      // long l = note.getUpdated();
96 //                      long l = note.getCreated();
97 //                      String title = DateUtils
98 //                                      .formatDateTime(this, l, DateUtils.FORMAT_SHOW_YEAR
99 //                                                      | DateUtils.FORMAT_SHOW_DATE/*
100 //                                                                                                               * | DateUtils.
101 //                                                                                                               * FORMAT_SHOW_TIME
102 //                                                                                                               */);
103 //                      if (!title.equals(oldTitle)) {
104 //                              item = new NoteListItem(title, null);
105 //                              list.add(item);
106 //                              oldTitle = title;
107 //                      }
108 //
109 //                      item = new NoteListItem(null, note);
110 //                      list.add(item);
111 //              }
112 //
113 //              NoteListViewArrayAdapter adapter = new NoteListViewArrayAdapter(this,
114 //                              R.layout.simple_list_item_1, list);
115 //              listView.setAdapter(adapter);
116 //      }
117
118         public void done(NoteList noteList) {
119                 if (noteList == null){
120                         return;
121                 }
122                 
123                 List<NoteListItem> list = new ArrayList<NoteListItem>();
124
125                 // TODO
126                 List<com.evernote.edam.type.Note> notes = noteList.getNotes();
127                 String oldTitle = "";
128                 NoteListItem item;
129                 for (com.evernote.edam.type.Note note : notes) {
130                         long l = note.getUpdated();
131                         //long l = note.getCreated(); // TODO \83\\81[\83g\8f\87\82Í\81A\8dX\90V\93ú?
132                         String title = DateUtils
133                                         .formatDateTime(this, l, DateUtils.FORMAT_SHOW_YEAR
134                                                         | DateUtils.FORMAT_SHOW_DATE);
135                         if (!title.equals(oldTitle)) {
136                                 item = new NoteListItem(title, null);
137                                 list.add(item);
138                                 oldTitle = title;
139                         }
140
141                         Note n = new Note(note.getGuid(), note.getTitle());
142                         item = new NoteListItem(null, n);
143                         list.add(item);
144                 }
145
146                 NoteListViewArrayAdapter adapter = new NoteListViewArrayAdapter(this,
147                                 R.layout.simple_list_item_1, list);
148                 listView.setAdapter(adapter);           
149         }
150 }