2016-11-28 74 views
0

我正在製作具有滾動偵聽器的應用程序,它可以從多個URL添加數據 但是當我滾動列表跳轉到第一個位置並加載URL時我知道適配器在我的同步任務得到執行每次我打開新的URL任務時,我加載新的項目,但我不知道如何解決它Listview在添加新項目時跳轉到頂部

public class jsontask extends AsyncTask<String, String, List<newsmodel>> { 


    @Override 
    protected List<newsmodel> doInBackground(String... params) { 
     BufferedReader reader = null; 
     HttpURLConnection connection = null; 
     try { 
      URL url = new URL(params[0]); 

      connection = (HttpURLConnection) url.openConnection(); 
      if (connectiondetector.isConnected()) { 
       connection.addRequestProperty("Cache-Control", "max-age=0"); 
      } else { 
       moviemodelList.addAll((List<newsmodel>) cacheThis.readObject(
         technology.this, fileName)); 

      } 
      connection.connect(); 
      InputStream stream = connection.getInputStream(); 
      reader = new BufferedReader(new InputStreamReader(stream)); 
      StringBuffer buffer = new StringBuffer(); 
      String line = ""; 
      while ((line = reader.readLine()) != null) { 
       buffer.append(line); 

      } 


      String finaljson = buffer.toString(); 
      JSONObject parentobject = new JSONObject(finaljson); 


      JSONArray parentarray = parentobject.getJSONArray("articles"); 

      for (int i = 0; i < parentarray.length(); i++) { 
       JSONObject finalobject = parentarray.getJSONObject(i); 
       newsmodel newsmodel = new newsmodel(); 
       newsmodel.setAuthor(finalobject.getString("author")); 
       if (finalobject.isNull("author")) { 

       } 
       newsmodel.setDescription(finalobject.getString("description")); 
       newsmodel.setTitle(finalobject.getString("title")); 
       newsmodel.setImage(finalobject.getString("urlToImage")); 
       newsmodel.setUrl(finalobject.getString("url")); 

       newsmodel.setPublishedAt(finalobject.getString("publishedAt")); 
       cacheThis.writeObject(technology.this, fileName, moviemodelList); 
       moviemodelList.add(newsmodel); 

      } 


      return moviemodelList; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
      return moviemodelList; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return moviemodelList; 
     } catch (JSONException e) { 

     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } finally { 
      if (connection != null) { 


      } 
      try { 
       if (reader != null) { 
        reader.read(); 

       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 


     return moviemodelList; 
    } 


    @Override 
    protected void onPostExecute(List<newsmodel> result) { 


     super.onPostExecute(result); 
     newsadapter adapter = new newsadapter(getApplicationContext(), R.layout.row, result); 
     lvnews.setAdapter(adapter); 
    } 

} 


public class newsadapter extends ArrayAdapter { 
    private List<newsmodel> moviemodelList; 
    private int resource; 
    private LayoutInflater inflater; 

    public newsadapter(Context context, int resource, List<newsmodel> objects) { 
     super(context, resource, objects); 
     moviemodelList = objects; 
     this.resource = resource; 
     inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 


    }` 

,這是我怎麼EXCUTE我的一個同步任務

protected void onStart() { 
    super.onStart(); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    moviemodelList.clear(); 
    new jsontask().execute("https://newsapi.org/v1/articles?source=engadget&sortBy=top&apiKey=ade8f00a634b4825a028837ec107afae"); 
    lvnews.setOnScrollListener(new EndlessScrollListener() { 
     @Override 
     public boolean onLoadMore(int page, int totalItemsCount) { 

      new jsontask().execute("url1"); 
      new jsontask().execute("url2"); 
      new jsontask().execute("url3"); 
      new jsontask().execute("url4"); 
      new jsontask().execute("url5"); 
      new jsontask().execute("url6"); 
      return false; 
     } 
    }); 

} 

回答

1

從onPostExecute

newsadapter adapter = new newsadapter(getApplicationContext(), R.layout.row, result); 
lvnews.setAdapter(adapter); 

您正在執行doInBackground後設置適配器eveytime刪除這些行。你只需要做一次。可以做到這一點在的onCreate

在一個機器人的AsyncTask類中所用的基本方法定義如下

doInBackground():此方法包含需要在後臺執行的代碼。在這個方法中,我們可以通過publishProgress()方法向UI線程發送多次 次結果。以通知 後臺處理已經完成,我們只需要使用 return語句

onPreExecute():此方法包含前的後臺處理開始

onPostExecute被執行的代碼() :在doInBackground方法完成處理後調用此方法。從doInBackground結果傳遞給 此方法

onProgressUpdate():這個方法接收從doInBackground方法,其經由publishProgress方法, 出版進度更新,並且此方法可以使用這個進度更新來更新UI線程

請參閱本link知道

+0

如何異步任務謝謝它的工作,但我必須去另一個活動,比回去的主要活動,使之出現的消息如何解決這個 –

+0

add adapter.notifyItemChanged(moviemodelList.size());在moviemodelList.add(newsmodel)之後;在doInBackground – Eswar

+0

謝謝它的工作 –