How to load more items in a ListView using AsyncTask or any other method

前端 未结 2 1175
小鲜肉
小鲜肉 2021-01-27 10:09

I am very new to android development and I know that this question might be answer before but I can\'t seem to find a suitable answer for my situation. I am creating an android

2条回答
  •  心在旅途
    2021-01-27 10:27

    try like this:

     //used for populate the listView
     private void populateListView(HashMap datas){
        if(mRecipeAdapter ==null){
           String[] keys = {KEY_TITLE};
           int[] ids = {R.id.list_recipe_title};
           mRecipeAdapter = new ExtendedSimpleAdapter(getContext(), datas, R.layout.itemlistrow, keys, ids);
           listView.setAdapter(mRecipeAdapter);
        }else
        {
          mRecipeAdapter.notifyDataSetChanged();
        }
     }
    //create ListView Data::: i have removed your five last line, and repleced them by return mRecipePostData 
    private ArrayList> getRecipeData() throws JSONException {
            JSONArray jsonPosts = mRecipeData.getJSONArray("posts");
            mRecipePostData = new ArrayList>();
            for (int i = 0; i < jsonPosts.length(); i++){
                JSONObject post = jsonPosts.getJSONObject(i);
                String title = post.getString(KEY_TITLE);
                title = Html.fromHtml(title).toString();
                String author = post.getJSONObject(KEY_AUTHOR).getString("name");
                author = Html.fromHtml(author).toString();
                String imgUrl = post.getJSONObject(KEY_IMG_URL).getJSONObject("full").getString("url");
                String recipeContent = post.getString(KEY_CONTENT);
                recipeContent = Html.fromHtml(recipeContent).toString();
                String recipeUrl = post.getString(KEY_RECIPE_URL);
    
                HashMap singleRecipePost = new HashMap<>();
                singleRecipePost.put(KEY_TITLE, title);
                singleRecipePost.put(KEY_AUTHOR, author);
                singleRecipePost.put(KEY_IMG_URL, imgUrl);
                singleRecipePost.put(KEY_CONTENT, recipeContent);
                singleRecipePost.put(KEY_RECIPE_URL, recipeUrl);
    
                mRecipePostData.add(singleRecipePost);
            }
           return mRecipePostData;
        } 
    
    //after getData, i am populating ListView
    private void handleRecipeData() {
            mProgressBar.setVisibility(View.INVISIBLE);
            if(mRecipeData == null){
                handleErrors();
    
            }else {
                try {
                    HashMap datas=getRecipeData();
                    populateListView(datas);
    
                } catch (JSONException e) {
                    Log.e(LOG_TAG,"Exception Caught: ",e);
                }
            }
        }
    

提交回复
热议问题