Android 如何使用BaseAdapter在ListView中显示远程图像?
我使用下面的代码使用BaseAdapter在ListView上显示图像。但我想修改代码,使其显示来自以下阵列的远程图像:Android 如何使用BaseAdapter在ListView中显示远程图像?,android,listview,android-studio,baseadapter,imageurl,Android,Listview,Android Studio,Baseadapter,Imageurl,我使用下面的代码使用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.pn
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();
}
});
}
}
您需要:
1) 在一个单独的线程中获取这些图像,您可以使用截击、改装、机器人空间来实现这一点
2) 在1)中的任何方法响应时,必须将从服务获得的值列表传递给适配器的构造函数。您需要为模型创建一个POJO,该结构将保存REST Web服务中的所有元素
3) 建议为listview的适配器使用viewholder,以避免视图一再膨胀。最简单的方法是使用或:
您还可以使用诸如毕加索或Glide之类的第三方库将图像直接加载到适配器的get view方法中 毕加索.with(this).load(标志[适配器的 位置),插入(图像视图) 滑翔时也一样
这里是简单的教程谢谢回复。我在android开发方面不是很有经验,所以你能给我指一些关于如何编写函数并从getView内部调用它并将图像数组传递给它以便在listview上显示它们的教程吗?是的,你可以开始:稍后你可以使用jackson数据绑定将响应中的值映射到POJO(模型类)。请参阅此链接以供参考:谢谢您的帖子。你能告诉我如何将GlideApp添加到android studio 2.2中吗?我以前从未用过。
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;
}
}
@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;
}