Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据真/假条件从URL绘制位图_Java_Android - Fatal编程技术网

Java 根据真/假条件从URL绘制位图

Java 根据真/假条件从URL绘制位图,java,android,Java,Android,使用下面的代码,我只想在chatMessageObj.left为真时显示图像,但图像总是显示出来 public View getView(int position, View convertView, ViewGroup parent) { ... String personPhotoUrl = "Image url"; personPhotoUrl = personPhotoUrl.substring(0, personPhotoUrl.length() - 2) + PROFILE_PI

使用下面的代码,我只想在
chatMessageObj.left
为真时显示图像,但图像总是显示出来

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

...

String personPhotoUrl = "Image url";
personPhotoUrl = personPhotoUrl.substring(0, personPhotoUrl.length() - 2) + PROFILE_PIC_SIZE;
    if (chatMessageObj.left) {
        new LoadProfileImage(chatImage).execute(personPhotoUrl);
    }

...

}

private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public LoadProfileImage(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }
    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

谢谢。

这是因为listview总是保持刷新,所以您需要做的只是添加else块

像这样的,

if (chatMessageObj.left) {
    new LoadProfileImage(chatImage).execute(personPhotoUrl);
} else {
    chatImage.setVisibility(View.GONE);
}

希望这能帮助您:)

我假设如果chatMessageObj.left不是真的,那么您不想执行异步任务?如果是这样,那么您在这里提供的内容看起来是正确的。您需要检查值或chatMessageObj.left的分配方式
if (chatMessageObj.left) {
    new LoadProfileImage(chatImage).execute(personPhotoUrl);
} else {
    chatImage.setVisibility(View.GONE);
}