ラベル ListView の投稿を表示しています。 すべての投稿を表示
ラベル ListView の投稿を表示しています。 すべての投稿を表示

2013年7月23日火曜日

SimpleExpandableListAdapter

SimpleExpandableListAdapter

Adapter系のクラスを使うメリットは、データ群を一括して取り扱えることだ。
例えば、SimpleAdapterの場合、文字列群と画像群と、そしてそれら以外のデータ(例えば、自分で作成した任意のクラス等)をも、一括して、対応するView(例えば、TextViewやImageView等)に代入することができた。

(参考)SimpleAdapterの簡単すぎる基本形
    String sFrom[] = {"Bitmap", "Title"};
    int iTo[] = new int[]{
        R.id.Image_List_Favorite,
   R.id.Text_List_Favorite_Title
    };
      sa = new SimpleAdapter(mainActivity, al, R.layout.list_keyword, sFrom, iTo);
上記は、"Bitmap"と命名した画像データを、R.id.Image_List_FavoriteというImageViewに代入&表示する。

しかし、SimpleExpandableListAdapterの場合、一括して代入できるのは文字列データだけである。画像データはできないし、それ以外のデータもできない。仕様です。

画像データを、対応するImageViewに代入する場合、それ用のプログラムをしなければならない。
また、ChildViewに、例えばButtonを配置した場合、そのButtonに対する利用者からのクリック状況の把握の仕方もプログラムしなければならない。

サンプルプログラムは下記のとおりです。

    String sFrom[] = {"Title"};
    int iTo[] = new int[]{
        R.id.Text_List_Favorite_Title//自動的に代入できるのは、文字列データだけである。画像データを取り扱うことはできない。
    };
    private ExpandableListView lv;
    private MySA sa;
    private ArrayList<ArrayList<Map<String, Object>>> ChildrenList;
 
    /* 画像データを表示させ、かつ、ChildViewにButtonを実装させるため、
    SimpleExpandableListAdapterを拡張して使う。
    */
    class MySA extends SimpleExpandableListAdapter{
        /*
         * constructorは、Eclipseが自動生成したものをそのまま使う。
         * constructorは、ごちゃごちゃしているが、何も問題は無い。見ないでおこう。
         */
        public MySA(Context context,
                List<? extends Map<String, ?>> groupData, int groupLayout,
                String[] groupFrom, int[] groupTo,
                List<? extends List<? extends Map<String, ?>>> childData,
                int childLayout, String[] childFrom, int[] childTo) {
            super(context, groupData, groupLayout, groupFrom, groupTo, childData,
            childLayout, childFrom, childTo);
        }

    /*以下はプログラミングする箇所である。*/
        @Override
        public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

            View v = super.getChildView(groupPosition, childPosition, isLastChild,
                convertView, parent);
            /*
             * superによる処理が終わった後のViewを使うこと。
    super内で文字列の代入が行われているのである。
             * 以下は、ImageViewへ画像を代入する処理である。
    つまり、文字列データの処理を先にしてから画像データの処理を行う。
             */
            RelativeLayout rl = (RelativeLayout)v;//私のPGではRelativeLayoutである
            ImageView iv = (ImageView)rl.findViewById(R.id.Image_List_Favorite);
            Map<String, Object> m = ChildrenList.get(groupPosition).get(childPosition);
            Bitmap bm = (Bitmap)m.get("Bitmap");
            iv.setImageBitmap(bm);
            
            /*
             * 下記newChildView()においてButtonにリスナーを付けクリックイベントを捕捉できるようにする。しかし、それだけでは個々のButtonを識別できない。
このため、識別できるようにするため、次のようにタグを付ける。
             */
            Button btn = (Button)rl.findViewById(R.id.Button_List_Favorite);
            long l = ExpandableListView.getPackedPositionForChild(
                    groupPosition, childPosition);
            btn.setTag(l);
            return v;
        }

        /*
         * 次のnewChildView()は、上記getChildView()のsuper.getChildView()内において、
         * アプリ開始時にChildViewが新規に生成する時に呼ばれる。
         * 一定数のChildViewを生成し終えたら、もはや生成されることはない。
         * 生成後の処理は、getChildView()で行うことになる。
         * layout用xmlにおいてViewを書いてあるので、基本的にはnewChildView()をイジル必要は無い。
         */
        @Override
        public View newChildView(boolean isLastChild, ViewGroup parent) {
            View v = super.newChildView(isLastChild, parent);
            RelativeLayout rl = (RelativeLayout)v;//私のPGではRelativeLayoutである
            Button btn = (Button)rl.findViewById(R.id.Button_List_Favorite);
            btn.setOnClickListener(FavoriteView.this);
            return v;
        }
    }
    //以上が、SimpleExpandableListAdapterの拡張クラスでした。

    public 自分のメソッド(){
        
        lv = (ExpandableListView)****.findViewById(R.id.ListFavorite);
       
        ArrayList<Map<String, String>> parentList = new ArrayList<Map<String, String>>();
        ArrayList<String> als = Group用文字列データ群を取得する。
        for(String s : als){
            Map<String, String> m = new HashMap<String, String>();
            m.put("Parent_Text", s);
            parentList.add(m);
        }
        
        ChildrenList = Child用文字列データ群を取得する。
        
        sa = new MySA(
            ****,

            //Group用文字列データ群の代入
            parentList,//データそのもの
            android.R.layout.simple_expandable_list_item_1,
            new String[]{"Parent_Text"},//入力
            new int[]{android.R.id.text1},//出力

            //Child用文字列データ群の代入
            ChildrenList,//データそのもの
            R.layout.list_favorite,//layout用xmlファイル
            sFrom,//入力
            iTo//出力
        );
        
        lv.setAdapter(sa);
        lv.setOnChildClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.Button_List_Favorite){
        //サンプルなので、ログを表示するだけです。
        //任意のChild内に表示されたボタンが押されたことを取得します。
            Button btn = (Button)v;
            long l = (Long)btn.getTag();
            int iG = ExpandableListView.getPackedPositionGroup(l);
            int iC = ExpandableListView.getPackedPositionChild(l);
if(BuildConfig.DEBUG) Log.d(TAG, "onClick(). " + iG + ". " + iC);
            return;
        }
    }

    @Override
    public boolean onChildClick(ExpandableListView arg0, View v, int iG, int iC, long arg4) {
        //サンプルなので、ログを表示するだけです。
        //任意のChildが押されたことを取得します。
if(BuildConfig.DEBUG) Log.d(TAG, "onChildClick(). " + iG + ". " + iC);
        return false;
    }


以下は、ChildViewのlayout用xmlファイルであり、ファイル名はR.layout.list_favorite.xmlです。
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <Button
        android:id="@+id/Button_List_Favorite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:drawableLeft="@android:drawable/ic_menu_edit"
        android:focusable="false"
        >
    </Button>
    <ImageView
        android:id="@+id/Image_List_Favorite"
        android:contentDescription="@string/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        >
    </ImageView>
    <TextView
        android:id="@+id/Text_List_Favorite_Title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_toRightOf="@id/Image_List_Favorite"
        android:layout_toLeftOf="@id/Button_List_Favorite"
        android:layout_alignBottom="@id/Image_List_Favorite"
        android:focusable="false"
        >
    </TextView>
</RelativeLayout>

2013年4月15日月曜日

SimpleAdapter : レコード毎に色を変えて表示する

SimpleAdapter : レコード毎に色を変えて表示する

文字列だけで構成されるレコードを一覧にして表示させた場合、レコード間の区切りが見分けにくくなります。
その対処方法として、 レコード毎に色を変えて表示するとか、レコード間の幅を大きくするという方法があります。
javaのコードの概要は下記のとおりです。概要を示したものであるため、このママでは動作しません(見れば分かる)
色分けのフラグをレコード毎に設定し、そのフラグに基づいて背景色を変更すること、そして、背景色の設定にはアルファ値を適当な値で設定することがポイントです。アルファ値を0xffに設定した場合、利用者がそのレコードを選択しても「選択したことを示す背景色」が表示されません。

    ArrayList<Map<String, Object>> al;//List表示用データ
        al = new ArrayList<Map<String, Object>>();
        
            b = cs.moveToLast();
            int i = 0;
            while(b==true){
                String s;
                Map<String, Object> m;
                m = new HashMap<String, Object>();
                s = cs.getString(cs.getColumnIndex("Keyword"));
                m.put("Keyword", s);
                m.put("Background", i%2);//ゼロか1を代入する
                al.add(m);
                i++;
                b = cs.moveToPrevious();
            }
        }
        cs.close();
        
        String sFrom[] = {"Keyword", "Background"};
        int iTo[] = new int[]{ R.id.Keyword_Text, R.id.Layout_Keyword_Background };
        
        SimpleAdapter sa;
        SimpleAdapter.ViewBinder savb;
        savb = new SimpleAdapter.ViewBinder(){
            @Override
            public boolean setViewValue(View v, Object data, String s){
                if(v.getId()==R.id.Layout_Keyword_Background){
                    int i = (Integer)data;
                    if(i==1) v.setBackgroundColor(0x60ffdddd);
                    else v.setBackgroundColor(0x00ffffff);
                    return true;
                }
                return false;
            }
        };
        
        sa = new SimpleAdapter(mainActivity, al, R.layout.list_keyword, sFrom, iTo);
        sa.setViewBinder(savb);
        ListView lv;
        lv = (ListView)mainActivity.findViewById(R.id.ListKeyword);
        lv.setOnItemClickListener(this);
        lv.setAdapter(sa);
        lv.setDividerHeight(10);

list_keyword.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Layout_Keyword_Background"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    >
    
    <TextView
        android:id="@+id/Keyword_Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textIsSelectable="false"
        >
    </TextView>
</LinearLayout>

面白い動画を見つけました
メ~テレ ぬいぐるみのラパン

2013年1月15日火曜日

Localeをsortする

Localeをsortする

Stringをsortすることは、ArrayAdapterのsort()を使ってみるのとおりです。

今回はArrayAdapterを使ってLocaleをsortします。

toString()メソッドを実装しているObjectであればArrayAdapterで取り扱えます。幸いにして、Locale classはtoString()メソッドを実装しています。

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView lv;
        lv = new ListView(this);
        setContentView(lv);
        
        ArrayAdapter<Locale> aa;
        aa = new ArrayAdapter<Locale>(this, R.layout.activity_main, ls);
        aa.sort(new MyComparator());
        lv.setAdapter(aa);
    }
    
    public class MyComparator implements Comparator<Locale>{
        @Override
        public int compare(Locale l1, Locale l2){
            int iRet;
            iRet = l1.getLanguage().compareTo(l2.getLanguage());
            if(iRet==0){
                return l1.getCountry().compareTo(l2.getCountry());
            }
            else return iRet;
        }
    }

    Locale[] ls = {//ランダムに並んでいる
        Locale.CHINA,
        Locale.FRENCH,
        Locale.CANADA,
        Locale.JAPANESE,
        Locale.GERMANY,
        Locale.ENGLISH,
        Locale.CANADA,
        Locale.GERMAN,
        Locale.JAPAN,
        Locale.US,
        Locale.FRANCE,
        Locale.UK
    };
}

R.layout.activity_mainのxmlは次のとおりです。
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" 
    >
</TextView>

2012年6月28日木曜日

SimpleAdapter : データの変更を画面に反映させる。

SimpleAdapter : データの変更を画面に反映させる。

Activityの起動時に(onCreateメソッド内で)SimpleAdapterを使ってListViewに値を設定する方法は既に述べた。
参考:android.R.drawableのアイコンを一覧表示する


ListViewが表示された後で、データの内容が変更された場合における、その変更をListViewに反映させる方法は次のとおりである。
この例では、任意のレコードに表示されている文字列を、別の文字列に変更する。
public class SimpleAdapterActivity extends ListActivity {

    SimpleAdapter sa;
    ArrayList<Map<String, Object>> data;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        int i;
        String sFrom[] = {"Image", "String"};
        int iTo[] =  new int[] {R.id.Image, R.id.Text};
        data =new ArrayList<Map<String, Object>>();
        
        for(i=0; i<IconRawData.length; i++){
            Map<String, Object> m = new HashMap<String, Object>();
            m.put("Image", IconRawData[i++]);
            m.put("String", IconRawData[i]);
            data.add(m);
        }
        sa = new SimpleAdapter(this, data, R.layout.child, sFrom, iTo);
        setListAdapter(sa);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Map<String, Object> m;
        m = data.get(position);//クリックした位置にあるレコードを取得する。
        m.put("String", "This data has been modified.");//文字列を書き変える。
        data.set(position, m);//レコード内のデータを変更する。
        sa.notifyDataSetChanged();//変更を画面に反映させる。
    }


    public Object IconRawData[] = {
        android.R.drawable.ic_btn_speak_now,
        "ic_btn_speak_now",
        
        android.R.drawable.ic_delete,
        "ic_delete",
        
        android.R.drawable.ic_dialog_alert,
        "ic_dialog_alert",
        
        android.R.drawable.ic_dialog_dialer,
        "ic_dialog_dialer",
(以下省略)        

2012年3月14日水曜日

SimpleAdapterのsample code

SimpleAdapterのsample code

文字列を代入し、色を付けたTextViewを一覧にして表示するSimpleAdapterのsample codeです。

main.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="wrap_content"
    >
    <TextView
        android:id="@+id/TextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    </TextView>
</LinearLayout>


Javaのcodeは次のとおりです。

public class SimpleAdapterActivity extends ListActivity{
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        String sFrom[] = {"String", "Color"};
        int iTo[] = new int[]{R.id.TextView, R.id.TextView};
        ArrayList<Map<String, Object>> data =new ArrayList<Map<String, Object>>();
        SimpleAdapter sa;
        SimpleAdapter.ViewBinder savb;
        
        savb = new SimpleAdapter.ViewBinder(){
            @Override
            public boolean setViewValue(View view, Object data, String s){
                if(view.getId()==R.id.TextView){
                    TextView tv;
                    tv = (TextView)view;
                    if(data.getClass().equals(String.class)==true){
                        String sData;
                        
                        sData = (String)data;
                        tv.setText(sData);
                        return true;
                    }
                    if(data.getClass().equals(Integer.class)==true){
                        Integer iData;
                        
                        iData = (Integer)data;
                        tv.setBackgroundColor(iData.intValue());
                        return true;
                    }
                }
                return false;
            }
        };
        
        //適当にデータを作成する。
        Map<String, Object> m;
        m = new HashMap<String, Object>();
        m.put("String", "Red");
        m.put("Color", 0xffff0000);
        data.add(m);
        m = new HashMap<String, Object>();
        m.put("String", "Green");
        m.put("Color", 0xff00ff00);
        data.add(m);
        m = new HashMap<String, Object>();
        m.put("String", "Blue");
        m.put("Color", 0xff0000ff);
        data.add(m);
        
        sa = new SimpleAdapter(this, data, R.layout.main, sFrom, iTo);
        sa.setViewBinder(savb);
        setListAdapter(sa);
    }
}

2012年3月12日月曜日

複雑な形状のViewの一覧を作成する

複雑な形状のViewの一覧を作成する

ListViewを使えば複雑な形状のViewの一覧を作成できる。
参考:SimpleAdapterのsample code
しかし、この技法の欠点は、書き込み可能なView、例えば、EditTextを配置しても、そのEditTextに対して利用者が書き込みを行うことはできない、ということである。
ListViewのような作り込まれたものは、プログラミングが終わった後で、予期しない不都合が判明する。

素直に、ScrollView & LayoutInflaterを使って、地道に作成する。

ただし、この場合、EditTextにはIDを与えてはいけない、という点に留意しなければならない。
例えば、次のxmlのように、android:id="@+id/EditText"を設けてはいけない。削除しよう。
    <EditText
        android:id="@+id/EditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    </EditText>

仮に、IDを設けたとしても、初回起動時には、上手く表示される。しかし、configuration changeに伴う再起動が発生した後で表示されるEditTextには、全て同一の文字列が代入されてしまう。
この問題を避けるために、ID指定を書いてはいけないのである。
TextViewの場合はID指定をしても問題は発生しない。

main.xmlは次のとおりである。IDがParentListのLinearLayoutに、複数の子Viewを動的に追加する。
<?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"
    >
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <LinearLayout
            android:id="@+id/ParentList"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
        </LinearLayout>
    </ScrollView>>
</LinearLayout>

上記のxmlの中に、次のlistitem.xmlを動的に挿入する。各ViewにはIDを付けていない。
<?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="wrap_content"
    android:padding="2dp"
    android:background="#555"
    >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#000"
        >
        <CheckBox
            android:text="@string/Apply"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >
        </CheckBox>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >
            <TextView
                android:text="@string/Before"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            >
            </TextView>
            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                >
            </EditText>
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >
            <TextView
                android:text="@string/After"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            >
            </TextView>
           <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                >
            </EditText>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

上記2個のxmlを使って、複数のViewを動的に生成し、一覧を作成するJavaのcodeは次のとおりである。ViewのIDが存在しないため、個々のViewの識別はアドレス(相対位置と表現して良いかもしれない)で行う。
public class InflateListActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
        setContentView(R.layout.main);
        
        int i;
        ArrayList<String> al;
        BackupData bd;
        
        bd = (BackupData)getLastNonConfigurationInstance();
        if(bd==null){
            //サンプルプログラムなので適当にデータを代入する。
            al = new ArrayList<String>();
            al.add("true");
            al.add("Green");
            al.add("Blue");
            al.add("false");
            al.add("Green");
            al.add("Blue");
            al.add("true");
            al.add("Green");
            al.add("Blue");
            al.add("false");
            al.add("Red");
            al.add("Blue");
            al.add("true");
            al.add("Yellow");
            al.add("Red");
        }
        else{//configuration changeに伴う再起動
            al = bd.getBackupData();
        }
        
        for(i=0; i<al.size()/3; i++){
            LayoutInflater inf;
            LinearLayout llParent, ll, ll2, llChild;
            CheckBox cb;
            EditText et;
            String s;
            
            //親Viewを取得する。
            llParent = (LinearLayout)findViewById(R.id.ParentList);
            
            //挿入する子Viewを作成する。
            inf = getLayoutInflater();
            ll = (LinearLayout)inf.inflate(R.layout.listitem, null);
            
            ll2 = (LinearLayout)ll.getChildAt(0);
            
            //子Viewにデータを入れる。
            cb = (CheckBox)ll2.getChildAt(0);
            s = al.get(i*3);
            if(s.equals("true")==true) cb.setChecked(true);
            else cb.setChecked(false);
            
            llChild = (LinearLayout)ll2.getChildAt(1);
            et = (EditText)llChild.getChildAt(1);
            et.setText(al.get(i*3+1));
            
            llChild = (LinearLayout)ll2.getChildAt(2);
            et = (EditText)llChild.getChildAt(1);
            et.setText(al.get(i*3+2));
            
            //親Viewに子Viewを入れる。
            llParent.addView(ll);
        }
    }
    
    //configuration changeに伴う再起動への対応
    @Override
    public Object onRetainNonConfigurationInstance(){
        ArrayList<String> al;
        BackupData bd;
        LinearLayout llParent;
        int i, iSize;
        
        al = new ArrayList<String>();
        llParent = (LinearLayout)findViewById(R.id.ParentList);
        iSize = llParent.getChildCount();
        for(i=0; i<iSize; i++){
            LinearLayout ll, ll2, llChild;
            CheckBox cb;
            EditText et;
            String s;
            
            ll = (LinearLayout)llParent.getChildAt(i);
            ll2 = (LinearLayout)ll.getChildAt(0);
            
            cb = (CheckBox)ll2.getChildAt(0);
            if(cb.isChecked()==true) s = "true";
            else s = "false";
            al.add(s);
            
            llChild = (LinearLayout)ll2.getChildAt(1);
            et = (EditText)llChild.getChildAt(1);
            s = et.getText().toString();
            al.add(s);
            
            llChild = (LinearLayout)ll2.getChildAt(2);
            et = (EditText)llChild.getChildAt(1);
            s = et.getText().toString();
            al.add(s);
        }
        
        bd = new BackupData();
        bd.setBackupData(al);
        return bd;
    }
    
    private class BackupData{
        private ArrayList<String> dataBackup;
        void setBackupData(ArrayList<String> al){
            dataBackup = al;
        }
        ArrayList<String> getBackupData(){
            return dataBackup;
        }
    }
}



2011年11月9日水曜日

画像と文字列の一覧表を表示する

画像と文字列の一覧表を表示する

画像と文字列を含むViewを一覧にして表示する方法は、次に説明しました。
android.R.drawableのアイコンを一覧表示する
android.R.drawableに含まれる全ての画像を一覧表示させる
これらの方法は、画像の全てがresource IDとして取り扱える場合に使用できます。
SimpleAdapterは、デフォルトでは、resource IDの画像を取り扱えるのです。

しかし、事情により、画像が、resource IDとして取り扱うことができず、Drawableである場合、SimpleAdapterの振る舞いを修正しなければなりません。
この修正には、SimpleAdapter.ViewBinderを使います。

sample codeを下記のとおり掲載しておきます。共有により起動可能なアプリの一覧を、アプリの名称とアイコンを対にして表示します。
この例では、Drawable型の画像とresource IDによる画像を一覧表の中に混在させて表示します。
学習・実験を目的とするため、冗長に書いてあります。
参考:共有を事前に登録する

2011年11月14日 アイコンが重複して表示されるバグを修正しました。

Resources r;
Intent it;
PackageManager pm;
String sFrom[] = {"Image", "String"};
int iTo[] =  new int[] {R.id.Image, R.id.TextAppName};
final List<Map<String, Object>> data;
Map<String, Object> m;
SimpleAdapter sa;
SimpleAdapter.ViewBinder savb;

savb = new SimpleAdapter.ViewBinder(){
    @Override
    public boolean setViewValue(View view, Object data, String textRepresentation){
        if(view.getId()==R.id.Image){
            if(data instanceof Integer){
                Integer ig;
                ig = (Integer)data;
                view.setBackgroundResource(ig.intValue());
            }
            if(data instanceof Drawable){
                view.setBackgroundDrawable((Drawable)data);
            }
        }
        return false;
    }
};

r = getResources();
it = new Intent();
//起動するアプリを限定させて頂きます。
it.setAction(Intent.ACTION_SEND);
it.setType("text/plain");
pm = getPackageManager();
lri = pm.queryIntentActivities(it, PackageManager.MATCH_DEFAULT_ONLY);
Collections.sort(lri, new ResolveInfo.DisplayNameComparator(pm));

data =new ArrayList<Map<String, Object>>();
if(lri.size()==0){
    m = new HashMap<String, Object>();
    m.put("String", r.getString(R.string.noData));
    m.put("Image", 0);
    data.add(m);
}
else{
    int i;
    for(i=0; i<lri.size(); i++){
        m = new HashMap<String, Object>();
        m.put("String", lri.get(i).loadLabel(pm).toString());
        
        //Drawabledeで取り扱う。
        m.put("Image", lri.get(i).loadIcon(pm));
        
        //リソースで取得すると、表示できない。
        //m.put("Image", lri.get(i).getIconResource());
        data.add(m);
    }
    //最後に、resource IDによるアイコンを表示する。
    m = new HashMap<String, Object>();
    m.put("Image", android.R.drawable.ic_menu_revert);
    m.put("String", r.getString(R.string.noShare));
    data.add(m);
}

sa = new SimpleAdapter(this, data, R.layout.listitem, sFrom, iTo);
sa.setViewBinder(savb);
lv = (ListView)findViewById(R.id.ListView);
lv.setAdapter(sa);//一覧の表示を提供する。
lv.setOnItemClickListener(this);