EndlessAdapter不与Android的CWAC-EndlessAdapter更新更新 [英] EndlessAdapter not updating with updates in Android CWAC-EndlessAdapter

查看:231
本文介绍了EndlessAdapter不与Android的CWAC-EndlessAdapter更新更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个艰难的时间用于了解无尽的适配器的文档

我试图通过以下这些Commonsware快速步骤无济于事:<一href=\"http://stackoverflow.com/questions/9650129/how-can-i-use-a-custom-adapter-with-the-cwac-endless-adapter\">How我可以使用自定义适配器与CWAC,无尽的适配器?


  

所以,首先,你会得到您的自定义适配器上自己的正常工作。
  然后,您创建EndlessAdapter子类(与您
  实施cacheInBackground()等),包住您的自定义适配器
  在EndlessAdapter子类的实例,并把您的实例
  在你的ListView的EndlessAdapter子类


我有三个Java类。我的活动,我的正常适配器(之前我试图整合无尽的适配器工作),我有一个子类无尽的适配器。我敢肯定,我只是做一些小的,以至于使这个不正确触发。我的应用程序不会崩溃,它显示了我的加载看来,这只是从来没有更新了,看来以后要加载很多东西,因为GUI真的减慢。我可能陷在某种循环。任何想法?

我的主要活动:

 包com.eghdk.myapp.gui;进口的java.util.ArrayList;进口android.annotation.TargetApi;
进口android.app.ListActivity;
进口android.content.Intent;
进口android.os.Build;
进口android.os.Bundle;
进口android.support.v4.app.NavUtils;
进口android.util.Log;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.ListView;进口com.google.gson.JsonArray;
进口com.google.gson.JsonElement;
进口com.google.gson.JsonObject;
进口com.koushikdutta.async.future.FutureCallback;
进口com.koushikdutta.ion.Ion;
进口com.eghdk.myapp.R;
进口com.eghdk.myapp.adapters.MyAdapter;
进口com.eghdk.myapp.adapters.MyEndlessAdapter;
进口com.eghdk.myapp.util.AppUtil;公共类MyBlog扩展ListActivity {
    字符串内容;
    字符串URL;
    字符串称号;
    ArrayList的&LT;串GT;标题;
    ArrayList的&LT;串GT;网址;
    ArrayList的&LT;串GT;内容;    MyAdapter适配器;
    INT页面= 1;
    诠释计数= 20;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_my_blog);
        //显示操作栏中的向上按钮。
        setupActionBar();        标题=新的ArrayList&LT;串GT;();
        网址=新的ArrayList&LT;串GT;();
        内容=新的ArrayList&LT;串GT;();        适配器=新MyAdapter(这一点,标题);        loadDataFromWeb(页计数);        }
    }    / **
     *设置{@link android.app.ActionBar},如果API是可用的。
     * /
    @TargetApi(Build.VERSION_ codeS.HONEYCOMB)
    私人无效setupActionBar(){
        如果(Build.VERSION.SDK_INT&GT; = Build.VERSION_ codeS.HONEYCOMB){
            getActionBar()setDisplayHomeAsUpEnabled(真)。
        }
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.activity_my_blog,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
        案例android.R.id.home:
            //这个ID重新presents主页或向上按钮。在这种情况下
            //活动,向上按钮显示。使用NavUtils允许用户
            //浏览了在应用结构中的一个级别。对于
            //更多详细信息,请参见Android设计的导航模式:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(本);
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }    公共无效loadDataFromWeb(INT页,诠释计数){
//我使用Koushs离子库执行异步调用的Json。
        Ion.with(
                MyBlog.this,
                http://myblog.com/api/get_posts/?page=+页+&放大器;计数=
                        +计数).asJsonObject()
                .setCallback(新FutureCallback&LT;&JSONObject的GT;(){
                    @覆盖
                    公共无效onCompleted(例外五,结果的JSONObject){                        字符串状态= result.getAsJsonPrimitive(身份)
                                .getAsString();                        如果(!status.equals(OK)){
                            Log.e(TAG,API不存在);
                            Log.e(TAG,API不);                        }                        JsonArray jsonPostArray =结果
                                .getAsJsonArray(上岗);                        对于(JsonElement jsonElementPost:jsonPostArray){                            JSONObject的jsonPost = jsonElementPost
                                    .getAsJsonObject();
                            INT ID = jsonPost.get(ID)getAsInt()。
                            。标题= jsonPost.get(题)符getAsString();
                            titles.add(职称);
                            。内容= jsonPost.get(内容)符getAsString();
                            contents.add(内容);
                                在= jsonPost.getAsJsonArray(附件)
                                        获得(0).getAsJsonObject();
                                。URL = at.get(URL)符getAsString();
                                urls.add(URL);                        }                        // Log.d(TAG,ID +);
                        完成();
                    }
                });    }    做私人无效(){
        MyEndlessAdapter无尽=新MyEndlessAdapter(适配器,这一点);
        setListAdapter(无尽);
    }}

MyAdapter.java:

 包com.eghdk.myapp.adapters;进口的java.util.ArrayList;进口android.R;
进口android.content.Context;
进口android.text.Html;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.TextView;公共类MyAdapter延伸BaseAdapter {
    ArrayList的&LT;串GT;标题;
    上下文语境;
    LayoutInflater mInflater;    公共MyAdapter(上下文的背景下,ArrayList的&LT;串GT;名单){
        标题=清单;
        this.context =背景;
        mInflater = LayoutInflater.from(上下文);
    }    @覆盖
    公众诠释的getCount(){
        // TODO自动生成方法存根
        返回titles.size();
    }    @覆盖
    公共对象的getItem(INT位置){
        // TODO自动生成方法存根
        返回的位置;
    }    @覆盖
    众长getItemId(INT位置){
        // TODO自动生成方法存根
        返回的位置;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        // TODO自动生成方法存根
        如果(convertView == NULL){
            convertView = mInflater.inflate(
                    android.R.layout.simple_list_item_1,父母,假);
        }
        ((的TextView)convertView.findViewById(R.id.text1))。的setText(HTML
                .fromHtml(titles.get(位置)));
        返回convertView;
    }}

EndlessAdapter:

 包com.eghdk.myapp.adapters;进口的java.util.ArrayList;进口android.content.Context;
进口android.util.Log;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ListAdapter;
进口android.widget.TextView;进口com.commonsware.cwac.endless.EndlessAdapter;
进口com.google.gson.JsonArray;
进口com.google.gson.JsonElement;
进口com.google.gson.JsonObject;
进口com.koushikdutta.async.future.FutureCallback;
进口com.koushikdutta.ion.Ion;

公共类MyEndlessAdapter扩展EndlessAdapter {
    ArrayList的tempList =新的ArrayList();
    受保护的字符串称号;
    保护ArrayList的标题;
    保护字符串的内容;
    保护字符串atUrl;
    ArrayAdapter oldadapter;
    INT页面= 1,计数= 20;
    上下文的背景下;

 公共MyEndlessAdapter(ListAdapter包裹,上下文CTX){
    超(包装);
    上下文= CTX;
    标题=新的ArrayList&LT;串GT;();
    oldadapter =(ArrayAdapter)包裹;    // TODO自动生成构造函数存根
}@覆盖
保护无效appendCachedData(){
    // TODO自动生成方法存根
    oldadapter.addAll(职称);}    @覆盖
    受保护的视图getPendingView(ViewGroup中父){
        // TODO自动生成方法存根
        TextView的视图=新的TextView(背景);
        view.setText(加载);
        返回视图。
    }    @覆盖
    保护布尔cacheInBackground()抛出异常{
        Ion.with(
                的背景下,
                http://myblog.com/api/get_posts/?page=+页+&放大器;计数=
                        +计数).asJsonObject()
                .setCallback(新FutureCallback&LT;&JSONObject的GT;(){
                    @覆盖
                    公共无效onCompleted(例外五,结果的JSONObject){
                        如果(结果== NULL){
                            Log.e(TAG,服务器崩溃);
                            Log.e(TAG,撞车);
                        }                        字符串状态= result.getAsJsonPrimitive(身份)
                                .getAsString();                        如果(!status.equals(OK)){
                            Log.e(TAG,API不存在);
                            Log.e(TAG,API不);                        }其他{                        }                        JsonArray jsonPostArray =结果
                                .getAsJsonArray(上岗);
                        INT I = 0;
                        对于(JsonElement jsonElementPost:jsonPostArray){
                            Log.d(I +);
                            我++;
                            JSONObject的jsonPost = jsonElementPost
                                    .getAsJsonObject();
                            INT ID = jsonPost.get(ID)getAsInt()。
                            。字符串URL = jsonPost.get(URL)符getAsString();
                            。标题= jsonPost.get(题)符getAsString();
                            titles.add(职称);
                            。内容= jsonPost.get(内容)符getAsString();                            字符串日期= jsonPost.get(DATE)符getAsString()。                            尝试{
                                JSONObject的猫= jsonPost
                                        .getAsJsonArray(类)。得到(0)
                                        .getAsJsonObject();
                                字符串catTitle = cat.get(题)
                                        .getAsString();
                            }赶上(例外E2){
                                // TODO自动生成catch块
                                e2.printStackTrace();
                            }                            笔者的JSONObject = jsonPost
                                    .getAsJsonObject(作家);
                            字符串名称= author.get(name)的符getAsString()。
                            在JSONObject的= NULL;
                            尝试{
                                在= jsonPost.getAsJsonArray(附件)
                                        获得(0).getAsJsonObject();
                                。atUrl = at.get(URL)符getAsString();
                                字符串atMime = at.get(MIME_TYPE)
                                        .getAsString();                            }赶上(例外E1){
                                // TODO自动生成catch块
                                e1.printStackTrace();
                                atUrl =;                            }                        }                        // Log.d(TAG,ID +);
                        //完成();
                    }
                });
        返回true;
    }}


解决方案

  

我敢肯定,我只是做一些小的,以至于使这个不正确触发。


让我们回顾一下文档中的用法一节的大标题:


  • 构造:在您呼叫的构造你的 EndlessAdapter 子类


  • 占位符:您已实现 getPendingView() EndlessAdapter 子类


  • 的加载:您已实现 cacheInBackground() EndlessAdapter 子类,而你的实现是怪(三级的ArrayList&LT;弦乐&GT; 而不是一个的ArrayList&LT; WhateverYourActualModelObjectShouldBe&GT; ),它可能适用


  • 的附加 BZZZT - 你的 appendCachedData()不会做任何事情


  • 线程:在您的code应该罚款,只要你没有尝试使用其他 AsyncTasks 在同一时间,或者如果你的的android:targetSdkVersion 是在13


  • 压倒一切的:在您的code应该罚款


所以,我建议你回去的附加,阅读通过部分,偷看了演示程序的执行情况。你可能会更复杂,因为您选择使用 BaseAdapter ,而不是 ArrayAdapter ,尽管你的数据模型是一个数组。

I'm having a tough time understanding the documentation for endless adapter

I tried following these quick steps by Commonsware to no avail: How can I use a custom adapter with the cwac-endless adapter?

So, first, you get your custom adapter working correctly on its own. Then, you create your EndlessAdapter subclass (with your implementation of cacheInBackground(), etc.), wrap your custom adapter in an instance of your EndlessAdapter subclass, and put your instance of your EndlessAdapter subclass in your ListView

I have three java classes. My activity, my regular adapter (that works before I tried to integrate endless adapter), and I have a subclassed endless adapter. I'm sure I'm just doing something small that is making this not trigger properly. My app does NOT crash, and it SHOWS my "Loading" view, it just never updates, and seems to load a lot of stuff afterwards because the gui really slows down. I'm probably stuck in some kind of loop. Any ideas?

My main activity:

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;

public class MyEndlessAdapter extends EndlessAdapter { ArrayList tempList = new ArrayList(); protected String title; protected ArrayList titles; protected String content; protected String atUrl; ArrayAdapter oldadapter; int page = 1, count = 20; Context context;

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;
    }

}

解决方案

I'm sure I'm just doing something small that is making this not trigger properly.

Let's review the major headings of the "Usage" section of the documentation:

  • Constructors: you are calling the constructor on your EndlessAdapter subclass

  • Placeholder: you have implemented getPendingView() on your EndlessAdapter subclass

  • The Loading: you have implemented cacheInBackground() on your EndlessAdapter subclass, and while your implementation is strange (three ArrayList<String> rather than one ArrayList<WhateverYourActualModelObjectShouldBe>?), it probably works

  • The Attaching: BZZZT -- your appendCachedData() does not do anything

  • The Threading: your code should be fine, so long as you are not trying to use other AsyncTasks at the same time, or if your android:targetSdkVersion is under 13

  • The Overriding: your code should be fine

So, I would recommend that you go back to The Attaching, read through that section, and peek at the demo app's implementation. Yours may be more complicated, as you chose to use BaseAdapter rather than ArrayAdapter, despite your data model being an array.

这篇关于EndlessAdapter不与Android的CWAC-EndlessAdapter更新更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆