I have a ArrayAdapter
that gathers information from an AsyncTask.
The AsyncTask
is triggered by an on scroll listener. When the user scrolls to the bottom of the content the AsyncTask
is triggered and loads more content. The problem is I have implemented a reset button. What this does is clears the ArrayAdapter
and resets the tokens that are sent through JSON to a php script that gathers the information. The reset button doesn't work at all. When I call clear()
it sometimes clears the list and then adds new data but then the onScrollListener
doesn't work or it doesn't clear the list and just adds new data to the top of the list. Here is my code, all of this code is in the same activity.
GLOBAL CLASS FOR TOKENS SENT TO PHP SCRIPT
class CommentInfo {
public CommentInfo() {}
private String commentID;
private int gatheredComments;
private int totalComments;
public String getCommentID(){
return commentID;
}
public void setCommentID(String c){
commentID = c;
}
public int getGatheredComments(){
return gatheredComments;
}
public void setGatheredComments(int forNumber){
gatheredComments = forNumber;
}
}//end commentInfor class
final CommentInfo info = new CommentInfo();
info.setCommentID("0");
This class contains Global Variables. CommentID is set to 0. This is sent to a php script via Json. After it gathers a group of comments it resets the CommentID to the ID of the last comment gathered. When the Reset button is clicked it resets the CommentID to 0, as if the Activity has just began.
ASYNCTASK THAT GATHERS COMMENTS AND USES GLOBAL VARIABLES
class loadComments extends AsyncTask<JSONObject, String, JSONObject> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progDailog.getWindow().setGravity(Gravity.BOTTOM);
progDailog.setIndeterminate(false);
progDailog.setCancelable(false);
progDailog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
progDailog.show();
progDailog.setContentView(R.layout.progress_circle);
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
protected JSONObject doInBackground(JSONObject... params) {
JSONObject json2 = CollectComments.collectComments(usernameforcomments, info.getCommentID());
return json2;
}
@Override
protected void onPostExecute(JSONObject json2) {
progDailog.dismiss();
try {
if (json2.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res2 = json2.getString(KEY_SUCCESS);
if(Integer.parseInt(res2) == 1){
JSONArray commentArray = json2.getJSONArray(KEY_COMMENT);
String comments[] = new String[commentArray.length()];
for ( int i=0; i<commentArray.length(); i++ ) {
comments[i] = commentArray.getString(i);
}
JSONArray contentsArray = json2.getJSONArray(KEY_CONTENT);
String contents[] = new String[contentArray.length()];
for ( int i=0; i<contentArray.length(); i++ ) {
contents[i] = contentArray.getString(i);
}
JSONArray usernameArray = json2.getJSONArray(KEY_USERNAME);
String usernames[] = new String[usernameArray.length()];
for ( int i=0; i<usernameArray.length(); i++ ) {
usernames[i] = usernameArray.getString(i);
}
final List <Item> tempList2 = new ArrayList <Item>();
String tempForNumber = json2.getString(KEY_COMMENTS_GATHERED);
int forNumber = Integer.parseInt(tempForNumber);
for (int x = 0; x < forNumber;x++){
tempList2.add (new Item (usernames [x], contents[x], comments[x]));
}
customAdapter.addAll(tempList2);
String commentID = json2.getString(KEY_COMMENT_ID);
info.setCommentID(commentID);
info.setGatheredComments(forNumber);
if (info.getTotalComments() >= Integer.parseInt(json2.getString(KEY_NUMBER_OF_COMMENTS))){
int tempNum = Integer.parseInt(json2.getString(KEY_NUMBER_OF_COMMENTS));
info.setTotalComments(tempNum);
}
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
}
}//end if key is == 1
else{
// Error in registration
registerErrorMsg.setText(json2.getString(KEY_ERROR_MSG));
}//end else
}//end if
} //end try
catch (JSONException e) {
e.printStackTrace();
}//end catch
}
}
The Asynctask
gathers multiple items and places them into an ArrayAdapter.
This ArrayAdapter
is what I try and clear but it never works out right.
THIS IS THE ON SCROLL LISTENER THAT ACTIVATES THE ASYNCTASK
class EndlessScrollListener implements OnScrollListener {
private int visibleThreshold = 5;
private boolean loading = true;
public EndlessScrollListener() {
}
public EndlessScrollListener(int visibleThreshold) {
this.visibleThreshold = visibleThreshold;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (firstVisibleItem + visibleItemCount) == totalItemCount) {
new loadComments().execute();
loading = true;
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
}
I believe the problem is occurring because of something in this class. I am not sure what I believe its in there. Maybe because the variables below aren't resetting or something.
int firstVisibleItem,int visibleItemCount, int totalItemCount
THIS IS HOW I RESET THE ARRAYADAPTER
refresh = (ImageView) findViewById(R.id.refresh);
refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(customAdapter!= null)
{
customAdapter.clear();
info.setCommentID("0");
info.setGatheredComments(0);
yourListView.smoothScrollToPosition(0);
new loadComments().execute();
}
}
});
custom Adapter is the name of my adapter and youListView is the name of my ListView.
Please someone help to explain why the adapter doesn't clear and if it does the OnScrollListener
ceases to work.