Drawable不在ImageView中显示

时间:2016-01-25 19:09:47

标签: java android xml android-layout relativelayout

您好我遇到了一个愚蠢的错误,我想我可能会遗漏一些东西。我目前有一个对象列表。对象具有标题,描述和图像。标题和说明显示正确,但由于某种原因,图像不会。我在另一个活动上添加了对象并创建了一个列表,当用户选择他们想要看到的项时,它会将id发送到这个新页面,在那里它会拉动对象。我将一个随机图像作为jpg添加到应用程序中以查看它是否脉冲并且确实如此,但它看起来有点奇怪。任何想法为什么图像不会从列表中拉出来?提前谢谢。

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类:

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

加载图片:

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

1 个答案:

答案 0 :(得分:1)

发件人方:

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

接收方:

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

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

在执行此操作之前,请检查CustomOBJ类是否具有有效的构造函数。

参考此链接

How to get image from url website in imageview in android