| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「Android スケルトン ListActivity」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
(ページの作成:「==Android スケルトン ListActivity== [Android][Android スケルトン] *ListActivity を利用するスケルトン *メニューとコンテキストメニュー…」)
 
1行目: 1行目:
 
==Android スケルトン ListActivity==
 
==Android スケルトン ListActivity==
[Android][Android スケルトン]
+
[[Android][Android スケルトン]]
 
*ListActivity を利用するスケルトン
 
*ListActivity を利用するスケルトン
 
*メニューとコンテキストメニューを利用
 
*メニューとコンテキストメニューを利用
55行目: 55行目:
 
         registerForContextMenu(getListView());
 
         registerForContextMenu(getListView());
 
          
 
          
         List<String> items = new ArrayList<String>();
+
         List&lt;String&gt; items = new ArrayList&lt;String&gt;();
 
         // Dummy Data Create From
 
         // Dummy Data Create From
         for (int i=0; i<10; i++) {
+
         for (int i=0; i&lt;10; i++) {
 
             items.add("dummy item " + i);
 
             items.add("dummy item " + i);
 
         }
 
         }
127行目: 127行目:
 
       * @author piroto
 
       * @author piroto
 
       */
 
       */
     public static class NoteAdapter extends ArrayAdapter<String> {
+
     public static class NoteAdapter extends ArrayAdapter&lt;String&gt; {
         private List<String> items;
+
         private List&lt;String&gt; items;
 
   
 
   
         public NoteAdapter(Context context, int textViewResourceId, List<String> items) {
+
         public NoteAdapter(Context context, int textViewResourceId, List&lt;String&gt; items) {
 
             super(context, textViewResourceId, items);
 
             super(context, textViewResourceId, items);
 
             this.items = items;
 
             this.items = items;
152行目: 152行目:
 
===main.xml===
 
===main.xml===
  
  <?xml version="1.0" encoding="utf-8"?>
+
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
     android:orientation="vertical"
 
     android:orientation="vertical"
 
     android:layout_width="fill_parent"
 
     android:layout_width="fill_parent"
 
     android:layout_height="fill_parent"
 
     android:layout_height="fill_parent"
     >
+
     &gt;
     <ListView android:id="@+id/android:list"  
+
     &lt;ListView android:id="@+id/android:list"  
 
               android:layout_height="wrap_content"  
 
               android:layout_height="wrap_content"  
               android:layout_width="fill_parent">
+
               android:layout_width="fill_parent"&gt;
 
                
 
                
     </ListView>
+
     &lt;/ListView&gt;
     <TextView android:text="@string/msg_no_items"  
+
     &lt;TextView android:text="@string/msg_no_items"  
 
               android:id="@+id/android:empty"  
 
               android:id="@+id/android:empty"  
 
               android:layout_width="wrap_content"  
 
               android:layout_width="wrap_content"  
 
               android:layout_height="wrap_content"
 
               android:layout_height="wrap_content"
               android:textSize="20sp">
+
               android:textSize="20sp"&gt;
     </TextView>
+
     &lt;/TextView&gt;
  </LinearLayout>
+
  &lt;/LinearLayout&gt;
  
 
===row_note.xml===
 
===row_note.xml===
  
  <?xml version="1.0" encoding="utf-8"?>
+
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
+
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 
     android:layout_width="fill_parent"  
 
     android:layout_width="fill_parent"  
     android:layout_height="fill_parent">
+
     android:layout_height="fill_parent"&gt;
     <TextView android:text=""  
+
     &lt;TextView android:text=""  
 
               android:id="@+id/txt_note_title"  
 
               android:id="@+id/txt_note_title"  
 
               android:layout_width="fill_parent"  
 
               android:layout_width="fill_parent"  
 
               android:layout_height="fill_parent"  
 
               android:layout_height="fill_parent"  
 
               android:textSize="24sp"  
 
               android:textSize="24sp"  
               >
+
               &gt;
     </TextView>
+
     &lt;/TextView&gt;
  </LinearLayout>
+
  &lt;/LinearLayout&gt;
  
 
===string.xml===
 
===string.xml===
  
  <?xml version="1.0" encoding="utf-8"?>
+
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  <resources>
+
  &lt;resources&gt;
  <string name="hello">Hello World, NoteListActivity!</string>
+
  &lt;string name="hello"&gt;Hello World, NoteListActivity!&lt;/string&gt;
  <string name="app_name">Handrawroid</string>
+
  &lt;string name="app_name"&gt;Handrawroid&lt;/string&gt;
  <string name="mnu_edit_new">新規作成</string>
+
  &lt;string name="mnu_edit_new"&gt;新規作成&lt;/string&gt;
  <string name="msg_no_items">アイテムがありません</string>
+
  &lt;string name="msg_no_items"&gt;アイテムがありません&lt;/string&gt;
  <string name="mnu_preferences">設定</string>
+
  &lt;string name="mnu_preferences"&gt;設定&lt;/string&gt;
  <string name="mnu_close">終了</string>
+
  &lt;string name="mnu_close"&gt;終了&lt;/string&gt;
  <string name="mnu_edit">編集</string>
+
  &lt;string name="mnu_edit"&gt;編集&lt;/string&gt;
  <string name="mnu_share">共有</string>
+
  &lt;string name="mnu_share"&gt;共有&lt;/string&gt;
  <string name="mnu_delete">削除</string>
+
  &lt;string name="mnu_delete"&gt;削除&lt;/string&gt;
  </resources>
+
  &lt;/resources&gt;

2020年2月15日 (土) 08:00時点における版

Android スケルトン ListActivity

[[Android][Android スケルトン]]

  • ListActivity を利用するスケルトン
  • メニューとコンテキストメニューを利用

イメージ

メニュー

0116 list activity01.jpg

コンテキストメニュー

0117 list activity02.jpg

Tips

背景色の設定をしても、スクロールすると黒くなってしまう。

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>