2016-12-31 1 views
0

私はサーバーからデータを読み込むリストビューを持っています。データは正しく読み込まれますが、しばらくしてリストをスクロールするとアプリケーションがクラッシュし、「データは受信されましたがアダプタは通知されません」というメッセージが表示されます。以下は私のコードです:Android Listview notify

private class LoadData extends AsyncTask<Void, Void, Void> { 


    @Override 
    protected Void doInBackground(Void... voids) { 
     applicationList = new ArrayList(); 
     applicationList.clear(); 
     try { 
      JSONArray jsonData = new GetListviewsData().getJSONData(webfileName, limit, offset, priceCat); 

      for (int i = 0; i <= jsonData.length() - 2; i++) { 
       JSONObject c = jsonData.getJSONObject(i); 

       id = c.getString("id"); 
       name = c.getString("name"); 
       logo = c.getString("logo"); 
       developer = c.getString("developer"); 
       category = c.getInt("category"); 
       fileName = c.getString("filename"); 
       path = c.getString("path"); 
       appSize = c.getDouble("size_bytes"); 
       price = c.getInt("price"); 
       data = c.getString("data1").equals("t"); 
       obb = c.getString("data").equals("t"); 
       applicationList.add(new ApplicationPojo(id, name, logo, developer, appSize, category, fileName, path, data, obb, price)); 
      } 
      JSONObject sizeObj = jsonData.getJSONObject(jsonData.length() - 1); 
      listSize = sizeObj.getInt("size"); 
     } catch (Exception ex) { 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 

     if(listSize == 0){ 
      TextView noResult = (TextView) findViewById(R.id.noresult); 
      noResult.setVisibility(View.VISIBLE); 
      mProgressDialog.dismiss(); 

     }else{ 
      adapter = new ListViewAppAdapter(ListViewAppAll.this, applicationList, listview); 
      listview.setAdapter(adapter); 
      mProgressDialog.dismiss(); 

      listview.setOnScrollListener(new AbsListView.OnScrollListener() { 

       @Override 
       public void onScrollStateChanged(AbsListView view, int scrollState) { 
        int threshold = 1; 
        int count = listview.getCount(); 

        if (scrollState == SCROLL_STATE_IDLE) { 
         if (listview.getLastVisiblePosition() >= count - threshold) { 
          if (listSize >= offset) { 
           offset = offset + 15; 
           new LoadMoreData().execute(); 

          } else { 
           Toast.makeText(getApplicationContext(), "End of list!", Toast.LENGTH_LONG).show(); 
           listview.removeFooterView(footer); 
          } 
         } 
        } 
       } 

       @Override 
       public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
        // TODO Auto-generated method stub 
       } 
      }); 
     } 
    } 
} 

private class LoadMoreData extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected Void doInBackground(Void... voids) { 
     try { 
      GetListviewsData getDataAppList = new GetListviewsData(); 

      JSONArray jsonData = getDataAppList.getJSONData(webfileName, limit, offset, priceCat); 

      for (int i = 0; i <= jsonData.length(); i++) { 
       JSONObject c = jsonData.getJSONObject(i); 

       id = c.getString("id"); 
       name = c.getString("name"); 
       logo = c.getString("logo"); 
       developer = c.getString("developer"); 
       category = c.getInt("category"); 
       fileName = c.getString("filename"); 
       path = c.getString("path"); 
       appSize = c.getDouble("size_bytes"); 
       price = c.getInt("price"); 
       data = c.getString("data1").equals("t"); 
       obb = c.getString("data").equals("t"); 
       applicationList.add(new ApplicationPojo(id, name, logo, developer, appSize, category, fileName, path, data, obb, price)); 
      } 
     } catch (Exception ex) { 

     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     adapter.notifyDataSetChanged(); 
    } 
} 

私はこれで助けてください。ありがとう

+0

毎回コードを参照してください。 adapter.notifyDatasetChanged();を使用します。 –

+0

返信いただきありがとうございます!アダプタでonPostExecuteメソッド – Shoaib

+0

がコード内で通知されたときは、その1回だけ使用しました。データを複数回設定する –

答えて

1

コードにいくつかの変更を加えました。

変更、

  • 代わりの一つは、私のapplicationListに1項目を追加するには、一度addAllを使用してでそれをすべてを追加し、notifyDataSetChangedを呼び出します。これにより、エラーが取り除かれます。
  • oncreate内でadapterを初期化します。
  • 2つではなくAsyncTaskを1つ使用してください。
  • 1つを使用AsyncTaskdoInbackgroundvoidの代わりにArrayListを返します。

は、以下のあなたは、アダプタにデータを設定し

// start from 0 
int offset = 0; 

// create dataset 
ArrayList<ApplicationPojo> applicationList = new ArrayList(); 

// create adapter 
ListViewAppAdapter adapter; 

onCreate(){ 
    ... 

    TextView noResult = (TextView) findViewById(R.id.noresult); 

    adapter = new ListViewAppAdapter(ListViewAppAll.this, applicationList, listview); 
    listview.setAdapter(adapter); 

    listview.setOnScrollListener(new AbsListView.OnScrollListener() { 

     @Override 
     public void onScrollStateChanged(AbsListView view, int scrollState) { 
      int threshold = 1; 
      int count = listview.getCount(); 

      if (scrollState == SCROLL_STATE_IDLE) { 
       if (listview.getLastVisiblePosition() >= count - threshold) { 
        if (listSize >= offset) { 
         offset = offset + 15; 
         new LoadData().execute(); 

        } else { 
         Toast.makeText(getApplicationContext(), "End of list!", Toast.LENGTH_LONG).show(); 
         listview.removeFooterView(footer); 
        } 
       } 
      } 
     } 

     @Override 
     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 

     } 
    }); 
} 

private class LoadData extends AsyncTask<Void, Void, ArrayList<ApplicationPojo>> { 

    @Override 
    protected Void doInBackground(Void... voids) { 
     ArrayList<ApplicationPojo> temp = new ArrayList(); 

     try { 
      JSONArray jsonData = new GetListviewsData().getJSONData(webfileName, limit, offset, priceCat); 

      for (int i = 0; i < jsonData.length() - 1; i++) { 
       JSONObject c = jsonData.getJSONObject(i); 

       id = c.getString("id"); 
       name = c.getString("name"); 
       logo = c.getString("logo"); 
       developer = c.getString("developer"); 
       category = c.getInt("category"); 
       fileName = c.getString("filename"); 
       path = c.getString("path"); 
       appSize = c.getDouble("size_bytes"); 
       price = c.getInt("price"); 
       data = c.getString("data1").equals("t"); 
       obb = c.getString("data").equals("t"); 
       temp.add(new ApplicationPojo(id, name, logo, developer, appSize, category, fileName, path, data, obb, price)); 
      } 
      JSONObject sizeObj = jsonData.getJSONObject(jsonData.length() - 1); 
      listSize = sizeObj.getInt("size"); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return temp; 
    } 

    @Override 
    protected void onPostExecute(ArrayList<ApplicationPojo> list) { 
     if(listSize == 0) { 
      noResult.setVisibility(View.VISIBLE); 
     } else { 
      applicationList.addAll(list); 
      applicationList.notifyDataSetChanged(); 
     } 
     mProgressDialog.dismiss(); 
    } 
} 
+0

ありがとうございました! – Shoaib

+0

私は上記のコードを実装しましたが、public int getCount(){ return applicationList.size();でnullポインタ例外を返します。 } – Shoaib

+0

'onCreate'の前に' ArrayList applicationList = new ArrayList(); 'を追加しましたか? –