【问题标题】:Glide to display image in listview滑动以在列表视图中显示图像
【发布时间】:2017-09-26 10:15:34
【问题描述】:

我需要帮助。我正在尝试使用 glide 从服务器获取我的图像,但我不知道如何?请构建我的代码,以便 Glide 可以工作或给我任何其他可能的解决方案。我只想用 Glide 和 JSON 显示图像列表视图,JSON 结果来自我的 PHP 脚本。我能够获取除图像之外的其他数据。这是我的代码。我应该把滑翔机放在哪里?

 public class list extends ListActivity {

        private static final String TAG_POST= "posts";
        private static final String TAG_ID="lecID";
        private static final String TAG_NAME="lecName";
        private static final String TAG_EMAIL="lecEmail";
        private static final String TAG_PASS="lecPass";
        private static final String TAG_IMAGE="image";
        private static final String LECTURER_LIST_URL="http://system.com/list.php";

        private JSONArray mComments =null;
        private ArrayList<HashMap<String,String>> mcommentList;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.list_view);
            ImageView imageView=(ImageView)findViewById(R.id.lec_Image);
//upload is the folder to store my image
            String leciv="http://fypesystem.com/upload/";
            Glide.with(this).load(leciv)
                    .thumbnail(0.5f)
                    .into(imageView);

非常感谢您的帮助。

【问题讨论】:

  • 你能用问题添加你的json响应吗
  • 无法将文件夹 URL 加载到 ImageView 中

标签: android json listview


【解决方案1】:
public static void LoadImage(Context cx,ImageView imageView,int demoImage,String url) {
    if (!((Activity) cx).isFinishing()){
        Glide.with(cx)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(demoImage)
                .into(imageView);
}
}

cx 是上下文 demoImage 是在加载图像之前显示的图像,而 Url 是从 JSON 获取的图像路径。

【讨论】:

  • 嗨@Omer Hayat,我应该在哪里声明 LoadImage() ?
  • 这是一种方法,您可以将其放在任何类中并使用它,或者您可以将其复制到当前活动或主要活动中,并在加载图像时调用它,只需传递期望参数。
  • 但它显示错误 LoadImage();不能应用。 :(
  • 只需从中删除静态并尝试像这样调用它 LoadImage(this,imageView,R.drawable.demoImage,url)
  • ImageView imageView=(ImageView) findViewById(R.id.yourImageViewId);你想在哪里显示你的服务器图像
【解决方案2】:

您必须创建一个自定义适配器并将您从 json 创建的列表和您想要用于适配器的布局和您的活动的上下文传递给该适配器。 之后,如果您使用的是 listview,而不是在 listview 的 getView() 函数中添加此代码,其中 url 应该是您存储在列表中的值 像这样

字符串 url = mcommentList.get(position).get(TAG_IMAGE);

myImageView 应该是您通过适配器的布局中的图像

Glide.with(上下文) .load(网址) .placeholder(R.drawable.ic_launcher) .into(myImageView);

您可以查看此链接以获取帮助

https://www.caveofprogramming.com/guest-posts/custom-listview-with-imageview-and-textview-in-android.html

https://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial

【讨论】:

    【解决方案3】:

    更改这些行特别是 URL。

    ImageView imageView=(ImageView)findViewById(R.id.lec_Image);
    //upload is the folder to store my image
                String leciv="http://fypesystem.com/upload/9c419dd5659b0af4d14f642f454dcb07.jpg";
                Glide.with(this).load(leciv)
                        .thumbnail(0.5f)
                        .into(imageView);
    

    【讨论】:

    • 如果我写了upload/9c419dd5659b0af4d14f642f454dcb07.jpg,它只会获取相同的图像?
    • 你必须把所有的 Url 放在一个数组列表中,然后一个一个地设置它们。给我看看你的 json。
    猜你喜欢
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 2013-04-24
    相关资源
    最近更新 更多