我很难理解endless adapter
的文档我尝试按照Commonsware的这些快速步骤无效:How can I use a custom adapter with the cwac-endless adapter?
因此,首先,您可以自定义适配器。 然后,您创建您的EndlessAdapter子类(使用您的 执行cacheInBackground()等),包装自定义适配器 在您的EndlessAdapter子类的实例中,并放置您的实例 ListView中的EndlessAdapter子类
我有三个java类。我的活动,我的常规适配器(在我尝试集成无限适配器之前工作),我有一个子类无限适配器。我敢肯定我只是在做一些小事,这使得它不能正确触发。我的应用程序没有崩溃,它显示我的“正在加载”视图,它只是永远不会更新,并且似乎后来加载了很多东西,因为gui真的减慢了。我可能陷入了某种循环。有什么想法吗?
我的主要活动:
package com.eghdk.myapp.gui;
import java.util.ArrayList;
import android.annotation.TargetApi;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;
import com.eghdk.myapp.R;
import com.eghdk.myapp.adapters.MyAdapter;
import com.eghdk.myapp.adapters.MyEndlessAdapter;
import com.eghdk.myapp.util.AppUtil;
public class MyBlog extends ListActivity {
String content;
String Url;
String title;
ArrayList<String> titles;
ArrayList<String> urls;
ArrayList<String> contents;
MyAdapter adapter;
int page = 1;
int count = 20;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_blog);
// Show the Up button in the action bar.
setupActionBar();
titles = new ArrayList<String>();
urls = new ArrayList<String>();
contents = new ArrayList<String>();
adapter = new MyAdapter(this, titles);
loadDataFromWeb(page, count);
}
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_my_blog, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void loadDataFromWeb(int page, int count) {
//I'm using Koushs Ion library to do Async Json calls.
Ion.with(
MyBlog.this,
"http://myblog.com/api/get_posts/?page=" + page + "&count="
+ count).asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
String status = result.getAsJsonPrimitive("status")
.getAsString();
if (!status.equals("ok")) {
Log.e("TAG", "api doesn't exist");
Log.e("TAG", "api doesn't");
}
JsonArray jsonPostArray = result
.getAsJsonArray("posts");
for (JsonElement jsonElementPost : jsonPostArray) {
JsonObject jsonPost = jsonElementPost
.getAsJsonObject();
int ID = jsonPost.get("id").getAsInt();
title = jsonPost.get("title").getAsString();
titles.add(title);
content = jsonPost.get("content").getAsString();
contents.add(content);
at = jsonPost.getAsJsonArray("attachments")
.get(0).getAsJsonObject();
Url = at.get("url").getAsString();
urls.add(Url);
}
// Log.d("TAG", ID + "");
done();
}
});
}
private void done() {
MyEndlessAdapter endless = new MyEndlessAdapter(adapter, this);
setListAdapter(endless);
}
}
MyAdapter.java:
package com.eghdk.myapp.adapters;
import java.util.ArrayList;
import android.R;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
ArrayList<String> titles;
Context context;
LayoutInflater mInflater;
public MyAdapter(Context context, ArrayList<String> list) {
titles = list;
this.context = context;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return titles.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = mInflater.inflate(
android.R.layout.simple_list_item_1, parent, false);
}
((TextView) convertView.findViewById(R.id.text1)).setText(Html
.fromHtml(titles.get(position)));
return convertView;
}
}
EndlessAdapter:
package com.eghdk.myapp.adapters;
import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;
import com.commonsware.cwac.endless.EndlessAdapter;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;
公共类MyEndlessAdapter扩展了EndlessAdapter { ArrayList tempList = new ArrayList(); protected String title; protected ArrayList标题; protected String内容; protected String atUrl; ArrayAdapter oldadapter; int page = 1,count = 20; 上下文上下文;
public MyEndlessAdapter(ListAdapter wrapped, Context ctx) {
super(wrapped);
context = ctx;
titles = new ArrayList<String>();
oldadapter = (ArrayAdapter) wrapped;
// TODO Auto-generated constructor stub
}
@Override
protected void appendCachedData() {
// TODO Auto-generated method stub
oldadapter.addAll(titles);
}
@Override
protected View getPendingView(ViewGroup parent) {
// TODO Auto-generated method stub
TextView view = new TextView(context);
view.setText("LOADING");
return view;
}
@Override
protected boolean cacheInBackground() throws Exception {
Ion.with(
context,
"http://myblog.com/api/get_posts/?page=" + page + "&count="
+ count).asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
if (result == null) {
Log.e("TAG", "server crash");
Log.e("TAG", " crash");
}
String status = result.getAsJsonPrimitive("status")
.getAsString();
if (!status.equals("ok")) {
Log.e("TAG", "api doesn't exist");
Log.e("TAG", "api doesn't");
} else {
}
JsonArray jsonPostArray = result
.getAsJsonArray("posts");
int i = 0;
for (JsonElement jsonElementPost : jsonPostArray) {
Log.d("", i + "");
i++;
JsonObject jsonPost = jsonElementPost
.getAsJsonObject();
int ID = jsonPost.get("id").getAsInt();
String url = jsonPost.get("url").getAsString();
title = jsonPost.get("title").getAsString();
titles.add(title);
content = jsonPost.get("content").getAsString();
String date = jsonPost.get("date").getAsString();
try {
JsonObject cat = jsonPost
.getAsJsonArray("categories").get(0)
.getAsJsonObject();
String catTitle = cat.get("title")
.getAsString();
} catch (Exception e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
JsonObject author = jsonPost
.getAsJsonObject("author");
String name = author.get("name").getAsString();
JsonObject at = null;
try {
at = jsonPost.getAsJsonArray("attachments")
.get(0).getAsJsonObject();
atUrl = at.get("url").getAsString();
String atMime = at.get("mime_type")
.getAsString();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
atUrl = "";
}
}
// Log.d("TAG", ID + "");
// done();
}
});
return true;
}
}
答案 0 :(得分:0)
我确信我只是在做一些小事,这使得它不能正确触发。
让我们回顾一下文档“用法”部分的主要标题:
Constructors:您正在调用EndlessAdapter
子类
Placeholder:您已在getPendingView()
子类上实施EndlessAdapter
The Loading:您已在cacheInBackground()
子类上实施EndlessAdapter
,而您的实施很奇怪(三个ArrayList<String>
而不是ArrayList<WhateverYourActualModelObjectShouldBe>
?),它可能有效
The Attaching: BZZZT - 您的appendCachedData()
没有做任何事情
The Threading:您的代码应该没问题,只要您不同时使用其他AsyncTasks
,或者android:targetSdkVersion
未满13 < / p>
The Overriding:您的代码应该没问题
所以,我建议您回到The Attaching,仔细阅读该部分,并查看演示应用程序的实现。尽管您的数据模型是数组,但您选择使用BaseAdapter
而不是ArrayAdapter
可能会更复杂。