2012-05-06 15 views
1

私はListViewに2つのTextViewsを持っています。背景画像をTextViewsのいずれかに動的に設定しようとしています。私は各項目/行のカテゴリーに応じて表示したい約18種類の画像を持っています。ListView/rowからのTextViewの動的背景画像

private class MyListAdapter extends SimpleCursorAdapter { 

    public MyListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) { 
     super(context, layout , cursor, from, to); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 

     // Get the resource name and id 
     String resourceName = "R.drawable.abc" + cursor.getString(7); 
     int resid = context.getResources().getIdentifier(resourceName,"drawable",context.getPackageName()); 

     // Create the idno textview with background image 
     TextView idno = (TextView) view.findViewById(R.id.idno); 
     idno.setText(cursor.getString(3)); 
     idno.setBackgroundResource(resid); 

     // create the material textview 
     TextView materials = (TextView) view.findViewById(R.id.materials); 
     materials.setText(cursor.getString(1)); 
    } 
} 

私はデバッグにそれを実行すると、residは常にリソースが見つからなかったことを示している0を返します。画像がここに私のカスタムCursorAdapterためのコードがある"abc1""abc2"、などの名前が付けられresourceNameは正しく見えます。ID:"R.drawable.abc1"。画像ファイルをres/drawableフォルダにインポートしたところ、R.javaにリストされています。

これは正しいことですか、誰かがより良い解決策を持っていますか?

答えて

1

R.drawable.abc1のようにフルネームを使用しない場合は、drawableの後に名前のみを使用します。正確なStringnametypeおよびpackageから構築することは、getIdentifier()の仕事です。だから、getIdentifier()を使用する必要があります:getIdentifier()を実行するためにはるかに遅い方法であり、それはbindViewコールバックで呼び出されますので

String resourceName = "abc" + cursor.getString(7); 
int resid = context.getResources().getIdentifier(resourceName,"drawable",context.getPackageName()); 

また、あなたは、背景として画像を設定する別の方法をご覧くださいユーザーがListViewを上下にスクロールするときに何度も呼び出される可能性があります(ユーザーは非常に速いペースでこれを行うことができます)。

EDIT:あなたが使用することができます

一つの方法はgetIdentifierがより効率的にHashMap<Integer, Integer>名で表示される番号の間のマッピングにカスタムCursorAdapterと店舗内のIDを初期化することである(ABC 、ABC など)と実際のID:

private HashMap<Integer, Integer> setUpIdsMap() { 
    HashMap<Integer, Integer> mapIds = new HashMap<Integer, Integer>(); 
    // I don't know if you also have abc0, if not use this and substract 1 from the cursor value you get 
    for (int i = 0; i < 18; i++) { 
     String resourceName = "abc" + 0; 
     int resid =  context.getResources().getIdentifier(resourceName,"drawable",context.getPackageName()); 
     mapIds.put(i, resid); 
    } 

}

アダプタのコンストラクタで

:返信用

//... 
// Create the idno textview with background image 
     TextView idno = (TextView) view.findViewById(R.id.idno); 
     idno.setText(cursor.getString(3)); 
     idno.setBackgroundResource(ids.get(cursor.getString(7)));//depending if you have abc0 or not you may want to substract 1 from cursor.getString(7) to match the value from the setUpIdsMap method 
//... 
+0

ありがとう:

//...field in your adapter class HashMap<Integer, Integer> ids = new HashMap<Integer, Integer>(); //in the constructor: //... ids = setUpIdsMap(); //... 

次にbindView方法で返される配列を使用します!私は、 "drawable"を得るためにどのリソースを渡す必要があるので、 "R.drawable"はおそらく不要であることに気付きました。今、完璧な意味合いがあります! – wyoskibum

+0

私はハッシュマップを探検しますが、数字は必ず逐次的であるため、マップを手動で初期化する必要があります。各レコードのカテゴリは、abc1である1、またはabc11である1.1である可能性があります。もう一度ありがとうございます。 – wyoskibum