2016-02-27 52 views
0

我向Android製作了簡單的客戶端服務器。 我從服務器發送一個對象到客戶端時遇到問題。 收到的對象正常,當我檢查日誌時,它顯示對象已成功發送。Android客戶端服務器不會顯示對象

當我嘗試獲取此對象並將其放入我的ListView適配器時,會出現此問題。 適配器的工作原理,我用隨機創建的ArrayList進行了檢查。 我的問題是當我試圖將AsyncTask的值放入我的適配器時。

public class RestaurantListFragment extends Fragment { 

    private ArrayList<Resturant> res = new ArrayList<>(); 
    private ListAdapter adapter; 
    private Firebase restRef = new Firebase("https://restmeup.firebaseio.com/restaurants"); 
    private Client mClient; 
    // private connectTask t = (connectTask)new connectTask().execute(); 


    public RestaurantListFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     new connectTask().execute();  

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    //  new connectTask(getView()).execute(); 
     final View rootView = inflater.inflate(R.layout.fragment_two, container, false);  

     ListView restaurantList = (ListView) rootView.findViewById(R.id.list); 

     adapter = new ListAdapter(getContext(), res, getActivity()); 
     restaurantList.setAdapter(adapter); 

     // connectTask t = (connectTask)new connectTask().execute(); 
     if (mClient != null) { 
      mClient.sendMessage("bar"); 
     } 
     SqlQueriesConverter sql = new SqlQueriesConverter(); 
     sql.getResurantsListQuery("bar"); 
     //  sql.getUserFavoritesResturants(accessToken.getUserId()); 
     mClient.sendMessage(sql.getQuery()); 
     // t.setArray(res); 


     mClient.sendMessage("/quit"); 
     mClient.stopClient(); 

     final EditText searchText = (EditText)rootView.findViewById(R.id.searchListView); 

     searchText.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
       System.out.println("Before---------"); 
      } 
      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       String text = searchText.getText().toString().toLowerCase(Locale.getDefault()); 
       adapter.filter(text); 
       adapter.notifyDataSetChanged(); 
       System.out.println("array: " + res.toString()); 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       System.out.println("After---------"); 
      } 
     }); 

     // Inflate the layout for this fragment 
     return rootView; 
    } 


    public class connectTask extends AsyncTask<ArrayList<?>,ArrayList<?>,Client> { 
    // private Client mClient; 
     private ArrayList<?> arrayList = new ArrayList<>(); 

     @Override 
     protected Client doInBackground(ArrayList<?>... message) { 

      //we create a Client object and 
      mClient = new Client(new Client.OnMessageReceived() { 
       @Override 
       //here the messageReceived method is implemented 
       public void messageReceived(ArrayList<?> message) { 
        //this method calls the onProgressUpdate 
        // publishProgress(message); 
        onProgressUpdate(message); 
       } 
      }); 

      mClient.run(); 
      return null; 
     } 

     // @Override 
     protected void onProgressUpdate(ArrayList<?>... values) { 
       super.onProgressUpdate(values); 

      ArrayList<?> arr2; 
      if (values[0].get(0) instanceof Resturant){ 
       Log.d("step 1", "1"); 
       if (((ArrayList<?>)values[0]).get(0)instanceof Resturant) { 

        // arr2 = (ArrayList<Resturant>) values[0]; 
        res = (ArrayList<Resturant>) values[0]; 
        adapter.notifyDataSetChanged(); 
        Log.d("array",res.toString());  

       } 
      } 
      if (values[0].get(0)instanceof Review){ 
       arr2 = (ArrayList<Review>) values[0]; 
      } 
      if (values[0].get(0)instanceof UserFavorites){ 
       arr2 = (ArrayList<Review>) values[0]; 
       Log.d("step 2", "2"); 
      }  
     }  
    }  
} 
+0

什麼不行?你有錯誤嗎? –

回答

0

有兩樣東西需要更改才能使用AsyncTask。您需要的第一項更改是返回您在doInBackground方法中從mClient獲得的ArrayList。這有點麻煩,因爲它看起來像Client已經異步運行,因爲你通過回調來獲得結果(這是你在那裏的匿名類)。第二個變化是在AsyncTask上實施onPostExecute,因爲這是發送doInBackground的結果的地方。您將在012處添加從doInBackground發送到您的適配器的結果。

無論如何,因爲它看起來像Client已經在異步執行工作,所以根本不需要使用AsyncTask。只需實現邏輯,將結果添加到Client.OnMessageReceived回調中的適配器。

只需獲取onProgressUpdate中已有的代碼並將其丟入messageReceived即可。類似這樣的:

mClient = new Client(new Client.OnMessageReceived() { 
    @Override 
    //here the messageReceived method is implemented 
    public void messageReceived(ArrayList<?> values) { 
     if (values[0].get(0) instanceof Resturant){ 
      Log.d("step 1", "1"); 
      if (((ArrayList<?>)values[0]).get(0)instanceof Resturant) { 
       res = (ArrayList<Resturant>) values[0]; 
       adapter.notifyDataSetChanged(); 
       Log.d("array",res.toString());  
      } 
     } 
    } 
}); 
+0

它仍然無法正常工作。也許我有客戶端類的問題? –

+0

我不知道你的Client類看起來喜歡什麼,但考慮到它的結構,它似乎是異步運行代碼。我建議您查看Firebase文檔,瞭解如何以最直接的方式檢索數據:https://www.firebase.com/docs/android/guide/retrieving-data.html – ebarrenechea

相關問題