清除()ArrayAdapter工作不正常 - Android电子 [英] Clear( ) ArrayAdapter not working properly - Android
问题描述
我有一个从AsyncTask的收集信息的ArrayAdapter。该AsyncTask的由上滚动监听器触发。当用户滚动到内容的AsyncTask的底部被触发并加载更多的内容。问题是我实现了一个复位按钮。这样做是清除一个ArrayAdapter并重置那些通过JSON发送到PHP脚本,收集的信息的标记。复位按钮根本不工作。当我打电话清晰()有时会清除列表,然后添加新的数据却对onScrollListener不工作或不清除列表,只是增加了新的数据到列表的顶部。这里是我的code,这一切code是在同一个活动。
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.
全局类发送到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聚集。当Reset按钮被点击它重置CommentID为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的一个集评论和使用全局变量
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是我尝试和明确的,但它永远不会奏效了权利。
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.
这个是开启SCROLL侦听器激活的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,诠释visibleItemCount,诠释totalItemCount
int firstVisibleItem,int visibleItemCount, int totalItemCount
这是怎么了我RESET一个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();
}
}
});
自定义适配器是我的适配器的名称和youListView是我的ListView的名字。请人帮忙解释为什么适配器不明确的,如果它的onscrolllistener停止工作。
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.
推荐答案
在调用后 Adapter.clear()
您需要调用 Adapter.notifyDataSetChanged ()
基本上只要备份适配器的数据发生变化,需要调用 Adapter.notifyDataSetChanged()
反思的ListView的变化
。
Basically whenever the data backing the adapter changes, you need to call Adapter.notifyDataSetChanged()
to reflect the changes on the ListView
.
这篇关于清除()ArrayAdapter工作不正常 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!