简体   繁体   中英

Drawable not displaying in ImageView

Hello I am running into a silly error and I think I might be missing something. I currently have a list of objects. The objects have a title, description and an image. The title and description display properly, but for some reason the Image won't. I added the objects on another activity and made a list, when the user selects the item they wanted to see it send the id to this new page where it pulls the object. I added a random image as jpg to the application to see if it pulse and it does, but it looks a little weird. Any ideas why the images won't be pulling from the list? Thank you in advance.

XML:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/img"
    android:src="@drawable/img"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="false"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="News Title"
    android:id="@+id/title"
    android:layout_below="@+id/img"
    android:layout_alignParentLeft="false"
    android:layout_alignParentRight="false"
    android:layout_centerInParent="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/desc"
    android:layout_below="@+id/title"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" />
</RelativeLayout>

JAVA:

CustomOBJ selOBJ = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.det);
    Bundle objID = getIntent().getExtras();
    for(int x = 0; x < MainActivity.listobjs.size(); x++){
        if (MainActivity.listobjs.get(x).getID().equals(objID.getString("object") )){
            selOBJ = MainActivity.listobjs.get(x);
            break;
        }
    }

    TextView Title =(TextView)findViewById(R.id.title);
    TextView description = (TextView)findViewById(R.id.desc);
    ImageView objPic=(ImageView)findViewById(R.id.img);

    Title.setText(selOBJ.getName());
    description.setText(selOBJ.getDescription());
    objPic.setImageDrawable(selOBJ.getObjPic());

}

CustomOBJ Class:

public class CustomOBJ {
private String objID;
private String name;
private String Description;
private Drawable objPic;

public CustomOBJ(String i, String n, String d, Drawable pic) {
    objID = i;
    name = n;
    Description = d;
    objPic = pic;
}

public String getID() { return objID; }
public String getName() {return name;}
public String getDescription() {return description;}
public Drawable getObjPic() {return objPic; }

}

MainActivity:

public void jsonTolist(){
    //parse json data
    try{
        jArray = new JSONArray(welcome.result);
        for(int i=0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(i);
            Log.i("log_tag", "title:" + json_data.getString("title")

            );



            if(listobjs.isEmpty() || isNewsName(listobjs,json_data) == false){
                Drawable drawable = LoadImageFromWebOperations(json_data.getString("img"));

                selOBJ = new CustomOBJ(json_data.getString("id"), json_data.getString("title"), json_data.getString("desc"), drawable);
                listobjs.add(selOBJ);
            }


        }
    }catch(JSONException e){
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
}

Load Images:

private Drawable LoadImageFromWebOperations(String url){
    try{
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    }catch (Exception e) {
        System.out.println("Exc="+e);
        return null;
    }
}

Sender Side:

Bundle d1 = new Bundle();
d1.putInt("d",0);

Receiver Side:

Bundle objID = getIntent().getExtras();
int x = objID .getInt("d");     

if (MainActivity.listobjs.get(x).getID()
.equals(objID.getString("object") ))
{
        selOBJ = MainActivity.listobjs.get(x);
    }

Before doing this kindly check CustomOBJ class have a valid constructor or not.

ref this link also

How to get image from url website in imageview in android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM