* 更新* 嗨我解決我的問題我自己,我用的https://github.com/koush/UrlImageViewHelper/blob/master/README.md 並通過UrlImageViewHelper.setUrlDrawable(圖像,ei.img_url);從我的Entryadapter類,它運作良好。如何實現視圖座插入陣列適配器
我想顯示Listview與3文本和一個imageview與分隔符/節列表中。 所以我按照這個教程http://bartinger.at/listview-with-sectionsseparators/?
比我面對加載圖像(位圖)的問題,所以我使用文件和bitmapfactory通過使用AsyncTask在列表視圖上顯示位圖。
但問題是它加載每次當我向上或向下滾動。我想要顯示列表一次,所以我想實現查看持有人爲我的列表,但因爲我更新的Android和Java我不知道我怎麼能實現這個我的代碼。
任何人都可以幫助我嗎?我附上了所有的代碼文件,所以這段代碼會幫助像我這樣的其他學生和新手。
這裏是我的Entryadaptor.java
public class EntryAdapter extends ArrayAdapter<Item> {
private Context context;
private ArrayList<Item> items;
private LayoutInflater vi;
Uri myurl;
ImageView image;
public EntryAdapter(Context context,ArrayList<Item> items) {
super(context,0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.list_item_section, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
}else
{
EntryItem ei = (EntryItem)i;
v = vi.inflate(R.layout.list_item_entry, null);
final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
final TextView subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary);
image = (ImageView)v.findViewById(R.id.showlist_item_entry_drawable);
if (title != null)
title.setText(ei.title);
if(subtitle != null)
subtitle.setText(ei.subtitle);
if(image !=null)
{
try {
URL onLineURL = new URL(ei.img_url);
new MyNetworkTask(image).execute(onLineURL);
} catch (MalformedURLException e) {
e.printStackTrace();
}}}}
return v;
}
private class MyNetworkTask extends AsyncTask<URL, Void, Bitmap>{
ImageView tIV;
public MyNetworkTask(ImageView iv){
tIV = iv;
}
@Override
protected Bitmap doInBackground(URL... urls) {
Bitmap networkBitmap = null;
URL networkUrl = urls[0]; //Load the first element
try {
networkBitmap = BitmapFactory.decodeStream(
networkUrl.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
return networkBitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
tIV.setImageBitmap(result);
}
}
}
這裏是片段代碼
public class ShowsFragment extends SherlockListFragment { ArrayList<Item> items = new ArrayList<Item>();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
items.add(new SectionItem("Today"));
items.add(new EntryItem("Item 1", "This is item 1.2","http://pierre.chachatelier.fr/programmation/images/mozodojo-original-image.jpg"));
items.add(new EntryItem("Item 2", "This is item 1.3","http://upload.wikimedia.org/wikipedia/commons/8/85/Image-New_Delhi_Lotus.jpg"));
items.add(new SectionItem("This Week"));
items.add(new EntryItem("Item 4", "This is item 2.1","https://twimg0-a.akamaihd.net/profile_images/1080041262/VMIX_logo_zoom_bkbg1_normal.png")); EntryAdapter adapter = new EntryAdapter(getActivity(), items);
setListAdapter(adapter);
}
public void onListItemClick(ListView l, View v, int position, long id) {
if(!items.get(position).isSection()){
EntryItem item = (EntryItem)items.get(position);
Toast.makeText(getActivity(), "You clicked " + item.title , Toast.LENGTH_SHORT).show();
}
super.onListItemClick(l, v, position, id);
}
}
sectionItem代碼
public class SectionItem implements Item{
private final String title;
public SectionItem(String title) {
this.title = title;
}
public String getTitle(){
return title;
}
public boolean isSection() {
return true;
}
}
入境的物品類別代碼
public class EntryItem implements Item{
public final String title;
public final String subtitle;
public final String img_url;
public EntryItem(String title, String subtitle,String Image_url) {
this.title = title;
this.subtitle = subtitle;
this.img_url = Image_url;
}
public boolean isSection() {
return false;
}
}
我正在使用asynctask,因爲每次findviewby id在那個時候被調用,它使我的應用程序變得更慢,這就是爲什麼我使用Asyntask..at那時候我不知道視圖持有者。查看持有者重用舊視圖,它不會調用新視圖,除非直到列表更新。所以我會刪除異步任務,你可以建議我如何使用視圖持有人在我的代碼? –
你一定需要保持AsyncTask來加載圖像,否則你的應用程序將無法在UiThread上做太多的工作。我只是在指出,你需要知道在此期間事情可能會改變。在異步編程中始終要注意一件重要的事情。 – koljaTM
謝謝你能指導我如何使用MAP存儲位圖,以及如何避免下次findviewby id? –