2017-06-01 67 views
0

我的意圖是通過okhttp接口取回從服務器獲取的數據。 okhttp方法在異步中運行,因此在與活動分開的線程中運行。有問題的活動是創建listView的片段。我試圖通過ArrayAdapter與okhttp的數據填充listView。如何將數據從okhttp異步線程回發到主線程中的ArrayAdapter?

如何將數據從okhttp異步線程回發到ArrayAdapter?

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 



    View view = inflater.inflate(R.layout.fragment_friends, container, false); 
    final ListView listview = (ListView) view.findViewById(R.id.friendListView); 

    ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, nfl); 
    listview.setAdapter(listViewAdapter); 


    OkHttpClient client = new OkHttpClient(); 
    String url = "http://192.168.8.101:7777/friendlist"; 
    Request request = new Request.Builder() 
      .url(url) 
      .build(); 
    client.newCall(request).enqueue(new Callback() { 
     @Override 
     public void onFailure(Call call, IOException e) { 
      e.printStackTrace(); 
     } 

     @Override 
     public void onResponse(Call call, Response response) throws IOException { 
      if (!response.isSuccessful()) { 
       throw new IOException("Unexpected code " + response); 
      } 
      String RD = response.body().string(); 
      String trimmedList = RD.substring(1, RD.length()-1); 
      String[] fl = trimmedList.split(","); 
      final List<String> nfl = new ArrayList<String>(); 
      for (String element : fl){ 
       String ee = element.replaceAll("^\"|\"$", ""); 
       nfl.add(ee); 
      } 


     } 
    }); 


    return view; 
} 

回答

0

您必須啓動您的適配器並列出onResponse CallBack。如果沒有響應,只需顯示錯誤對話框,否則,啓動適配器並顯示結果。該代碼必須是這樣的:

@Override 
     public void onResponse(Call call, Response response) throws IOException { 
      if (!response.isSuccessful()) { 
       throw new IOException("Unexpected code " + response); 
      } 
      String RD = response.body().string(); 
      String trimmedList = RD.substring(1, RD.length()-1); 
      String[] fl = trimmedList.split(","); 
      final List<String> nfl = new ArrayList<String>(); 
      for (String element : fl){ 
       String ee = element.replaceAll("^\"|\"$", ""); 
       nfl.add(ee); 
      } 
//Put your code for showing the list item here. 
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, nfl); 
    listview.setAdapter(listViewAdapter); 


     } 
+0

這不可能是正確的。當我這樣做時,我得到一個'致命例外:只有創建視圖層次結構的原始線程可以觸及其視圖。' – Jupiter

+0

使用onResponse方法(列表 resultList)創建接口,並在您獲取數據時調用此方法並將其作爲說出你的結果列表,並讓你的活動實現這個方法,並從參數中拿出列表並顯示它! – Fakher

1

如果您有活動的實例的引用您可以創建填充適配器可運行和使用活動的runOnUiThread方法

結賬的文件運行它:

https://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)

+0

如何從工作線程中獲取活動的實例? – Jupiter

+0

如果我理解你的代碼是正確的,那麼你已經在片段內創建了工作線程,如果這是你可以使用方法getAcitvity()的情況,其他方式是你可以將一個活動實例傳遞給線程,但確保它是一個星期參考以避免內存泄漏。 –

+0

謝謝,這就是答案! (下面更新它) – Jupiter

0

答案是內runnable()

創建 runOnUiThread並調用一個ArrayAdapter

下面是正確的代碼:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_friends, container, false); 
    final ListView listview = (ListView) view.findViewById(R.id.friendListView); 


    OkHttpClient client = new OkHttpClient(); 
    String url = "http://192.168.8.101:7777/friendlist/" + USERNAME; 
    Request request = new Request.Builder() 
      .url(url) 
      .build(); 
    client.newCall(request).enqueue(new Callback() { 
     @Override 
     public void onFailure(Call call, IOException e) { 
      e.printStackTrace(); 
     } 

     @Override 
     public void onResponse(Call call, Response response) throws IOException { 
      if (!response.isSuccessful()) { 
       throw new IOException("Unexpected code " + response); 
      } 
      String RD = response.body().string(); 
      response.close(); 
      String trimmedList = RD.substring(1, RD.length()-1); 
      String[] fl = trimmedList.split(","); 
      final List<String> nfl = new ArrayList<String>(); 
      for (String element : fl){ 
       String ee = element.replaceAll("^\"|\"$", ""); 
       nfl.add(ee); 
      } 

      System.out.println(nfl); 
      getActivity().runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, nfl); 
        listview.setAdapter(listViewAdapter); 
       } 
      }); 

     } 
    }); 


    return view; 
} 
相關問題