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
try like this:
//used for populate the listView
private void populateListView(HashMap<String, String> 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<HashMap<String,String>> getRecipeData() throws JSONException {
JSONArray jsonPosts = mRecipeData.getJSONArray("posts");
mRecipePostData = new ArrayList<HashMap<String,String>>();
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<String, String> 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<String, String> datas=getRecipeData();
populateListView(datas);
} catch (JSONException e) {
Log.e(LOG_TAG,"Exception Caught: ",e);
}
}
}
Do not set adapter every time it will show you new data all the time when you will call for data. Just set it first time then only notify the adapter when you received new data from JSON.
Or you can use another List to store new data and add this list to your main List -
yourMainList.addAll(anotherList);
adapter.notifyDataSetChanged();
UPDATES--
1- Take a boolean value to check list is scrolling or not boolean iScrolling = false and make it true inside onScroll()-
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0) {
isScrolling = true;
mFooter.setVisibility(View.VISIBLE);
/*GetRecipeData getRecipeData = new GetRecipeData();
getRecipeData.execute();*/
GetRecipeData getRecipeData = new GetRecipeData();
getRecipeData.execute(yourCounts); // update
}
}
});
Now some changes inside getRecipeData()-
private void 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<String, String> 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);
}
String[] keys = {KEY_TITLE};
int[] ids = {R.id.list_recipe_title};
if (!isScrolling){
mRecipeAdapter = new ExtendedSimpleAdapter(getContext(), mRecipePostData, R.layout.itemlistrow, keys, ids);
listView.setAdapter(mRecipeAdapter);
mRecipeAdapter.notifyDataSetChanged();
}else{
mRecipeAdapter.notifyDataSetChanged();
isScrolling = false;
}
}
2- Or you can do it with the help of another List- Take another List and add data on it and add this List to your main List, inside getRecipeData()-
private void getRecipeData() throws JSONException {
JSONArray jsonPosts = mRecipeData.getJSONArray("posts");
if (!isScrolling) {
mRecipePostData = new ArrayList<>();
}else{
yourSecondList = 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<String, String> 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);
if (!isScrolling) {
mRecipePostData.add(singleRecipePost);
}else{
yourSecondList.add(singleRecipePost);
}
}
String[] keys = {KEY_TITLE};
int[] ids = {R.id.list_recipe_title};
if (!isScrolling){
mRecipeAdapter = new ExtendedSimpleAdapter(getContext(), mRecipePostData, R.layout.itemlistrow, keys, ids);
listView.setAdapter(mRecipeAdapter);
mRecipeAdapter.notifyDataSetChanged();
}else{
mRecipePostData.addAll(yourSecondList);
mRecipeAdapter.notifyDataSetChanged();
isScrolling = false;
}
}
UPDATES-
Change your AsyncTask params-
public class GetRecipeData extends AsyncTask<String, Void, JSONObject> {
// your code..
@Override
protected JSONObject doInBackground(String... params) {
try {
URL blogFeedUrl = new URL("http://www.bestfoodrecipesever.com/api/get_category_posts/?slug=" + RECIPE_CAT + "&count=" + params[0]);
// your code...
}
}
}
And also make some change here-
if(isNetworkAvailable()) {
mProgressBar.setVisibility(View.VISIBLE);
GetRecipeData getRecipeData = new GetRecipeData();
getRecipeData.execute(yourCount);
} else {
Toast.makeText(getContext(),getString(R.string.no_network), Toast.LENGTH_LONG).show();
}
Hope it will help.