开发者

Update TextView within custom ListView

I'm trying to update the TextView within a custom ListView at a set time interval, for example the Te开发者_开发知识库xtView will update every 200ms however I can't figure out how to do this. The object updates with a number internally and I would like to show that in the mTitleText Textview however as the code below shows at the moment I can only achieve it when the user presses a button.

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;
}
}


Okay I managed to figure this out myself, if your updates don't need to be very frequent ( >1 sec ) you can use notifyDataSetChanged() however if like me you need to constantly update the listview every 200ms or so you need to iterate through the visible objects on the list view and update it.

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();
        }
    }
};


Update the text in your list mObjects and call notifyDataSetChanged() on your adapter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜