我在使用cwac-endless 时遇到问题 请注意,我正在使用支持库和操作栏 sherlock(我的问题可能与此无关)。我的自定义适配器在 SherlockListFragment 中工作正常(扩展 BaseAdapter)。
当我向下滚动并最终获取新内容(使用自己的类扩展 Thread)时,一切似乎都工作正常(任务在 DemoAdapter(扩展 EndlessAdapter)的 onItemsReady() 中返回,新内容在那里(不显示,但对象不为空) ,并添加到我的对象中)。
所以当我调用 onDataReady() 时,什么也没有发生。几个小时以来一直在努力解决这个问题。
你看到我的代码有什么问题吗?没有错误,只是没有显示新内容。
片段类
public class BusinessListFragment extends SherlockListFragment {
private DemoAdapter adapter;
static public List<Business> items;
public static final String TAG = BusinessListFragment.class.getName();
public interface IItemsReadyListener {
public void onItemsReady(List<Business> data);
}
public BusinessListFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business_list, null);
return view;
}
@Override
public void onViewCreated(View arg0, Bundle arg1) {
super.onViewCreated(arg0, arg1);
RestoApplication app = (RestoApplication) (getActivity().getApplication());
if (adapter == null) {
adapter = new DemoAdapter(getSherlockActivity(), app, items);
adapter.setRunInBackground(false);
}
setListAdapter(adapter);
}
static public void start() {
}
}
无限功能的适配器
public class DemoAdapter extends EndlessAdapter implements IItemsReadyListener {
private RotateAnimation rotate = null;
ArrayList<String> tempList = new ArrayList<String>();
private final Context ctx;
private final RestoApplication app;
private List<Business> items;
private static final String LOG_TAG = DemoAdapter.class.getName();
private boolean hasMoreData = false;
private int page = 1;
final Handler mHandler = new Handler();
void setDataFromAnyThread(final List<Business> newData) {
// Enqueue work on mHandler to change the data on
// the main thread.
mHandler.post(new Runnable() {
@Override
public void run() {
items.addAll(newData);
onDataReady();
hasMoreData = items.isEmpty();
Log.d(LOG_TAG, "ok done, items size: " + items.size());
}
});
}
public DemoAdapter(Context ctx, RestoApplication app, List<Business> items) {
super(new SearchBusinessAdapter(ctx, app, items));
this.ctx = ctx;
this.app = app;
this.items = new LinkedList<Business>(items);
rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(600);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
page++; // the moment cacheInBackground will be called for the first
// time we'll need to download page 2;
}
@Override
protected View getPendingView(ViewGroup parent) {
View row = LayoutInflater.from(ctx).inflate(R.layout.list_item_business, null);
View child = row.findViewById(R.id.info_container);
child.setVisibility(View.GONE);
child = row.findViewById(R.id.throbber);
child.setVisibility(View.VISIBLE);
child.startAnimation(rotate);
return (row);
}
@Override
protected boolean cacheInBackground() {
Log.d(LOG_TAG, "cacheInBackground");
String path = new SearchBuinessUrlBuilder().businessType("1").lang(AbstractActivity.FRENCH).pageNumber(page).location("londres").build();
new SearchBusinessLoader(mHandler, DemoAdapter.this, ctx, path).start();
return hasMoreData;
}
@Override
protected void appendCachedData() {
Log.d(LOG_TAG, "appendCachedData");
}
@Override
public void onItemsReady(final List<Business> data) {
Log.d(LOG_TAG, "onItemsReady in DemoAdapter new data.size()= "+data.size());
setDataFromAnyThread(data);
page++;
}
}
自定义适配器
public class SearchBusinessAdapter extends BaseAdapter {
private final static String BUS_TAG = SearchBusinessAdapter.class.getSimpleName();
private final static String LOG_TAG = SearchBusinessAdapter.class.getSimpleName();
private final Context ctx;
private final RestoApplication app;
private final List<Business> items;
public SearchBusinessAdapter(Context ctx, RestoApplication app, List<Business> items) {
this.ctx = ctx;
this.items = new LinkedList<Business>(items);
this.app = app;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view;
if (convertView == null) {
view = LayoutInflater.from(ctx).inflate(R.layout.list_item_business, null);
} else {
view = convertView;
}
Business item = items.get(position);
ImageView imgThumbmail = ((ImageView) view.findViewById(R.id.newsListItemThumbnail));
imgThumbmail.setImageDrawable(ctx.getResources().getDrawable(R.drawable.ic_launcher));
TextView nameTf = ((TextView) view.findViewById(R.id.name));
FontsWrapper.setBoldTf(ctx, new TextView[] { nameTf });
nameTf.setText(item.getName());
TextView adressTf = ((TextView) view.findViewById(R.id.adress));
FontsWrapper.setCondensedTf(ctx, new TextView[] { adressTf });
StringBuilder adressSb = new StringBuilder();
adressSb.append(item.getStreet());
adressSb.append("\n");
adressSb.append(item.getZip() + " " + item.getCity());
adressTf.setText(adressSb.toString());
TextView cuisineTypeTf = ((TextView) view.findViewById(R.id.cuisineType));
FontsWrapper.setLightTf(ctx, new TextView[] { cuisineTypeTf });
StringBuilder cuisinesSb = new StringBuilder();
cuisinesSb.append(ctx.getText(R.string.cuisine_type_label));
cuisinesSb.append(": ");
if (item.getCuisinesList() != null) {
for (int i = 0; i < item.getCuisinesList().size(); i++) {
cuisinesSb.append(item.getCuisinesList().get(i).getCuisine());
if (i < item.getCuisinesList().size() - 1) {
cuisinesSb.append(", ");
}
}
} else {
cuisinesSb.append("/");
}
cuisineTypeTf.setText(cuisinesSb);
return view;
}
}
搜索业务加载器:
public class SearchBusinessLoader extends AbstractSearchBusinessLoader {
static final String LOG_TAG = SearchBusinessLoader.class.getSimpleName();
public SearchBusinessLoader(Handler handler, SearchBusinessListener listener, Context ctx, String path) {
super();
super.ctx = ctx;
this.handler = handler;
this.listener = listener;
this.path = path;
}
public SearchBusinessLoader(Handler handler, IItemsReadyListener listener, Context ctx, String path) {
super();
super.ctx = ctx;
this.handler = handler;
this.endlessListener = listener;
this.path = path;
}
@Override
InputStream getStream() throws IOException, Exception {
DefaultHttpClient client = new DefaultHttpClient();
Log.d(LOG_TAG, "path= "+path);
HttpGet request = new HttpGet(path);
HttpResponse response = client.execute(request);
return response.getEntity().getContent();
}
}
抽象类(完成后解析和调用监听器)
public abstract class AbstractSearchBusinessLoader extends Thread {
protected Handler handler;
protected SearchBusinessListener listener;
protected IItemsReadyListener endlessListener;
protected Context ctx;
protected String path;
private List<Business> businessesList = new ArrayList<Business>();
public interface SearchBusinessListener {
public void onSearchResult(List<Business> bizList);
}
protected final static String LOG_TAG = AbstractSearchBusinessLoader.class.getSimpleName();
public final static int MSG_ERROR = 201;
public final static int MSG_FINISHED = 200;
public final static String BUNDLE_KEY_ERROR = "bundleKeyError";
abstract InputStream getStream() throws IOException, Exception;
@Override
public void run() {
InputStream stream = null;
try {
stream = getStream();
} catch (IOException e) {
Log.e(LOG_TAG, "IOException while getting: " + path, e);
handler.sendEmptyMessage(MSG_ERROR);
return;
} catch (Exception e) {
Log.e(LOG_TAG, "Exception while getting: " + path, e);
handler.sendEmptyMessage(MSG_ERROR);
return;
}
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(stream, null);
parser.next();
while (!(parser.getEventType() == XmlPullParser.END_TAG && XmlTags.SEARCHRESPONSE.equals(XmlTags.fromString(parser.getName())))) {
if (parser.getEventType() == XmlPullParser.START_TAG && XmlTags.TOTALRESULTS.equals(XmlTags.fromString(parser.getName()))) {
String totalRes = parser.nextText();
}
if (parser.getEventType() == XmlPullParser.START_TAG && XmlTags.SEARCHSEED.equals(XmlTags.fromString(parser.getName()))) {
String searchSeed = parser.nextText();
}
if (parser.getEventType() == XmlPullParser.START_TAG && XmlTags.BUSINESSES.equals(XmlTags.fromString(parser.getName()))) {
parseBusinesses(parser);
}
parser.next();
}
} catch (XmlPullParserException e) {
Log.e(LOG_TAG, "XmlPullParserException while processing business result xml", e);
handler.sendEmptyMessage(MSG_ERROR);
return;
} catch (IOException e) {
Log.e(LOG_TAG, "IOException while processing business result xml", e);
handler.sendEmptyMessage(MSG_ERROR);
return;
}
if (listener != null) {
listener.onSearchResult(businessesList);
}
if (endlessListener != null) {
Log.d(LOG_TAG, "endlessListener : onItemsReady");
endlessListener.onItemsReady(businessesList);
}
}
private void parseBusinesses(XmlPullParser parser) throws XmlPullParserException, IOException {
...
}
private List<Cuisine> parseCuisines(XmlPullParser parser) throws XmlPullParserException, IOException {
...
}
private List<Picture> parsePictures(XmlPullParser parser) throws XmlPullParserException, IOException {
....
}
private void parseBusiness(XmlPullParser parser) throws XmlPullParserException, IOException {
...
}