Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clear()ArrayAdapter工作不正常-Android_Android_Listview_Android Asynctask_Global Variables_Android Arrayadapter - Fatal编程技术网

Clear()ArrayAdapter工作不正常-Android

Clear()ArrayAdapter工作不正常-Android,android,listview,android-asynctask,global-variables,android-arrayadapter,Android,Listview,Android Asynctask,Global Variables,Android Arrayadapter,我有一个ArrayAdapter从AsyncTask>收集信息。异步任务AsyncTask由滚动侦听器触发。当用户滚动到内容底部时,将触发AsyncTask,并加载更多内容。问题是我实现了一个重置按钮。这样做的目的是清除ArrayAdapter,并将通过JSON发送的令牌重置为收集信息的php脚本。重置按钮根本不起作用。当我调用clear()时,它有时会清除列表,然后添加新数据,但是onScrollListener不起作用,或者它不清除列表,只是将新数据添加到列表顶部。这是我的代码,所有这些代码

我有一个
ArrayAdapter
AsyncTask>收集信息。
异步任务
AsyncTask
由滚动侦听器触发。当用户滚动到内容底部时,将触发
AsyncTask
,并加载更多内容。问题是我实现了一个重置按钮。这样做的目的是清除
ArrayAdapter
,并将通过JSON发送的令牌重置为收集信息的php脚本。重置按钮根本不起作用。当我调用
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


                }


            }
我相信这个问题的出现是因为这门课上的某些东西。我不确定我在那里相信什么。可能是因为下面的变量没有重置或其他原因

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();

                    }

                }
            });

自定义适配器是我的适配器的名称,youListView是我的
列表视图的名称。
请有人帮助解释适配器不清除的原因,如果清除,OnScrollListener将停止工作。

调用
Adapter.clear()
后需要调用
Adapter.notifyDataSetChanged()


基本上,每当支持适配器的数据发生更改时,您都需要调用
adapter.notifyDataSetChanged()
,以反映调用
adapter.clear()
列表视图上的更改


基本上,只要支持适配器的数据发生更改,您就需要调用
adapter.notifyDataSetChanged()
,以反映
列表视图上的更改。似乎在
List.clear()
ListAdapter.notifyDataSetChanged()之后重新实例化和附加滚动侦听器解决了这个问题


可能侦听器在
List.clear()

刚刚遇到类似问题后被丢弃。似乎在
List.clear()
ListAdapter.notifyDataSetChanged()之后重新实例化和附加滚动侦听器解决了这个问题


可能侦听器在
List.clear()

之后被废弃,如果您以前进行过任何筛选,则需要调用
Adapter.getFilter().filter(“”)调用前
适配器.notifyDataSetChanged()

如果您以前进行过任何筛选,则需要调用
适配器.getFilter().filter(“”)调用前
适配器.notifyDataSetChanged()

有时不起作用。还可以从视图中删除所有项目。然后重新填充

    final int adapterCount = adapter.getCount();

    for (int i = 0; i < adapterCount; i++) {
        View item = adapter.getView(i, null, null);
        myLayout.removeView(item);
    }
final int adapterCount=adapter.getCount();
对于(int i=0;i
有时这不起作用。还可以从视图中删除所有项目。然后重新填充

    final int adapterCount = adapter.getCount();

    for (int i = 0; i < adapterCount; i++) {
        View item = adapter.getView(i, null, null);
        myLayout.removeView(item);
    }
final int adapterCount=adapter.getCount();
对于(int i=0;i
在调用asynctask时尝试删除listview的OnScrollListener,并在asynctask完成其作业时将其设置回原位。

在调用asynctask时尝试删除listview的OnScrollListener,并在asynctask完成其作业时将其设置回原位。

我已完成此操作,并且它确保listview已清除,但是,onscrollListener不起作用。如果列表为空,则无需滚动。这不是很明显。实际上我把代码
newloadcomments().execute(),因此它会向listView添加新数据。它添加的数据与活动刚刚启动但onScrollListener不工作时的数据相同。这就好像ArrayAdapter的内容仍然存在。当你说“onScrollListener不工作”时,你的确切意思是什么。您希望它做什么>?它假设在到达ListView底部时调用AsyncTask。它计算listView中有多少项,然后可以检测最后一个视图何时显示。然后调用AsyncTask。问题是它不知道我清除了列表,并将其他视图作为总视图数的一部分,因此它不会检测到我位于列表视图的底部。我这样做了,它确保了列表视图被清除,但onscrollListener无法正常工作。如果列表为空,则没有可滚动的内容。这不是很明显。实际上我把代码
newloadcomments().execute(),因此它会向listView添加新数据。它添加的数据与活动刚刚启动但onScrollListener不工作时的数据相同。这就好像ArrayAdapter的内容仍然存在。当你说“onScrollListener不工作”时,你的确切意思是什么。您希望它做什么>?它假设在到达ListView底部时调用AsyncTask。它计算listView中有多少项,然后可以检测最后一个视图何时显示。然后调用AsyncTask。问题是它不知道我清除了列表并统计了其他视图