2013-05-16 8 views
11

EDIT:ごめんなさい、私の質問から分かりません。私は新しいものを投稿します。ご迷惑をおかけして申し訳ありません。ドロアブルをプログラムでバックグラウンドとして設定する

Jsonファイルからリストビューを作成しています。 私のlistadapterでは、自分のリストの各行に適切なjsonデータを簡単に割り当てることができます。すなわち、例えば、テキストに適しています:上記のコードで

TextView tv = (TextView)ll.findViewById(R.id.descView); 
tv.setText(i.desc); 

、すべての行が正しく良いJSONデータによって移入されるであろう。

しかし、私はイメージのために同じことをすることはできません。私はこれを使用して、私のJSONデータから右の画像を設定することを試みた:

ImageView iv = (ImageView)ll.findViewById(R.id.imgView);   
iv.setBackgroundDrawable(context.getResources().getDrawable(i.img)); 

私は、私は私のパラメータの種類と間違って何かをやっていると思います:「setBackgroundDrawableは、」描画可能なパラメータが必要です。 "getDrawable"にはintが必要です。 フィールドimgの型をintに設定しましたが、これは機能しません。

理由は何ですか?

マイリストアダプタ:

public class adapter extends ArrayAdapter<ListItems> { 

int resource; 
String response; 
Context context; 

//Initialize adapter 
public ListItemsAdapter(Context context, int resource, List<ListItems> items) { 
    super(context, resource, items); 
    this.resource=resource; 
}  

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

    //Get the current object 
    ListItems i = getItem(position); 

    //Inflate the view 
    if(convertView==null) 
    { 
     ll = new LinearLayout(getContext()); 
     String inflater = Context.LAYOUT_INFLATER_SERVICE; 
     LayoutInflater li; 
     li = (LayoutInflater)getContext().getSystemService(inflater); 
     li.inflate(resource, ll, true); 
    } 
    else 
    { 
     ll = (LinearLayout) convertView; 
    } 

    //For the message 
    TextView tv = (TextView)ll.findViewById(R.id.descView); 
    tv.setText(i.desc); 

// For the Img 
    ImageView iv = (ImageView)ll.findViewById(R.id.imgView); 
    iv.setBackgroundDrawable(context.getResources().getDrawable(i.img)); 

    return ll; 
} 

私の項目クラス:

public class ListItems{ 
int id; 
int img;  
String desc;} 

そして、私のJSONファイルのサンプル:

[{"id":10001,"img":e1,"desc":"desc1"}, 
    {"id":10002,"img":e2,"desc":"desc2"}, 
    {"id":10003,"img":e3,"desc":"desc3"}] 
+0

おそらく、パラメータとしてintをとり、イメージビューのsrcを設定するsetImageResourcesを使用するとします。それでも問題はsetBackgroundDrawableには反映されません。また、あなたのコードはworlìkすることはできません。 llにはtextviewとimageviewのいずれも含まれていません – Blackbelt

+0

e1とは何ですか?あなたのjsonデータの画像はどこにありますか? – Oam

+0

あなたは "リソース"のレイアウトを投稿できます – Blackbelt

答えて

39

この

iv.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.img)); 
をお試しくださいそして、このようなチェックを行う

view.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable)); 

、あなたは16以下minSdkをtargatingしている場合:

または

iv.setBackgroundResource(R.drawable.img); 
+6

これはapi 16+でのみ動作することに注意してください。 – CorayThan

+4

はiv.setImageResource(R.drawable.img)を使用する必要があります。 – Brave

+0

@CorayThan以前のAPIバージョン(<= 15)でこれを達成するにはどうすればよいですか? – AnV

7

は今getDrawablesetBackgroundDrawableの両方を使用すると、このような背景に描画可能に設定する必要がありますdepricatedです:

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) { 
     view.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.your_drawable)); 
    } else { 
     view.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable)); 
    } 
0

ここで、新しいMetho d

recyclerView.setBackgroundResource(R.drawable.edit_text_button_shape);

これは古いメソッドです recyclerView.setBackgroundDrawable(this.getResources()。getDrawable(edit_text_button_shape));

関連する問題