2016-05-10 4 views
0

こんにちは私の質問のsolutiıonを見つけることができません。 私はdbからイメージの名前を取る。それから、image.setImageResourceに文字列を渡す必要があります。 getResources().getIdentifier( が含まれていますが、getResoruces()は機能しません。 エラー:(40,21)エラー:シンボルメソッドgetResources()を見つけることができません どうすればこの問題を解決できますか?私は、リストビューのアダプタクラスで名前の描画可能識別子を取得する

+0

貼り付けてくださいコンテキストへの参照を持っているので、あなたがこれを行うことができ

int resID = customView.getContext().getResources().getIdentifier(mDrawableName , "drawable", getPackageName()); 

を呼び出すことが容易である() –

+1

を ' getResources() 'が動作します。このメソッドにアクセスするには 'context'が必要です –

+0

コンテキストを追加するにはどうすればいいですか? – berkt

答えて

1

どちらの答えが正しいです仕事。

しかし、すべてのビューがどのように、どこがgetResourcesを呼び出している

public final Context getContext()

Returns the context the view is running in, through which it can access the current theme, resources, etc.

Returns Context The view's Context.

+0

私は知っているID:7614 java.lang.OutOfMemoryError:dalvik.system.VMRuntime.newNonMovableArray(ネイティブメソッド)でOOM まで、5184936フリーバイトと4MBの17640012バイト割り当てを割り当てられませんでした – berkt

+0

これは他の問題ですが、おそらくあなたの画像を読み込もうとすると大きいです –

+0

私はそれらを小さくすることによってその問題を処理していただきありがとうございます。より小さくするのではなく、解決策はありますか? – berkt

1

をこのコードを使用

public class ReceivedItemAdapter extends ArrayAdapter<ReceivedItem> { 

    public ReceivedItemAdapter(Context context, ArrayList<ReceivedItem> items){ 
     super(context,R.layout.row_item_received,items); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     View customView = LayoutInflater.from(this.getContext()) 
      .inflate(R.layout.row_item_received, parent, false); 

     ReceivedItem item = getItem(position); 
     TextView textName = (TextView) customView.findViewById(R.id.nameTextView); 
     TextView textCalorie = (TextView) customView.findViewById(R.id.calorieTextView); 
     ImageView imageView = (ImageView) customView.findViewById(R.id.imageView); 
     textName.setText(item.getName()); 
     textCalorie.setText(item.getCalorie()+ " cal"); 
     String mDrawableName = item.getImageName(); 
     int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName()); 
     imageView.setImageResource(resID); 
     customView.setTag(item.getId()); 
     return customView; 
    } 
} 

私たちはあなたを助けることができる前に、いくつかのコードを投稿してください。

外部のgetResourcesやアクティビティなどにアクセスする場合は、コンテキストへの参照を取得してからcontext.getResourcesを呼び出す必要があります。

だけcustomView.getContext()を呼び出します。getResources()

+0

私の質問を編集します。私はlistviewアダプタからアクセスしたいです。 – berkt

1

あなたは、アダプタ内のコンテキストインスタンスを維持する必要があり、その後、(context.getResourcesに呼び出す)

public class ReceivedItemAdapter extends ArrayAdapter<ReceivedItem> { 

private final Context context; 

public ReceivedItemAdapter(Context context, ArrayList<ReceivedItem> items){ 
    super(context,R.layout.row_item_received,items); 
    this. context = context; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 

    View customView = LayoutInflater.from(this.getContext()) 
      .inflate(R.layout.row_item_received, parent, false); 

    ReceivedItem item = getItem(position); 
    TextView textName = (TextView) customView.findViewById(R.id.nameTextView); 
    TextView textCalorie = (TextView) customView.findViewById(R.id.calorieTextView); 
    ImageView imageView = (ImageView) customView.findViewById(R.id.imageView); 
    textName.setText(item.getName()); 
    textCalorie.setText(item.getCalorie()+ " cal"); 
    String mDrawableName = item.getImageName(); 
    int resID = context.getResources().getIdentifier(mDrawableName , "drawable", getPackageName()); 
    imageView.setImageResource(resID); 
    customView.setTag(item.getId()); 
    return customView; 
} 
関連する問題