Android EndLessAdapter+;异步任务+;自定义碎片列表
我想在我的应用程序中从Commonware实现EndlessAdapter,它是一个带有自定义ListView的Listfragment和一个扩展Baseadapter的lazyadapter 问题:Android EndLessAdapter+;异步任务+;自定义碎片列表,android,android-asynctask,android-listfragment,cwac-endless,Android,Android Asynctask,Android Listfragment,Cwac Endless,我想在我的应用程序中从Commonware实现EndlessAdapter,它是一个带有自定义ListView的Listfragment和一个扩展Baseadapter的lazyadapter 问题: public class EndlessCustomView extends ListFragment{ // All static variables static final String URL = "http://api.androidhive.info/music
public class EndlessCustomView extends ListFragment{
// All static variables
static final String URL = "http://api.androidhive.info/music/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
ListView list;
DemoAdapter adapter=null;
ArrayList<HashMap<String, String>> songsList = null;
// @Override
// public void onActivityCreated(Bundle savedInstanceState) {
// // TODO Auto-generated method stub
// super.onActivityCreated(savedInstanceState);
//
// setRetainInstance(true);
//
// songsList = new ArrayList<HashMap<String, String>>();
//
// if (getActivity() != null) {
//
// adapter = new DemoAdapter(getActivity(), songsList);
// adapter.setRunInBackground(false);
// setListAdapter(adapter);
// }
//
// }
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
//
//// new MyAsyncTask().execute();
setRetainInstance(true);
songsList = new ArrayList<HashMap<String, String>>();
if (getActivity() != null) {
adapter = new DemoAdapter(getActivity(), songsList);
adapter.setRunInBackground(false);
setListAdapter(adapter);
}
}
class DemoAdapter extends EndlessAdapter implements IItemsReadyListener{
private RotateAnimation rotate=null;
private View pendingView=null;
private FragmentActivity activity;
private ArrayList<HashMap<String, String>> data;
public ImageLoader imageLoader;
private List<HashMap<String, String>> mOriginalNames;
private boolean hasMoreData=true;
DemoAdapter(FragmentActivity fragmentActivity, ArrayList<HashMap<String, String>> d) {
super(new LazyAdapter(fragmentActivity, d));
rotate=new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
}
@Override
protected View getPendingView(ViewGroup parent) {
View row = getActivity().getLayoutInflater().inflate(R.layout.row, null);
View child=row.findViewById(android.R.id.text1);
child.setVisibility(View.GONE);
child=row.findViewById(R.id.throbber);
child.setVisibility(View.VISIBLE);
child.startAnimation(rotate);
return(row);
}
@Override
protected boolean cacheInBackground() {
new MyAsyncTask(this, data.size()).execute();
return hasMoreData;
}
public void onItemsReady(ArrayList<HashMap<String, String>> data) {
songsList.addAll(data);
adapter.onDataReady(); // Tell the EndlessAdapter to remove it's pending
// view and call notifyDataSetChanged()
hasMoreData = songsList.isEmpty();
}
@Override
protected void appendCachedData() {
// if (getWrappedAdapter().getCount()<75) {
// @SuppressWarnings("unchecked")
// LazyAdapter a=(LazyAdapter)getWrappedAdapter();
// for (int i = 0; i < data.size(); i++) {
// a.add(data.get(i));
// }
// }
}
public class MyAsyncTask extends AsyncTask<Void,Void,ArrayList<HashMap<String, String>>>{
IItemsReadyListener listener;
private final ProgressDialog dialog=new ProgressDialog(getActivity());
int startPoint;
public MyAsyncTask(IItemsReadyListener listener, int startPoint) {
// TODO Auto-generated constructor stub
this.listener = listener;
this.startPoint = startPoint;
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... params) {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all song nodes <song>
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_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
return (songsList);
}
@Override
protected void onPreExecute()
{
dialog.setMessage("Loading ...");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result)
{
listener.onItemsReady(songsList);
// Getting adapter by passing xml data ArrayList
}
}
}
}
public class LazyAdapter extends BaseAdapter {
private FragmentActivity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
private List<HashMap<String, String>> mOriginalNames;
public LazyAdapter(FragmentActivity fragmentActivity, ArrayList<HashMap<String, String>> d) {
super();
this.activity = fragmentActivity;
this.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 data.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(CustomizedListView.KEY_TITLE));
artist.setText(song.get(CustomizedListView.KEY_ARTIST));
duration.setText(song.get(CustomizedListView.KEY_DURATION));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;
}
public void add(HashMap<String, String> map) {
// TODO Auto-generated method stub
data.add(map);
notifyDataSetChanged();
}
}
- 我在cacheInBackground()中遇到一个异常;当我调用MyAsyncTask时,有什么需要解决的帮助吗?我使用了EndlessadapterV1.2(最新版本)作为库
@Override protected boolean cacheInBackground() { new MyAsyncTask(this, data.size()).execute(); return hasMoreData; }
- 懒散的适配器在没有无端适配器的情况下运行良好
public class EndlessCustomView extends ListFragment{
// All static variables
static final String URL = "http://api.androidhive.info/music/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
ListView list;
DemoAdapter adapter=null;
ArrayList<HashMap<String, String>> songsList = null;
// @Override
// public void onActivityCreated(Bundle savedInstanceState) {
// // TODO Auto-generated method stub
// super.onActivityCreated(savedInstanceState);
//
// setRetainInstance(true);
//
// songsList = new ArrayList<HashMap<String, String>>();
//
// if (getActivity() != null) {
//
// adapter = new DemoAdapter(getActivity(), songsList);
// adapter.setRunInBackground(false);
// setListAdapter(adapter);
// }
//
// }
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
//
//// new MyAsyncTask().execute();
setRetainInstance(true);
songsList = new ArrayList<HashMap<String, String>>();
if (getActivity() != null) {
adapter = new DemoAdapter(getActivity(), songsList);
adapter.setRunInBackground(false);
setListAdapter(adapter);
}
}
class DemoAdapter extends EndlessAdapter implements IItemsReadyListener{
private RotateAnimation rotate=null;
private View pendingView=null;
private FragmentActivity activity;
private ArrayList<HashMap<String, String>> data;
public ImageLoader imageLoader;
private List<HashMap<String, String>> mOriginalNames;
private boolean hasMoreData=true;
DemoAdapter(FragmentActivity fragmentActivity, ArrayList<HashMap<String, String>> d) {
super(new LazyAdapter(fragmentActivity, d));
rotate=new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
}
@Override
protected View getPendingView(ViewGroup parent) {
View row = getActivity().getLayoutInflater().inflate(R.layout.row, null);
View child=row.findViewById(android.R.id.text1);
child.setVisibility(View.GONE);
child=row.findViewById(R.id.throbber);
child.setVisibility(View.VISIBLE);
child.startAnimation(rotate);
return(row);
}
@Override
protected boolean cacheInBackground() {
new MyAsyncTask(this, data.size()).execute();
return hasMoreData;
}
public void onItemsReady(ArrayList<HashMap<String, String>> data) {
songsList.addAll(data);
adapter.onDataReady(); // Tell the EndlessAdapter to remove it's pending
// view and call notifyDataSetChanged()
hasMoreData = songsList.isEmpty();
}
@Override
protected void appendCachedData() {
// if (getWrappedAdapter().getCount()<75) {
// @SuppressWarnings("unchecked")
// LazyAdapter a=(LazyAdapter)getWrappedAdapter();
// for (int i = 0; i < data.size(); i++) {
// a.add(data.get(i));
// }
// }
}
public class MyAsyncTask extends AsyncTask<Void,Void,ArrayList<HashMap<String, String>>>{
IItemsReadyListener listener;
private final ProgressDialog dialog=new ProgressDialog(getActivity());
int startPoint;
public MyAsyncTask(IItemsReadyListener listener, int startPoint) {
// TODO Auto-generated constructor stub
this.listener = listener;
this.startPoint = startPoint;
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... params) {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all song nodes <song>
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_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
return (songsList);
}
@Override
protected void onPreExecute()
{
dialog.setMessage("Loading ...");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result)
{
listener.onItemsReady(songsList);
// Getting adapter by passing xml data ArrayList
}
}
}
}
public class LazyAdapter extends BaseAdapter {
private FragmentActivity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
private List<HashMap<String, String>> mOriginalNames;
public LazyAdapter(FragmentActivity fragmentActivity, ArrayList<HashMap<String, String>> d) {
super();
this.activity = fragmentActivity;
this.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 data.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(CustomizedListView.KEY_TITLE));
artist.setText(song.get(CustomizedListView.KEY_ARTIST));
duration.setText(song.get(CustomizedListView.KEY_DURATION));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;
}
public void add(HashMap<String, String> map) {
// TODO Auto-generated method stub
data.add(map);
notifyDataSetChanged();
}
}
public类EndlessCustomView扩展了ListFragment{
//所有静态变量
静态最终字符串URL=”http://api.androidhive.info/music/music.xml";
//XML节点密钥
静态最终字符串键\u SONG=“SONG”//父节点
静态最终字符串键\u ID=“ID”;
静态最终字符串键\u TITLE=“TITLE”;
静态最终字符串键\u ARTIST=“ARTIST”;
静态最终字符串键\u DURATION=“DURATION”;
静态最终字符串键\u THUMB\u URL=“THUMB\u URL”;
列表视图列表;
德莫达