0

我有一个异步任务,它从数据库的本地主机响应中填充列表视图。如何仅在添加新项目时更新列表视图。我使用计时器来更新列表视图,但它不会让我向上滚动,因为它一直强制将滚动条推到底部。

class MessageTask extends AsyncTask<String, Void, List<String>> {

             private final HttpClient Client = new DefaultHttpClient();

            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            protected List<String> doInBackground(String... params) {

                String output = "";
               for(String out:params){
                   list.removeAll(list);
                   //Collections.reverse(list);
                     try{
                     HttpGet httpget = new HttpGet(out);
                     ResponseHandler<String> responseHandler = new BasicResponseHandler();
                     output = Client.execute(httpget, responseHandler);

                       try {


                        JSONObject jObject= new JSONObject(output);
                        JSONArray menuObject = new JSONArray(jObject.getString("response"));   

                        //int len=menuObject.length();


                        //HashMap<String,ArrayList> map = new HashMap<String,ArrayList>();

                     for (int i = menuObject.length()-1; i>=0; i--)
                      {

                         list.add(menuObject.getJSONObject(i).getString("fk_username_c").toString()+" "+menuObject.getJSONObject(i).getString("message_c").toString());     
                        // if(i==len){
                        // newtc=menuObject.getJSONObject(i).getString("timestamp_c").toString();
                         //}
                     }
                     /* if(oldtc==null){
                          oldtc=newtc;
                      }if
                      else{


                      }*/
                      adapter=new ArrayAdapter<String>(ChatActivity.this,android.R.layout.simple_list_item_1);

                       } catch (JSONException e) {
                           Log.e("log_tag", "Error parsing data ");
                       }
                     }catch(Exception e){
                         Log.i("Animation", "Thread  exception " );
                     }
               }

              return list;

            }

      @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override      
    protected void onPostExecute(List<String> list) {


            //if(!oldtc.equals(newtc)){       
              listview.setAdapter(adapter);
          adapter.clear();
          listview.clearTextFilter();
          adapter.addAll(list);
          adapter.notifyDataSetChanged();
            //}


    }
     }

请帮助我如何更新列表视图只有新数据插入数据库

4

5 回答 5

2

添加新项目时,只需调用adapter.NotifyDataSetChanged()

于 2013-10-17T10:12:17.050 回答
0
adapter.notifyDataSetChanged();
于 2013-10-17T10:12:40.853 回答
0

setAdapter之后添加addAll

      adapter.clear();
      listview.clearTextFilter();
      adapter.addAll(list);
      listview.setAdapter(adapter);
      adapter.notifyDataSetChanged();
于 2013-10-17T10:13:41.887 回答
0

使用广播,每当新响应来自服务器时,广播此响应并在要更新列表视图的活动中注册广播接收器。

于 2013-10-17T10:20:42.163 回答
0

您不需要清除适配器阵列。只需更新适配器数组(添加或删除项目),然后调用.notifyDataSetChanged()

于 2013-10-17T10:24:53.943 回答