Android スケルトン ListActivity
ナビゲーションに移動
検索に移動
目次
Android スケルトン ListActivity
Android | Android スケルトン |
- ListActivity を利用するスケルトン
- メニューとコンテキストメニューを利用
イメージ
メニュー
{{ref_image list_activity01.jpg}}
コンテキストメニュー
{{ref_image list_activity02.jpg}}
Tips
背景色の設定をしても、スクロールすると黒くなってしまう。
- ListView#scrollingChacheをfalseに設定する。
android:scrollingCache="false"
Activity
package info.typea.handrawroid; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.content.Context; import android.os.Bundle; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.ContextMenu.ContextMenuInfo; import android.widget.ArrayAdapter; import android.widget.TextView; /** * @author piroto */ public class NoteListActivity extends ListActivity { private static final int MENU_NEW = Menu.FIRST; private static final int MENU_CLOSE = Menu.FIRST + 1; private static final int MENU_PREFERENCES = Menu.FIRST + 2; private static final int MENU_EDIT = Menu.FIRST + 3; private static final int MENU_SHARE = Menu.FIRST + 4; private static final int MENU_DELETE = Menu.FIRST + 5; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // コンテキストメニューを登録 registerForContextMenu(getListView()); List<String> items = new ArrayList<String>(); // Dummy Data Create From for (int i=0; i<10; i++) { items.add("dummy item " + i); } // Dummy Data Create To NoteAdapter adapter = new NoteListActivity.NoteAdapter(this, R.layout.row_note, items); setListAdapter(adapter); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0,MENU_NEW,0,R.string.mnu_edit_new)
.setIcon(android.R.drawable.ic_menu_add);
menu.add(0,MENU_PREFERENCES,0,R.string.mnu_preferences)
.setIcon(android.R.drawable.ic_menu_preferences);
menu.add(0,MENU_CLOSE,0,R.string.mnu_close)
.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_NEW:
break;
case MENU_PREFERENCES:
break;
case MENU_CLOSE:
break;
default:
break;
}
return true;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, MENU_EDIT, 0, R.string.mnu_edit);
menu.add(0, MENU_SHARE, 0, R.string.mnu_share);
menu.add(0, MENU_DELETE, 0, R.string.mnu_delete);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case MENU_EDIT:
break;
case MENU_SHARE:
break;
case MENU_DELETE:
break;
default:
break;
}
return true;
}
/**
* @author piroto
*/
public static class NoteAdapter extends ArrayAdapter<String> {
private List<String> items;
public NoteAdapter(Context context, int textViewResourceId, List<String> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater lif = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = lif.inflate(R.layout.row_note, null);
}
String item = this.items.get(position);
if (item != null) {
TextView tv = (TextView)view.findViewById(R.id.txt_note_title);
tv.setText(item);
}
return view;
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/android:list" android:layout_height="wrap_content" android:layout_width="fill_parent"> </ListView> <TextView android:text="@string/msg_no_items" android:id="@+id/android:empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp"> </TextView> </LinearLayout>
row_note.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="" android:id="@+id/txt_note_title" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="24sp" > </TextView> </LinearLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, NoteListActivity!</string> <string name="app_name">Handrawroid</string> <string name="mnu_edit_new">新規作成</string> <string name="msg_no_items">アイテムがありません</string> <string name="mnu_preferences">設定</string> <string name="mnu_close">終了</string> <string name="mnu_edit">編集</string> <string name="mnu_share">共有</string> <string name="mnu_delete">削除</string> </resources>
© 2006 矢木浩人