2010-06-29 107 views
1

我有一個自定義的ArrayAdapter的列表視圖。當ArrayAdapter改變了gui應該更新。但是,當它應該更新時,除非您在模擬器上按一個鍵,否則它不會。可點擊的行不可點擊,除非我點擊了仿真器上的任意按鈕。 爲ListView底層數據被其他地方改變,然後通知ListView不會更新

public void update(java.util.Observable arg0, Object arg1) 
    { 
     Log.e("Supposed","to update"); 
     ad.notifyDataSetChanged(); //Ad is the arrayadapter       
    } 

數據在魔術觸摸的按鈕後適當地呈現的觀察者(在這種情況下,列表視圖)。但爲什麼? 有什麼想法? 當我檢查ArrayAdapter的項目 - 參考它顯示更新的值,但gui不會。 plz幫助

編輯:

private class MustAdapter extends ArrayAdapter<Item> 
    { 
     public MustAdapter(Context context, int textViewResourceId, ArrayList<Item> items) 
     { 
      super(context,textViewResourceId,items); 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
       View v = convertView; 
       if (v == null) { 
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        v = vi.inflate(R.layout.row, null); 
       } 

       Item o = items.get(position); 
       if (o != null) { 

         TextView tt = (TextView) v.findViewById(R.id.text1); 
         TextView bt = (TextView) v.findViewById(R.id.text2); 
         ImageView iv= (ImageView) v.findViewById(R.id.img); 
         ImageView ib = (ImageView)v.findViewById(R.id.OnOffButt); 
         if (ib != null) { 
          ib.setTag(position); 
          if (((Ventilation)o).enabled ==0) { 
           ib.setImageResource(android.R.drawable.ic_input_delete); 
          } 
          else 
           ib.setImageResource(android.R.drawable.ic_input_add); 
          ib.setOnClickListener(new OnClickListener() { 

          public void onClick(View arg0) { 
           int index=((Integer)arg0.getTag()).intValue(); 
           Ventilation v= (Ventilation)items.get(index); 
           v.enabled=v.enabled != 0 ? 0 : 1; 
           //ad.notifyDataSetChanged(); 
           DU.toggle(v); 


          } 
         }); 
         } 

         if (iv !=null){ 
          iv.setImageResource(R.drawable.fan); 
         } 
         if (tt != null) { 
           tt.setText(""+((Ventilation)o).actual);       } 
         if(bt != null){ 
           bt.setText(""+((Ventilation)o).setPoint); 
         } 
       } 
       return v; 
     } 

即使當我使用arrayadapter.add(項目),我必須按其他地方爲它重新加載

回答

0

好吧,事情是沒有線程,但gui線程可以更新gui線程。 所以我做的是在onCreate事件中聲明一個Handler。

h= new Handler(){ 
       @Override 
       public void handleMessage(Message msg) 
       { 
        switch (msg.what) 
        { 
        case 0: 
         ad.notifyDataSetChanged(); 
        } 
       } 
      }; 

,並在我的public void update(java.util.Observable arg0, Object arg1)

我把

Message ms= new Message(); 
     ms.what=0; 
     h.sendMessage(ms); 

完蛋了!希望這個對於某個人來說很方便。

0

當在一個ArrayAdapter已 改變gui應該更新。

如果意志,如果你通過add()insert(),或remove()改變ArrayAdapter的數據。如果您以其他方式更改數據,ArrayAdapter將不知道有關更改。

+0

即使arrayadapter.add(Item)需要按任意按鈕才能響應更改。 – Raul 2010-06-29 13:32:24

+0

@Raul:不,它沒有。見http://github.com/commonsguy/cw-android/tree/master/Threads/Asyncer/和http://github.com/commonsguy/cw-andtutorials/tree/master/04-ListView/ – CommonsWare 2010-06-29 13:58:17

+0

猜猜我的模擬器然後不起作用。我會在幾天內嘗試Ubuntu的 – Raul 2010-06-30 06:34:58