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"}]
おそらく、パラメータとしてintをとり、イメージビューのsrcを設定するsetImageResourcesを使用するとします。それでも問題はsetBackgroundDrawableには反映されません。また、あなたのコードはworlìkすることはできません。 llにはtextviewとimageviewのいずれも含まれていません – Blackbelt
e1とは何ですか?あなたのjsonデータの画像はどこにありますか? – Oam
あなたは "リソース"のレイアウトを投稿できます – Blackbelt