我有一个从滚动监听器触发的信息ArrayAdapter
中收集信息。当用户滚动到内容的底部时,会触发并加载更多内容。问题是我已经实现了一个重置按钮。这样做是清除并重置通过 JSON 发送的令牌到收集信息的 php 脚本。重置按钮根本不起作用。当我调用它时,有时会清除列表然后添加新数据,但随后不起作用或它不清除列表,只是将新数据添加到列表顶部。这是我的代码,所有这些代码都在同一个活动中。AsyncTask.
AsyncTask
AsyncTask
ArrayAdapter
clear()
onScrollListener
发送到 PHP 脚本的令牌的全局类
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");
此类包含全局变量。CommentID 设置为 0。这通过 Json 发送到 php 脚本。在它收集到一组评论后,它会将 CommentID 重置为最后收集到的评论的 ID。单击重置按钮时,它将 CommentID 重置为 0,就像活动刚刚开始一样。
收集评论和使用全局变量的异步任务
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
}
}
收集Asynctask
多个项目并将它们放入一个ArrayAdapter.
这ArrayAdapter
是我尝试清除的,但它永远不会正确。
这是激活异步任务的滚动监听器
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) {
}
}
我相信问题的发生是因为这个类中的某些东西。我不确定我相信它在那里。也许是因为下面的变量没有重置或其他什么。
int firstVisibleItem,int visibleItemCount, int totalItemCount
这就是我重置阵列适配器的方法
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 是我的适配器的名称,youListView 是我的名称ListView.
请有人帮忙解释为什么适配器不清除以及它是否OnScrollListener
停止工作。