2012-09-20 12 views
5

से डेटा कैसे पास करें I सर्वर से डेटा लोड करने के लिए asyncTask कक्षा लागू कर रहा हूं। मेरे पास पहले से ही डेटा है और मैं इसे यूआई थ्रेड में पास करना चाहता हूं, लेकिन मुझे नहीं पता कि कैसे।AsyncTask से Ui Thread

public class InboxHandler extends FragmentActivity implements OnLineSelectedListener { 

    static final int GET_CODE = 0; 
    private TextView textView3, textView2; 
    private Button button1, button2, button3; 
    private LinearLayout tab1, tab2, tab3; 
    private CheckBox cbVerPorCategoria; 
    private ActionBar actionBar; 
    private TextView txtTitle; 
    private Settings settings; 
    FragmentTransaction fragmentTransaction; 
    BottomPanel bottomPanel; 
    public List<OrderRequest> orderRequestArray; 
    private OrderRequestParser orderRequestParser; 
    public static int number; 
    int clickCounter = 0; 
    private boolean doubleBackToExitPressedOnce = false; 
    private static Context context; 
    DataLoader dataLoader; 
    private MyAsynckTask myAsynckTask; 

    public InboxHandler(){} 

    public List<OrderRequest> getOrderRequestArray() { 
     return orderRequestArray; 
    } 

    public void setOrderRequestArray(List<OrderRequest> orderRequestArray) { 
     this.orderRequestArray = orderRequestArray; 
    } 

    public static Context getContext() 
    { 
     return context; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_inbox); 

     context = this.getApplicationContext(); 

     settings = Settings.getInstance(); 
     settings.setPrefs(getSharedPreferences(settings.getPREFERENCES(), Activity.MODE_PRIVATE)); 
     settings.setPrefsEditor(settings.getPrefs().edit());   
     settings.getPrefsEditor().putBoolean("isLoggedIn", true); 
     settings.isVistaSimple = true; 

     actionBar = (ActionBar) findViewById(R.id.actionbar); 

     initComponents(); 
     prepareActionBar(); 
    } 

    private void initComponents() { 

     try { 
      MyAsynckTask myAsynckTask = new MyAsynckTask(this); 
      myAsynckTask.execute(); 

      //HERE I USED A SUGGESTION IN A SIMILAR QUESTION. THAT IS: PASS THE ACTIVITY TO THE ASYNCTASK, AND FROM IT, SAVE IN THE ACTIVITY, THE DATA THAT IU WANT FROM THE ASYNCTASK. I DO THAT, BUT IN THE ACTIVITY, THE DATA IS NULL I SUPPOSE DUE TO, I DONT EVEN SEE IN THE LOGCAT, THE FOLLOWING RESULT: 

      try{ 
       Log.e("e", ""+this.getOrderRequestArray().get(0).getDocNumber()); 
      } catch(Exception e){ 
       e.printStackTrace(); 
      } 

//If you see the onPostExecute, I test if the data I want is there. and it is there! I set that to the list of the class InboxHandler, but in the above code, when i try to test if it is there, it´s not. And that code don´t even appear in logcat. The "Log.e("e", ""+this.getOrderRequestArray().get(0).getDocNumber());" nor the "e.printStackTrace();" 

      bottomPanel =new BottomPanel(); 
      Bundle bundle = new Bundle(); 
      bundle.putSerializable("arrayPedidos", (Serializable) orderRequestArray); 
      bottomPanel.setArguments(bundle); 

      //actionBar.setTitle("Inbox"); 
      NotificationFragment notificationFragment = new NotificationFragment(orderRequestArray, actionBar); 

      Bundle bundleNotificationFragment = new Bundle(); 
      bundleNotificationFragment.putString("title", "Inbox"); 
      notificationFragment.setArguments(bundleNotificationFragment); 

      fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
      fragmentTransaction.add(R.id.frame_list_container, notificationFragment); 
      fragmentTransaction.add(R.id.frame_bottom_panel, bottomPanel);  
      fragmentTransaction.commit(); 

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

    } 

    private class MyAsynckTask extends AsyncTask { 

     private String stringMensaje; 
     public List<OrderRequest> orderRequestArray; 
     private OrderRequestParser orderRequestParser; 
     private Request request; 
     private Settings settings; 
     private InboxHandler inboxHandler; 

     public MyAsynckTask(InboxHandler inboxHandler){ 
      this.inboxHandler = inboxHandler; 
     } 

     @Override 
     protected Object doInBackground(Object... params) { 

      List<OrderRequest> array = this.getOrderRequests(); 
      settings = Settings.getInstance(); 


      if(array != null){ 
       settings.setOrderRequestArray(array); 
       orderRequestArray = array; 
       Log.e("orderRequestArray", "Me trajo el orderRequestArray en doInBackground!"); 
      } else { 
       Log.e("orderRequestArray", "No me trajo el orderRequestArray en doInBackground!"); 
      } 

      return array; 

     } 

     @Override 
     protected void onPostExecute(Object result) { 
      super.onPostExecute(result); 

      List<OrderRequest> array = (List<OrderRequest>) result; 

      this.setOrderRequestArray(array); 

      if(orderRequestArray != null){ 
       inboxHandler.setOrderRequestArray(orderRequestArray); 
       Log.e("orderRequestArray", "is not null en onPostExecute!"); 
      } else { 
       Log.e("orderRequestArray", "is null en onPostExecute!"); 
      } 


      if(inboxHandler.getOrderRequestArray() != null){ 
       Log.e("inboxHandler.getOrderRequestArray()", "is not null en onPostExecute!"); 
      } 
      else { 
       Log.e("orderRequestArray", "is null en onPostExecute!"); 
      } 
     } 


     public List<OrderRequest> getOrderRequests() { 

      orderRequestParser = new OrderRequestParser(); 
      orderRequestArray = new ArrayList<OrderRequest>(); 
      List<OrderRequest> orderRequestArray = orderRequestParser.listOrderRequest(); 

      Settings.getInstance().setOrderRequestArray(orderRequestArray); 
      if(orderRequestArray != null){ 
       Log.e("orderRequestArray", "Me trajo el orderRequestArray en getOrderRequestArray!"); 
      } else { 
       Log.e("orderRequestArray", "No me trajo el orderRequestArray en getOrderRequestArray!"); 
      } 

      return orderRequestArray; 

     } 

     public void setOrderRequestArray(List<OrderRequest> orderRequestArray) { 
      this.orderRequestArray = orderRequestArray; 
     } 

     public List<OrderRequest> getOrderRequestArray() { 
      return orderRequestArray; 
     } 
    } 

उत्तर

2
protected void onPostExecute(Object result) 

यह जीयूआई धागा में चल रहा है, तो आप जीयूआई तत्वों वहाँ पहुँच सकते हैं:

इस कोड है।

और केवल वहाँ होना चाहिए:

 try{ 
      Log.e("e", ""+this.getOrderRequestArray().get(0).getDocNumber()); 
     } catch(Exception e){ 
      e.printStackTrace(); 
     } 

और

Bundle bundle = new Bundle(); 
    bundle.putSerializable("arrayPedidos", (Serializable) orderRequestArray); 
    bottomPanel.setArguments(bundle); 

मैं कोई कारण नहीं देख सकते हैं, यही कारण है कि इस सवाल का मतदान किया है।

संबंधित मुद्दे