2016-09-24 80 views
-1

所以,我不知道該怎麼做。我想獲取jsonData並使用來自同一個Activtiy的這些數據設置一個適配器。這甚至有可能嗎?如果是,我應該怎麼做,因爲它會因爲「下載」延遲而一直崩潰。適配器想要設置比獲取數據的時間早,因爲它是空數組。如何設置recyclerView適配器?

private SzabadEuMusorok[] mSzabadEuMusoroks; 

@InjectView(R.id.archivumReyclerView) 
RecyclerView mRecyclerView; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_archivum); 
    ButterKnife.inject(this); 

    setArray(); 

    CollectionsAdapter adapter = new CollectionsAdapter(this, mSzabadEuMusoroks); 
    mRecyclerView.setAdapter(adapter); 

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); 
    mRecyclerView.setLayoutManager(layoutManager); 

    mRecyclerView.setHasFixedSize(true); 
} 
+0

在你的下載器中設置setAdapter()...... – sushildlh

+0

我應該在哪裏設置它?什麼是下載器? – Marci

+0

你的JSON數據在哪裏? – sushildlh

回答

0

請務必從調用webservice的

請了遵循以下讓你得到關於如何實現它的概述適配器設置你的數據後,才:

 public class MainActivity extends Activity implements OnClickListener { 
      private SzabadEuMusorok[] mSzabadEuMusoroks; 

      @InjectView(R.id.archivumReyclerView) 
      RecyclerView mRecyclerView; 
      RecyclerView.LayoutManager layoutManager ; 
      @Override 
      protected void onCreate(@Nullable Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_archivum); 
       ButterKnife.inject(this); 

       setArray(); 


       layoutManager = new LinearLayoutManager(this); 

     new LongOperation().execute("");//call your webservice 
      } 



       private class LongOperation extends AsyncTask<String, Void, String> { 

        @Override 
        protected String doInBackground(String... params) { 
         for (int i = 0; i < 5; i++) { 
          try { 

      //call your webservice here 
          } catch (InterruptedException e) { 
           Thread.interrupted(); 
          } 
         } 
         return serviceResult;// this will be passed to onPostExecute 
        } 

        @Override 
        protected void onPostExecute(String result) { 
mRecyclerView.setLayoutManager(layoutManager); 

       mRecyclerView.setHasFixedSize(true); 
        CollectionsAdapter adapter = new CollectionsAdapter(this, mSzabadEuMusoroks); 
      mRecyclerView.setAdapter(adapter); //set your adapter using result in webservice called in doinbackground 
        } 

        @Override 
        protected void onPreExecute() { 
      // show a progress dialog 

      } 

       } 
      } 
+0

謝謝爲了答案!但我想用OkHttp來做。在完成設置之後,我調用了setAdapter,現在它崩潰,出現異常:沒有連接適配器;跳過佈局 – Marci

+0

迴應你的okhttp響應'setAdapter()'響應成功後...... – sushildlh

+0

請將onPostExecute(String結果)中的所有代碼複製到你的responseSuccess塊 –

相關問題