【问题标题】:How to display remote images in ListView using BaseAdapter?如何使用 BaseAdapter 在 ListView 中显示远程图像?
【发布时间】:2017-12-03 01:05:36
【问题描述】:

我正在使用以下代码使用 BaseAdapter 在 ListView 上显示图像。该代码显示来自可绘制文件夹内部的图像。但我想修改代码,使其显示来自以下数组的远程图像:

  String flags[] ={"http://www.website.com/images/usa.png","http://www.website.com/images/china.png","http://www.website.com/images/australia.png","http://www.website.com/images/portugle.png","http://www.website.com/images/norway.png","http://www.website.com/images/new_zealand.png"};

专家能否告诉我需要更改的部分。在此先感谢。

MainActivity.java:

public class MainActivity extends Activity {

    ListView simpleList;
    String countryList[] = {"USA", "China", "australia", "Portugle", "Norway", "NewZealand"};
    int flags[] = {R.drawable.usa, R.drawable.china, R.drawable.australia, R.drawable.portugle, R.drawable.norway, R.drawable.new_zealand};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        simpleList = (ListView) findViewById(R.id.simpleListView);
        CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), countryList, flags);
        simpleList.setAdapter(customAdapter);

        simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(getApplicationContext(), "Hello " + countryList[position], Toast.LENGTH_LONG).show();


            }
        });
    }
}

CustomAdapter.java:

Public class CustomAdapter extends BaseAdapter {
    Context context;
    String countryList[];
    int flags[];
    LayoutInflater inflter;

    public CustomAdapter(Context applicationContext, String[] countryList, int[] flags) {
        this.context = context;
        this.countryList = countryList;
        this.flags = flags;
        inflter = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return countryList.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflter.inflate(R.layout.activity_listview, null);
        TextView country = (TextView) view.findViewById(R.id.textView);
        ImageView icon = (ImageView) view.findViewById(R.id.icon);
        country.setText(countryList[i]);
        icon.setImageResource(flags[i]);
        return view;
    }
}

【问题讨论】:

    标签: android listview android-studio baseadapter imageurl


    【解决方案1】:

    你需要:

    1) 在单独的线程中获取这些图像,您可以为此使用 volley、retrofit、robospice。

    2) 对于 1) 中任何这些方法的响应,您必须将从服务获得的值列表传递给适配器的构造函数。您需要为模型创建一个 POJO,该结构将包含来自 REST Web 服务的所有元素。

    3) 建议为您的列表视图适配器使用视图支架,以避免一次又一次地膨胀视图。

    【讨论】:

    • 感谢您的回复。我在 android 开发方面不是很有经验,所以你能告诉我一些关于如何编写函数并从 getView 内部调用它并将图像数组传递给它以便在 listview 上显示它们的教程吗?
    • 稍后您可以使用杰克逊数据绑定将响应中的值映射到 POJO(模型类)。请参阅此链接以供参考:stackoverflow.com/questions/25545984/…
    【解决方案2】:

    最简单的做法是使用GlidePicasso

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflter.inflate(R.layout.activity_listview, null);
        TextView country = (TextView) view.findViewById(R.id.textView);
        ImageView icon = (ImageView) view.findViewById(R.id.icon);
        country.setText(countryList[i]);
    
        // Assuming flags is now the list of Strings of image urls
        GlideApp.with(view.getContext()).load(flags[i]).into(icon);
    
        return view;
    }
    

    【讨论】:

    • 感谢您的帖子。你能告诉我如何将 GlideApp 添加到 android studio 2.2 吗?我以前从未使用过。
    【解决方案3】:

    您还可以使用诸如 Picasso 或 Glide 之类的第三方库将图像直接加载到适配器的 get view 方法中

    Picasso.with(this).load(flags[adapter's 位置]).into(imageView);

    滑翔也一样。

    这里是简单教程https://www.simplifiedcoding.net/picasso-android-tutorial-picasso-image-loader-library/

    【讨论】:

      猜你喜欢
      • 2016-01-10
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多