如何使用BaseAdapter在ListView中显示远程图像?

我正在使用以下代码在BaseView上使用BaseAdapter显示图像。代码显示可绘制文件夹内的图像。但我想修改代码,使其显示从以下数组远程图像:如何使用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"}; 

能否专家告诉我什么部分需要提前change.Thanks。

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;

}

}

回答:

您需要:

1)获取这些图像在一个单独的线程,你可以使用抽射,改造,为此抢劫。

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

3)建议为您的listview的适配器使用一个viewholder,以避免反复膨胀视图。

回答:

最简单的事情是用Glide或Picasso:

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

}

回答:

,你也可以使用一些第三方库像毕加索或滑翔到的图像加载到您的权利适配器的get观点方法

毕加索。 ((this).load(flags [适配器的 位置])转换为(imageView);

相同的滑行。

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

以上是 如何使用BaseAdapter在ListView中显示远程图像? 的全部内容, 来源链接: utcz.com/qa/259067.html

回到顶部