18

EDIT: Sorry I realise from your comment my question was not clear enough. I will post a new one. Sorry for this and thanks for your answers

I am populating a ListView from a Json file.

With my listadapter, I can easily assign appropriate json data to each row of my list. That works well for text, for example:

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

With the above code, every row will be correctly populated by the good json data.

However, I don't manage to do the same thing for an image. I have tried to set the right image from my json data using this:

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

I guess I am doing something wrong with the type of my parameters: "setBackgroundDrawable" requires a drawable parameter. "getDrawable" requires an int. I have set the type of my field img to int, but that doesn't work.

Any idea why?

My list adapter:

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;
}

my item class:

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

And a sample of my json file:

    [{"id":10001,"img":e1,"desc":"desc1"},
    {"id":10002,"img":e2,"desc":"desc2"},
    {"id":10003,"img":e3,"desc":"desc3"}]
4
  • probably you want to use setImageResources wich take an int as parameter and set the src of your imageview. Still the problem is not realted to setBackgroundDrawable. Also your code can not worlìk. ll does not contains either the textview and the imageview Commented May 16, 2013 at 13:46
  • What is e1 means? Where is the image in your json data? Commented May 16, 2013 at 13:47
  • can you post "resource" layout Commented May 16, 2013 at 13:47
  • are e1,e2,e3 the names of drawable images which you want to set? Commented May 16, 2013 at 13:50

4 Answers 4

57

Try this

iv.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.img));

or

iv.setBackgroundResource(R.drawable.img);
Sign up to request clarification or add additional context in comments.

3 Comments

should use iv.setImageResource(R.drawable.img);
@CorayThan How to achieve this in previous API versions(<= 15) ?
@CorayThan did you find a solution to old apis?
14

Now as getDrawable and setBackgroundDrawable both are depricated you should set drawable as Background like this :

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

and if you are targating minSdk below 16 then make a check like this :

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));
    }

Comments

2

If you want to change the ImageView image/src,

  1. In Layout android:src="@drawable/img" or app:srcCompat="@drawable/img"
  2. Programmatically imageview.setImageResource(R.drawable.img);

If you want to change the background of ImageView

  1. In Layout android:background="@drawable/img"
  2. programmatically selectimg.setBackgroundResource(R.drawable.circle_filled);

Here imageview is a ImageView and img is a drawable.

Comments

0

Here the new Method

recyclerView.setBackgroundResource(R.drawable.edit_text_button_shape);

don't use this it's an old method

recyclerView.setBackgroundDrawable(this.getResources().getDrawable(edit_text_button_shape));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.