2011-10-07 70 views
3

我想在設定的時間間隔更新自定義ListView內的TextView,例如TextView將每200ms更新一次,但我無法弄清楚如何做到這一點。該對象內部更新一個數字,我想在mTitleText文本視圖中顯示,但下面的代碼顯示,此時我只能在用戶按下按鈕時才能實現它。更新自定義ListView中的TextView

public class ListAdapter extends BaseAdapter { 
private ArrayList<Object> mObjects; 
private int mNumObjs = 0; 

private LayoutInflater mLayoutInflater; 
private Context mContext; 

public ListAdapter (Context context, ArrayList<Object> objects) { 
    mObjects;= objects; 
    mLayoutInflater = LayoutInflater.from(context); 
    mContext = context; 
} 

public int getCount() { 
    return mObjects;.size(); 
} 

public Object getItem(int position) { 
    return mObjects;.get(position); 
} 

public long getItemId(int position) { 
    return position; 
} 

public void addObject(Object obj) { 
    obj.setId(mNumObjs); 
    mObjects.add(obj); 
    (mNumObjs);++; 
    notifyDataSetChanged(); 
} 

public void deleteObject(int pos) { 
    mObjects;.remove(pos); 
    notifyDataSetChanged(); 
} 

public View getView(final int position, View convertView, ViewGroup parent) { 
    final TimerView holder; 

    if(convertView == null) { 
     convertView = mLayoutInflater.inflate(R.layout.customlistview, null); 

     holder = new HolderView(); 
     holder.mListPosition = position; 
     holder.mDeleteButton = (Button)convertView.findViewById(R.id.Delete); 
     holder.mDeleteButton.setText("Button No: " + position); 
     holder.mDeleteButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       deleteObject(holder.mListPosition); 
      } 
     }); 

     holder.mButton = (Button)convertView.findViewById(R.id.Button); 
     holder.mButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Object obj = mObjects.get(holder.mListPosition); 

       mTitleText.setText(obj.getNum()); 
      } 
     }); 

     convertView.setTag(holder); 
    } 
    else { 
     holder = (TimerView) convertView.getTag(); 
    } 
    holder.mListPosition = position; 
    holder.mDeleteButton.setText("Button No: " + position); 

    return convertView; 
} 

class HolderView{ 
    int mListPosition; 
    Button mDeleteButton; 
    Button mButton; 

    TextView mTitleText; 
} 
} 
+0

如何在更新文本時調用視圖上的invalidate方法。 http://developer.android.com/reference/android/view/View.html – Pramod

+0

只需使用計時器或線程200毫秒,並完成後,只需調用notifyDataSetChanged()到您的listAdapter和getView()更新textview。嘗試這個。 – user370305

+0

@Pramod我將如何去關於無效的對象內的視圖? – kiwijus

回答

1

好我自己弄清楚了,如果你的更新不需要非常頻繁(> 1秒),你可以使用notifyDataSetChanged(),但是如果像我一樣,你需要每隔200毫秒不斷更新列表視圖您需要遍歷列表視圖中的可見對象並對其進行更新。

private Runnable showUpdate = new Runnable(){ 
    public void run(){ 
     mAdapter.updateList(); 
     //mAdapter.notifyDataSetChanged(); 
     int count = mListView.getCount(); 

     for(int i = 0; i < count; i ++) 
     { 
      View convertView = mListView.getChildAt(i); 

      if(convertView != null) 
      { 
       HolderView holder = (HolderView) convertView.getTag(); 

       Object obj = (Object)mAdapter.getItem(holder.mListPosition); 

       holder.mTitleText.setText(obj.getText()); 
      } 
     } 
    } 
}; 

Thread mThread = new Thread() 
{ 
    @Override 
    public void run() { 
     try { 
      while(true) { 
       sleep(100); 
       mHandler.post(showUpdate); 

       //mHandler.sendEmptyMessage(MSG_UPDATE); 
      } 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
}; 
0

更新您的列表mObjects文字和您的適配器上調用notifyDataSetChanged()

+0

我設法通過這樣做來實現它的工作,但是這導致listview有一些「滯後」,使它有時不可能點擊按鈕... – kiwijus

相關問題