我将此代码用于custum listview
自定义列表视图
public class ListAdapt extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public ListAdapt(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_item, null);
TextView title = (TextView)vi.findViewById(R.id.name); // title
TextView title1 = (TextView)vi.findViewById(R.id.name1);
TextView title2 = (TextView)vi.findViewById(R.id.idi);
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(online.KEY_NAME));
title2.setText(song.get(online.KEY_ID));
title1.setText(song.get(online.KEY_DESC));
String as = title1.getText().toString();
new DownloadImageTask((ImageView) vi.findViewById(R.id.imageView1))
.execute(as);
return vi;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(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);
}
}
}
自定义列表视图中的使用Asynctask,从Asynctask中的URL加载图像。
网址=字符串
此代码工作并加载图片但是当触摸手机中的滚动时,凌乱的列表视图图像。
ListActivity
public class online extends ListActivity {
private ProgressDialog pDialog;
// All static variables
static final String URL = "http://titanz13.byethost10.com/codee.xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_DESC = "description";
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setTheme(android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
super.onCreate(savedInstanceState);
setContentView(R.layout.online);
new GetContacts().execute();
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name = ((TextView) view.findViewById
(R.id.name)).getText().toString();
String h = ((TextView) view.findViewById
(R.id.idi)).getText().toString();
int mynum = Integer.parseInt(h);
if (mynum==1){
Uri uri = Uri.parse(name);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}else {
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, name);
try {
startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
Context context = getApplicationContext();
CharSequence text = "نرم افزار واتس اپ بر روی دستگاه شما نصب نیست";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
}
});
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(online.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
ListAdapt adapter = new ListAdapt(online.this, menuItems);
setListAdapter(adapter);
}
}
答案 0 :(得分:1)
它很混乱,因为您正在遵循一种不好的做法,将远程URL /图像加载到ListView内的ImageView中。
您需要遵循最佳做法将Image加载到ListView内的ImageView中。一种最佳实践是实现延迟加载图像逻辑。使用哪些图像将在后台加载并缓存到内存中,以便从缓存加载图像而不是再次下载。
网上有很多图书馆都有相同的实现,我在一些图书馆写了一篇文章:Image loading library
(P.S。很抱歉在答案中放置一个链接,因为我不能在原始链接中包含所需的部分,因为它很长并且很抱歉包含个人博客链接,但此处需要参考。)