Android分页无限滚动列表视图无法正常工作

5
无限滚动视图在列表视图中无法工作,列表视图项从Web服务加载,每页显示10个项目。列表视图应该像分页无限列表视图一样动态地从Web服务加载。我是Android的新手,有人能帮助我朝正确的方向前进吗?以下是代码:
 public class MainActivity extends ListActivity   {


   ListView list;
   LazyAdapter adapter;
   JSONArray posts;

// All static variables
static final String URL = "website";


 static final String KEY_POSTS = "posts";
 static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_CONTENT = "content";
static final String KEY_AUTHOR = "author";
static final String KEY_NAME = "name";
static final String KEY_ATTACHMENTS = "attachments";
static final String KEY_SLUG = "slug";
static final String KEY_THUMB_URL = "thumbnail";
static final String KEY_IMAGES = "images";
static final String KEY_URL = "url";




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    final Handler handler = new Handler();
    Runnable runable = new Runnable() {

        @Override
        public void run() {

            //call the function
            LoadData();
            //also call the same runnable
            handler.postDelayed(this, 40000);
        }
    };
    handler.postDelayed(runable, 10);

}public void LoadData(){

     ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();


        // Creating JSON Parser instance
                JSONParser jParser = new JSONParser();

                // getting JSON string from URL
                JSONObject json = jParser.getJSONFromUrl(URL);
                try {
                    JSONArray posts = json.getJSONArray(KEY_POSTS);

        // looping through all song nodes <song>
                for(int i = 0; i < posts.length(); i++){
                    JSONObject c = posts.getJSONObject(i);
                    // Storing each json item in variable
                    String id = c.getString(KEY_ID);
                    String title = c.getString(KEY_TITLE);
                    String date = c.getString(KEY_DATE);
                    String content = c.getString(KEY_CONTENT);
                    // to remove all <P> </p> and <br /> and replace with ""
                     content = content.replace("<br />", "");
                     content = content.replace("<p>", "");
                     content = content.replace("</p>", "");

                    //authornumber is agin  JSON Object
                    JSONObject author = c.getJSONObject(KEY_AUTHOR);
                    String name = author.getString(KEY_NAME);

                    String url = null;
                    String slug = null;
                    try {
                    JSONArray atta = c.getJSONArray("attachments");
                    for(int j = 0; j < atta.length(); j++){
                        JSONObject d = atta.getJSONObject(j);

                        slug = d.getString(KEY_SLUG);

                        JSONObject images = d.getJSONObject(KEY_IMAGES);

                        JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
                        url = thumbnail.getString(KEY_URL);

                    }
                    } catch (Exception e) {
                        e.printStackTrace();

                    }




            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(KEY_ID, id);
            map.put(KEY_TITLE, title);
            map.put(KEY_DATE, date);
            map.put(KEY_NAME, name);
            map.put(KEY_CONTENT, content);
            map.put(KEY_SLUG, slug);
            map.put(KEY_URL, url);


            // adding HashList to ArrayList
            songsList.add(map);
                }   
                }catch (JSONException e) {
                    e.printStackTrace();

                    }

        ListView  list=(ListView)findViewById(android.R.id.list);

        // Getting adapter by passing json data ArrayList
        adapter=new LazyAdapter(this, songsList);        
        list.setAdapter(adapter);



    // Launching new screen on Selecting Single ListItem
    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
            String date = ((TextView) view.findViewById(R.id.date)).getText().toString();
            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String content = ((TextView) view.findViewById(R.id.content)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(),SampleDesp.class);
            in.putExtra(KEY_TITLE, title);
            in.putExtra(KEY_DATE, date);
            in.putExtra(KEY_NAME, name);
            in.putExtra(KEY_CONTENT, content);
            startActivity(in);

        }
    });


    list.setOnScrollListener(new OnScrollListener(){

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onScrollStateChanged(AbsListView view,
                int scrollState) {
            // TODO Auto-generated method stub

        }

       });
}
  }
1个回答

0

你的方法load()总是返回false,它永远不会执行(new LoadNextPage()).execute("");


我的问题类似于http://stackoverflow.com/questions/10204698/endless-scrolling-for-listview,所以我编辑了我的代码,请你帮忙检查一下。 - user1950395

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接