Java EndlessAdapter未使用Android CWAC EndlessAdapter中的更新进行更新
我很难理解的文档 我尝试使用Commonware执行以下快速步骤,但没有成功: 因此,首先,您可以让自定义适配器自己正常工作。 然后,创建EndlessAdapter子类(使用 实现cacheInBackground()等),包装自定义适配器 在EndlessAdapter子类的实例中,并将 ListView中EndlesAdapter子类的 我有三个java类。我的活动,我的常规适配器(在我尝试集成无止境适配器之前工作),我有一个子类无止境适配器。我肯定我只是在做一些小事情,让它不能正常触发。我的应用程序没有崩溃,它显示了我的“加载”视图,它只是从不更新,而且似乎加载了很多东西,因为gui真的变慢了。我可能陷入了某种圈套。有什么想法吗 我的主要活动:Java EndlessAdapter未使用Android CWAC EndlessAdapter中的更新进行更新,java,android,android-listview,commonsware-cwac,Java,Android,Android Listview,Commonsware Cwac,我很难理解的文档 我尝试使用Commonware执行以下快速步骤,但没有成功: 因此,首先,您可以让自定义适配器自己正常工作。 然后,创建EndlessAdapter子类(使用 实现cacheInBackground()等),包装自定义适配器 在EndlessAdapter子类的实例中,并将 ListView中EndlesAdapter子类的 我有三个java类。我的活动,我的常规适配器(在我尝试集成无止境适配器之前工作),我有一个子类无止境适配器。我肯定我只是在做一些小事情,让它不能正常触发。
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);
}
}
公共类MyEndlesAdapter扩展了EndlesAdapter{
ArrayList tempList=新的ArrayList();
受保护的字符串标题;
受保护的ArrayList标题;
受保护的字符串内容;
保护字符串;
阵列适配器;
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;
}
}
公共MyEndlesAdapter(ListAdapter包装,上下文ctx){
超级(包装);
上下文=ctx;
titles=新的ArrayList();
oldadapter=(ArrayAdapter)已包装;
//TODO自动生成的构造函数存根
}
@凌驾
受保护的无效appendCachedData(){
//TODO自动生成的方法存根
oldapter.addAll(标题);
}
@凌驾
受保护的视图getPendingView(视图组父视图){
//TODO自动生成的方法存根
TextView视图=新的TextView(上下文);
view.setText(“加载”);
返回视图;
}
@凌驾
受保护的布尔cacheInBackground()引发异常{
离子交换(
上下文
"http://myblog.com/api/get_posts/?page=“+页面+”&计数=”
+count).asJsonObject()
.setCallback(新的FutureCallback(){
@凌驾
未完成公共无效(异常e,JsonObject结果){
如果(结果==null){
Log.e(“标签”、“服务器崩溃”);
Log.e(“标签”、“崩溃”);
}
String status=result.getAsJsonPrimitive(“状态”)
.getAsString();
如果(!status.equals(“ok”)){
Log.e(“标签”,“api不存在”);
Log.e(“标签”,“api不”);
}否则{
}
JsonArray jsonPostArray=结果
.getAsJsonArray(“职位”);
int i=0;
对于(JsonElement jsonElementPost:jsonPostArray){
Log.d(“,i+”);
i++;
JsonObject jsonPost=jsonElementPost
.getAsJsonObject();
int ID=jsonPost.get(“ID”).getAsInt();
字符串url=jsonPost.get(“url”).getAsString();
title=jsonPost.get(“title”).getAsString();
标题。添加(标题);
content=jsonPost.get(“content”).getAsString();
字符串date=jsonPost.get(“日期”).getAsString();
试一试{
JsonObject cat=jsonPost
.getAsJsonArray(“类别”).get(0)
.getAsJsonObject();
字符串catTitle=cat.get(“标题”)
.getAsString();
}捕获(异常e2){
//TODO自动生成的捕捉块
e2.printStackTrace();
}
JsonObject author=jsonPost
.getAsJsonObject(“作者”);
String name=author.get(“name”).getAsString();
JsonObject at=null;
试一试{
at=jsonPost.getAsJsonArray(“附件”)
.get(0.getAsJsonObject();
atUrl=at.get(“url”).getAsString();
字符串atMime=at.get(“mime类型”)
.getAsString();
}捕获(异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
atUrl=“”;
}
}
//Log.d(“TAG”,ID+);
//完成();
}
});
返回true;
}
}
我肯定我只是在做一些小事情,让它不能正常触发
让我们回顾一下文档“用法”部分的主要标题:
- :您正在调用
子类上的构造函数EndlessAdapter
- :您已经在
子类上实现了EndlessAdapter
getPendingView()
- :您已经在
子类上实现了EndlessAdapter
,虽然您的实现很奇怪(三个cacheInBackground()
而不是一个ArrayList
?),但它可能可以工作ArrayList
- :BZZZT--您的
没有appendCachedData()
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;
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; } }